rcnn/backend/apps/challenges/tasks.py
Marius Unsel d93412cd0d Initial commit
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-07 01:12:40 +02:00

35 lines
1.2 KiB
Python

import logging
from celery import shared_task
logger = logging.getLogger(__name__)
@shared_task(queue="default")
def notify_challenge_expiring(challenge_id: str):
"""
Send an FCM notification to all participants of a challenge that is
expiring soon. Scheduled by celery-beat.
"""
from apps.challenges.models import Challenge, ChallengeParticipant
from apps.utils.fcm import send_notification
try:
challenge = Challenge.objects.get(pk=challenge_id, status=Challenge.Status.ACTIVE)
except Challenge.DoesNotExist:
return
participants = ChallengeParticipant.objects.filter(
challenge=challenge,
submitted_splat__isnull=True, # only those who haven't submitted yet
).select_related("user")
for participant in participants:
send_notification(
participant.user.fcm_token,
title="Challenge expiring soon!",
body=f'"{challenge.title}" is closing soon. Don\'t miss your chance!',
data={"challenge_id": str(challenge.id), "type": "challenge_expiring"},
)
logger.info("Sent expiry notifications for challenge %s to %d participants", challenge_id, participants.count())