phantasmobile/app/listmonk/tasks.py

42 lines
1.4 KiB
Python
Raw Normal View History

2023-11-19 13:29:39 +00:00
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
2023-11-20 11:14:19 +00:00
from ..item import views
2023-11-19 13:29:39 +00:00
from ..item import models
2023-11-20 11:14:55 +00:00
from ..celery import app
2023-11-19 13:29:39 +00:00
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())