email backend

This commit is contained in:
j 2023-09-23 14:38:28 +02:00
parent 3faf2c2493
commit e82d5d5f15
11 changed files with 167 additions and 0 deletions

0
app/listmonk/__init__.py Normal file
View file

3
app/listmonk/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
app/listmonk/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ListmonkConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "app.listmonk"

View file

3
app/listmonk/models.py Normal file
View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

58
app/listmonk/test.py Normal file
View file

@ -0,0 +1,58 @@
body = '''
<h1>weeks might have titles</h1>
<div class="item">
<a href="https://phantas.ma/36/">
<div class="info">
<h1>Walking in Coronation Park</h1>
<h2>W1RK: Surya at Coronation Park</h2>
</div>
<div class="image">
<img src=https://pad.ma/IEI/480p243.133.jpg>
</div>
<div class="url">
https://phantas.ma/36/
</div>
</a>
</div>
<div class="item">
<a href="https://phantas.ma/37/">
<div class="info">
<h1>The Dog who walked Back and Forth</h1>
<h2>JhunJhun ShunShun</h2>
</div>
<div class="image">
<img src=https://pad.ma/JPQ/480p149.61599999999999.jpg>
</div>
<div class="url">
https://phantas.ma/37/
</div>
</a>
</div>
<div class="item">
<a href="https://phantas.ma/38/">
<div class="info">
<h1>Walk through Manori fishers beach</h1>
<h2>Selinas afternoon (2007)</h2>
</div>
<div class="image">
<img src=https://pad.ma/AG/480p390.566.jpg>
</div>
<div class="url">
https://phantas.ma/38/
</div>
</a>
</div>
<div class="item">
<a href="https://phantas.ma/39/">
<div class="info"><h1>Maitreya and Charvaka down Tulsi Pipe</h1><h2>SOT: Monk - Scene 9 (Conv. b/w Maitreya and Charvaka): MNK 9949</h2>
</div>
<div class="image"><img src=https://pad.ma/GCC/480p110.289.jpg></div>
<div class="url">https://phantas.ma/39/</div>
</a>
</div>
'''

3
app/listmonk/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

47
app/listmonk/utils.py Normal file
View file

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

23
app/listmonk/views.py Normal file
View file

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

View file

@ -61,6 +61,7 @@ INSTALLED_APPS = [
"app.item", "app.item",
"app.signalbot", "app.signalbot",
"app.telegrambot", "app.telegrambot",
"app.listmonk",
"app.page", "app.page",
] ]
@ -162,6 +163,11 @@ CELERY_BROKER_URL = "redis://localhost:6379"
CELERY_RESULT_BACKEND = 'django-db' CELERY_RESULT_BACKEND = 'django-db'
SIGNAL_MODERATORS = [] SIGNAL_MODERATORS = []
LISTMONK_USER = "listmonk"
LISTMONK_PASSWORD = "listmonk"
LISTMONK_API = "http://localhost:9000/api/"
LISTMONK_LISTS = []
RATELIMIT_CACHE_BACKEND = "app.brake_backend.BrakeBackend" RATELIMIT_CACHE_BACKEND = "app.brake_backend.BrakeBackend"

View file

@ -0,0 +1,18 @@
{% if week_title %}
<h1>{{ week_title }}</h1>
{% endif %}
{% for item in items %}
<div class="item">
<a href="{{ item.full_url }}">
<div class="info">
<h1>{{ item.title }}</h1>
<h2>{{ item.data.title }}</h2>
<div class="url">{{ item.full_url }}</div>
</div>
<div class="image">
<img src={{ item.data.thumbnail }}>
</div>
</a>
</div>
{% endfor %}