import logging import requests from django.conf import settings from django.utils.timezone import datetime, timedelta logger = logging.getLogger(__name__) def send_week(year, month, day): from . import views date = '%s-%02d-%02d' % (year, month, day) date = datetime.strptime(date, '%Y-%m-%d') monday = views.get_monday(date) body = views.week(year, month, day) name = 'weekly-digest-%s' % monday subject = 'Phantas.ma weekly update %s' % views.format_week(monday) 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