From 073f8add07fb8eeff4d93bea8e46bd7d30611292 Mon Sep 17 00:00:00 2001 From: j Date: Thu, 28 Sep 2023 11:33:42 +0200 Subject: [PATCH] add email, don't send by default, only queue --- app/listmonk/utils.py | 39 ++++++++++++++++++++++++++++++++++----- app/settings/common.py | 1 + 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/app/listmonk/utils.py b/app/listmonk/utils.py index 18f671d..21e9d11 100644 --- a/app/listmonk/utils.py +++ b/app/listmonk/utils.py @@ -8,6 +8,10 @@ from django.utils.timezone import datetime, timedelta logger = logging.getLogger(__name__) +headers = { + 'Content-Type': 'application/json;charset=utf-8' +} + def send_week(year, month, day): from . import views @@ -34,17 +38,42 @@ def send_email(name, subject, body): 'type': 'regular', 'body': body } - headers = { - 'Content-Type': 'application/json;charset=utf-8' - } url = settings.LISTMONK_API + 'campaigns' auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD) r = requests.post(url, json=data, headers=headers, auth=auth).json() - if 'id' in r.get('data', {}): - url = settings.LISTMONK_API + 'campaigns/%s/status' % r['data']['id'] + list_id = r.get('data', {}).get('id') + if list_id and settings.LISTMONK_SEND: + url = settings.LISTMONK_API + 'campaigns/%s/status' % list_id data = {"status": "running"} r = requests.put(url, json=data, headers=headers, auth=auth).json() return True else: return False + +def email2name(email): + name = email.split("@")[0] + name = name.replace(".", " ") + name = " ".join([part.capitalize() for part in name.split(" ")]) + return name + + +def add_email(email): + url = settings.LISTMONK_API + 'subscribers' + auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD) + exists = url + '?' + "list_id=&query=email='%s'&page=1&order_by=id&order=desc" % email + if not len(requests.get(exists, auth=auth).json()['data']['results']): + data = { + "email": email, + "name": email2name(email), + "status": "enabled", + "lists": settings.LISTMONK_LISTS, + } + r = requests.post(url, json=data, headers=headers, auth=auth).json() + if r.get("data", {}).get("id"): + return True + else: + logger.errror("failed to add email to list %s => %s", email, r) + return False + return False + diff --git a/app/settings/common.py b/app/settings/common.py index d8001fe..9646823 100644 --- a/app/settings/common.py +++ b/app/settings/common.py @@ -167,6 +167,7 @@ LISTMONK_USER = "listmonk" LISTMONK_PASSWORD = "listmonk" LISTMONK_API = "http://localhost:9000/api/" LISTMONK_LISTS = [] +LISTMONK_SEND = False RATELIMIT_CACHE_BACKEND = "app.brake_backend.BrakeBackend"