add celery
This commit is contained in:
parent
ff3e5d3bed
commit
a5f1e1340a
3 changed files with 41 additions and 5 deletions
18
apps/push_notifications/migrations/0002_pushmessage_stats.py
Normal file
18
apps/push_notifications/migrations/0002_pushmessage_stats.py
Normal file
|
|
@ -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),
|
||||
),
|
||||
]
|
||||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -1,5 +1,15 @@
|
|||
from main import celery_app
|
||||
from utils.clients import gotify
|
||||
|
||||
|
||||
@celery_app.task
|
||||
def send_push_notification(uuid):
|
||||
print(uuid)
|
||||
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()
|
||||
Loading…
Add table
Reference in a new issue