import logging import os import tempfile from celery.schedules import crontab from django.conf import settings from django.utils import timezone from django.core.cache import cache from . import utils from ..signalbot import rpc from ..item import views from ..item import models from ..celery import app 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() notified_key = "week-%s-notified" % monday 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) 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())