phantasmobile/app/listmonk/utils.py
2023-09-23 14:38:28 +02:00

47 lines
1.3 KiB
Python

import logging
import requests
from django.conf import settings
logger = logging.getLogger(__name__)
def send_week(year, month, day):
from . import views
body = views.week(year, month, day)
week = '%s-%02d-%02d' % (year, month, day)
name = 'weekly-digest-%s' % week
subject = 'Phantas.ma weekly update %s' % views.format_week(week)
return send_email(name, subject, body)
def send_email(name, subject, body):
'''
send_meail('weekly digest 2023-09-22', 'Phantas.ma weekly update Sep 23', body)
'''
if not settings.LISTMONK_LISTS:
return False
data = {
'name': name,
'subject': subject,
'lists': settings.LISTMONK_LISTS,
'content_type': 'html',
'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']
data = {"status": "running"}
r = requests.put(url, json=data, headers=headers, auth=auth).json()
return True
else:
return False