automate weekly newsletter

This commit is contained in:
j 2023-11-19 14:29:39 +01:00
parent 629491eeed
commit 3345b5b887
4 changed files with 71 additions and 2 deletions

View file

@ -0,0 +1,29 @@
# Generated by Django 4.2.3 on 2023-11-19 13:25
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("item", "0006_week"),
]
operations = [
migrations.AddField(
model_name="week",
name="published",
field=models.DateTimeField(
blank=True, default=None, editable=False, null=True
),
),
migrations.AlterField(
model_name="item",
name="published",
field=models.DateTimeField(blank=True, default=None, null=True),
),
migrations.AlterField(
model_name="week",
name="title",
field=models.CharField(max_length=2048),
),
]

View file

@ -187,6 +187,7 @@ class Week(models.Model):
modified = models.DateTimeField(auto_now=True) modified = models.DateTimeField(auto_now=True)
monday = models.DateField(unique=True) monday = models.DateField(unique=True)
title = models.CharField(max_length=2048) title = models.CharField(max_length=2048)
published = models.DateTimeField(null=True, default=None, blank=True, editable=False)
def __str__(self): def __str__(self):
return "%s (%s)" % (self.title, self.monday) return "%s (%s)" % (self.title, self.monday)

40
app/listmonk/tasks.py Normal file
View file

@ -0,0 +1,40 @@
import logging
import os
import tempfile
from celery.schedules import crontab
from django.conf import settings
from django.utils import timezone
from . import utils
from ..signalbot import rpc
from ..item import view
from ..item import models
logger = logging.getLogger(__name__)
@app.task(queue="default", ignore_results=True)
def send_week():
now = views.get_now()
if now.hour < 15:
return
monday = views.get_monday(now)
week, archive = models.Item.public(now)
week_obj = models.Week.objects.filter(monday=monday).first()
if not week_obj:
message = "no title for week, can not send out email please add: %s/admin/item/week/add/?monday=%s" % (settings.URL, monday)
r = rpc.send(message, group=settings.SIGNAL_MODERATORS_GROUP)
elif week.filter(announced=None).exists():
logger.error("waiting for last item of the week to be public")
elif not week_obj.published:
monday = views.get_monday(now)
week_obj.published = now
week_obj.save()
if not utils.send_week(monday):
message = "weekly newsletter ready, confirm at https://list.phantas.ma/admin/campaigns"
r = rpc.send(message, group=settings.SIGNAL_MODERATORS_GROUP)
@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(crontab(day_of_week=0, minute="*/30"), send_week.s())

View file

@ -13,9 +13,8 @@ headers = {
} }
def send_week(year, month, day): def send_week(date):
from . import views from . import views
date = '%s-%02d-%02d' % (year, month, day)
date = datetime.strptime(date, '%Y-%m-%d') date = datetime.strptime(date, '%Y-%m-%d')
monday = views.get_monday(date) monday = views.get_monday(date)
body = views.week(year, month, day) body = views.week(year, month, day)