From a5f1e1340ac3c397959c20f4fb1d97f7450e1421 Mon Sep 17 00:00:00 2001 From: Sayyid Hamid Mahdavi Date: Thu, 17 Jul 2025 12:03:18 +0330 Subject: [PATCH] add celery --- .../migrations/0002_pushmessage_stats.py | 18 ++++++++++++++++++ apps/push_notifications/models.py | 14 +++++++++++--- apps/push_notifications/tasks.py | 14 ++++++++++++-- 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 apps/push_notifications/migrations/0002_pushmessage_stats.py diff --git a/apps/push_notifications/migrations/0002_pushmessage_stats.py b/apps/push_notifications/migrations/0002_pushmessage_stats.py new file mode 100644 index 0000000..80d3361 --- /dev/null +++ b/apps/push_notifications/migrations/0002_pushmessage_stats.py @@ -0,0 +1,18 @@ +# Generated by Django 5.2.3 on 2025-07-17 08:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('push_notifications', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='pushmessage', + name='stats', + field=models.IntegerField(choices=[(0, 'init'), (1, 'Start'), (2, 'done'), (3, 'timeout'), (4, 'failed')], default=0), + ), + ] diff --git a/apps/push_notifications/models.py b/apps/push_notifications/models.py index 75ca4be..fae3d2f 100644 --- a/apps/push_notifications/models.py +++ b/apps/push_notifications/models.py @@ -4,6 +4,7 @@ from oauth2_provider.settings import oauth2_settings from apps.users.models import User from utils.models import BaseModel from utils.clients import gotify +from django.utils.translation import gettext_lazy as _ class PushUserQuerySet(models.QuerySet): @@ -44,6 +45,13 @@ class PushUser(BaseModel): class PushMessage(BaseModel): + class StateChoices(models.IntegerChoices): + INIT = 0, _('init') + START = 1, _('Start') + DONE = 2, _('done') + TIMEOUT = 3, _('timeout') + FAILED = 4, _('failed') + push_user = models.ForeignKey(PushUser, on_delete=models.PROTECT) application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL, on_delete=models.PROTECT, related_name='+', null=True, blank=True) @@ -51,8 +59,8 @@ class PushMessage(BaseModel): message = models.TextField() priority = models.IntegerField(default=5) extras = models.JSONField(default=dict) + stats = models.IntegerField(default=StateChoices.INIT, choices=StateChoices.choices) def send_push(self): - gotify.send_notif(token=self.push_user.application_token ,title=self.title, message=self.message, priority=self.priority, extras=self.extras) - - + from apps.push_notifications.tasks import send_push_notification + send_push_notification.delay(str(self.uuid)) diff --git a/apps/push_notifications/tasks.py b/apps/push_notifications/tasks.py index ab0750f..fa34663 100644 --- a/apps/push_notifications/tasks.py +++ b/apps/push_notifications/tasks.py @@ -1,5 +1,15 @@ from main import celery_app +from utils.clients import gotify + @celery_app.task -def send_push_notification(uuid): - print(uuid) \ No newline at end of file +def send_push_notification(push_message_uuid): + from .models import PushMessage + push_message = PushMessage.objects.get(uuid=push_message_uuid) + gotify.send_notif(token=push_message.push_user.application_token , + title=push_message.title, + message=push_message.message, + priority=push_message.priority, + extras=push_message.extras) + push_message.state = push_message.StateChoices.DONE + push_message.save() \ No newline at end of file