phantasmobile/app/listmonk/tasks.py

46 lines
1.6 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
2023-12-10 10:06:45 +00:00
from django.core.cache import cache
2023-11-19 13:29:39 +00:00
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()
2023-12-10 10:06:45 +00:00
notified_key = "week-%s-notified" % monday
2023-12-10 10:08:31 +00:00
if not week_obj:
if not cache.get(notified_key):
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)
cache.set(notified_key, True, 24*60*60)
2023-11-19 13:29:39 +00:00
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())