use listmonk token

This commit is contained in:
j 2025-01-23 15:19:19 +05:30
parent bee761e0c4
commit e3ece477c0
2 changed files with 13 additions and 4 deletions

View file

@ -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,

View file

@ -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