diff --git a/app/listmonk/utils.py b/app/listmonk/utils.py index 9d71547..11e2863 100644 --- a/app/listmonk/utils.py +++ b/app/listmonk/utils.py @@ -12,6 +12,14 @@ headers = { 'Content-Type': 'application/json;charset=utf-8' } +def get_auth(headers): + if settings.LISTMON_TOKEN: + headers['Authorization'] = 'token ' + settings.LISTMON_TOKEN + auth = None + else: + auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD) + return auth + def send_week(date): from . import views @@ -39,7 +47,7 @@ def send_email(name, subject, body): 'body': body } url = settings.LISTMONK_API + 'campaigns' - auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD) + auth = get_auth(headers) r = requests.post(url, json=data, headers=headers, auth=auth).json() list_id = r.get('data', {}).get('id') if list_id and settings.LISTMONK_SEND: @@ -59,13 +67,13 @@ def email2name(email): def is_subscribed(email): url = settings.LISTMONK_API + 'subscribers' - auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD) + auth = get_auth(headers) exists = url + '?' + "list_id=&query=email='%s'&page=1&order_by=id&order=desc" % email - return bool(len(requests.get(exists, auth=auth).json()['data']['results'])) + return bool(len(requests.get(exists, headers=headers, auth=auth).json()['data']['results'])) def add_email(email): url = settings.LISTMONK_API + 'subscribers' - auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD) + auth = get_auth(headers) if not is_subscribed(email): data = { "email": email, diff --git a/app/settings/common.py b/app/settings/common.py index 9646823..f71bf43 100644 --- a/app/settings/common.py +++ b/app/settings/common.py @@ -165,6 +165,7 @@ CELERY_RESULT_BACKEND = 'django-db' SIGNAL_MODERATORS = [] LISTMONK_USER = "listmonk" LISTMONK_PASSWORD = "listmonk" +LISTMONK_TOKEN = None LISTMONK_API = "http://localhost:9000/api/" LISTMONK_LISTS = [] LISTMONK_SEND = False