add celery task to send shift reminders

pull/1/head
parent 0da9d981c8
commit 3b0c0f7c9c

@ -88,13 +88,6 @@ class ShiftRegistration(models.Model):
self.reminder_sent = True self.reminder_sent = True
self.save() self.save()
def get_unnotified_registrations():
return ShiftRegistration.objects.filter(
reminder_sent=False,
shift__start_at__lte=timezone.now()
+ global_preferences["helper__reminder_time"],
)
def __str__(self): def __str__(self):
return f"{self.helper.name}: {self.shift}" return f"{self.helper.name}: {self.shift}"

@ -1,5 +1,5 @@
from celery import shared_task from celery import shared_task
from .models import Message from .models import Message, ShiftRegistration
from django.db import transaction from django.db import transaction
from django.utils import timezone from django.utils import timezone
from dynamic_preferences.registries import global_preferences_registry from dynamic_preferences.registries import global_preferences_registry
@ -36,3 +36,14 @@ def send_message(msgid):
print(f"TODO: send message @{msg.to.phone} {msg.text}") print(f"TODO: send message @{msg.to.phone} {msg.text}")
msg.sent_at = timezone.now() msg.sent_at = timezone.now()
msg.save() msg.save()
@shared_task
def send_reminders():
with transaction.atomic():
for reg in ShiftRegistration.objects.select_for_update().filter(
reminder_sent=False,
shift__start_at__lte=timezone.now()
+ global_preferences["helper__reminder_time"],
):
reg.send_reminder()

@ -154,6 +154,14 @@ CELERY_BEAT_SCHEDULE = {
"task": "shiftregister.importer.tasks.import_shifts", "task": "shiftregister.importer.tasks.import_shifts",
"schedule": float(getenv("SHIFT_IMPORT_INTERVAL", 60.0)), # seconds "schedule": float(getenv("SHIFT_IMPORT_INTERVAL", 60.0)), # seconds
}, },
"send-messages-every-120-seconds": {
"task": "shiftregister.app.tasks.send_messages",
"schedule": float(getenv("SHIFT_IMPORT_INTERVAL", 120.0)), # seconds
},
"send-reminders-every-300-seconds": {
"task": "shiftregister.app.tasks.send_reminders",
"schedule": float(getenv("SHIFT_IMPORT_INTERVAL", 300.0)), # seconds
},
} }
CELERY_BEAT_SCHEDULE_FILENAME = str(BASE_DIR / "storage" / "celerybeat-schedule") CELERY_BEAT_SCHEDULE_FILENAME = str(BASE_DIR / "storage" / "celerybeat-schedule")

Loading…
Cancel
Save