From e82d5d5f156190d378b3ff5a307ce24f52b35418 Mon Sep 17 00:00:00 2001 From: j Date: Sat, 23 Sep 2023 14:38:28 +0200 Subject: [PATCH] email backend --- app/listmonk/__init__.py | 0 app/listmonk/admin.py | 3 ++ app/listmonk/apps.py | 6 +++ app/listmonk/migrations/__init__.py | 0 app/listmonk/models.py | 3 ++ app/listmonk/test.py | 58 +++++++++++++++++++++++++++++ app/listmonk/tests.py | 3 ++ app/listmonk/utils.py | 47 +++++++++++++++++++++++ app/listmonk/views.py | 23 ++++++++++++ app/settings/common.py | 6 +++ app/templates/weekly_email.html | 18 +++++++++ 11 files changed, 167 insertions(+) create mode 100644 app/listmonk/__init__.py create mode 100644 app/listmonk/admin.py create mode 100644 app/listmonk/apps.py create mode 100644 app/listmonk/migrations/__init__.py create mode 100644 app/listmonk/models.py create mode 100644 app/listmonk/test.py create mode 100644 app/listmonk/tests.py create mode 100644 app/listmonk/utils.py create mode 100644 app/listmonk/views.py create mode 100644 app/templates/weekly_email.html diff --git a/app/listmonk/__init__.py b/app/listmonk/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/listmonk/admin.py b/app/listmonk/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/app/listmonk/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/app/listmonk/apps.py b/app/listmonk/apps.py new file mode 100644 index 0000000..18ad51e --- /dev/null +++ b/app/listmonk/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ListmonkConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "app.listmonk" diff --git a/app/listmonk/migrations/__init__.py b/app/listmonk/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/listmonk/models.py b/app/listmonk/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/app/listmonk/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/app/listmonk/test.py b/app/listmonk/test.py new file mode 100644 index 0000000..93262e3 --- /dev/null +++ b/app/listmonk/test.py @@ -0,0 +1,58 @@ + + +body = ''' +

weeks might have titles

+ +
+ +
+

Walking in Coronation Park

+

W1RK: Surya at Coronation Park

+
+
+ +
+
+ https://phantas.ma/36/ +
+
+
+
+ +
+

The Dog who walked Back and Forth

+

JhunJhun ShunShun

+
+
+ +
+
+https://phantas.ma/37/ +
+
+
+
+ +
+

Walk through Manori fishers beach

+

Selinas afternoon (2007)

+
+
+ +
+
+https://phantas.ma/38/ +
+
+
+
+ +

Maitreya and Charvaka down Tulsi Pipe

SOT: Monk - Scene 9 (Conv. b/w Maitreya and Charvaka): MNK 9949

+
+
+
https://phantas.ma/39/
+
+
+''' + + diff --git a/app/listmonk/tests.py b/app/listmonk/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/app/listmonk/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/app/listmonk/utils.py b/app/listmonk/utils.py new file mode 100644 index 0000000..97d07cf --- /dev/null +++ b/app/listmonk/utils.py @@ -0,0 +1,47 @@ +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 + diff --git a/app/listmonk/views.py b/app/listmonk/views.py new file mode 100644 index 0000000..249bd51 --- /dev/null +++ b/app/listmonk/views.py @@ -0,0 +1,23 @@ +from django.shortcuts import render +from django.template.loader import render_to_string +from django.utils.timezone import datetime, timedelta + +from ..item import models +from ..item.views import get_weeks + + +def week(year, month, day): + context = {} + _, archive = models.Item.public() + date = datetime.strptime('%s-%s-%s' % (year, month, day), '%Y-%m-%d') + week = int(date.strftime('%W')) + year = int(year) + archive_week = archive.filter(year=year, week=week).order_by('published') + context['weeks'] = get_weeks(archive) + context['this_week'] = date.strftime('%Y-%m-%d') + context['this_year'] = date.strftime('%Y') + extra = models.Week.objects.filter(monday=context['this_week']).first() + if extra: + context['week_title'] = extra.title + context['items'] = archive_week + return render_to_string("weekly_email.html", context) diff --git a/app/settings/common.py b/app/settings/common.py index 4fb9022..9a6fc4b 100644 --- a/app/settings/common.py +++ b/app/settings/common.py @@ -61,6 +61,7 @@ INSTALLED_APPS = [ "app.item", "app.signalbot", "app.telegrambot", + "app.listmonk", "app.page", ] @@ -162,6 +163,11 @@ CELERY_BROKER_URL = "redis://localhost:6379" CELERY_RESULT_BACKEND = 'django-db' SIGNAL_MODERATORS = [] +LISTMONK_USER = "listmonk" +LISTMONK_PASSWORD = "listmonk" +LISTMONK_API = "http://localhost:9000/api/" +LISTMONK_LISTS = [] + RATELIMIT_CACHE_BACKEND = "app.brake_backend.BrakeBackend" diff --git a/app/templates/weekly_email.html b/app/templates/weekly_email.html new file mode 100644 index 0000000..8533d39 --- /dev/null +++ b/app/templates/weekly_email.html @@ -0,0 +1,18 @@ +{% if week_title %} +

{{ week_title }}

+{% endif %} +{% for item in items %} +
+ +
+

{{ item.title }}

+

{{ item.data.title }}

+
{{ item.full_url }}
+
+
+ +
+
+
+{% endfor %} +