From 09df95fe6479f238c2d410663c3dba1c65b674f4 Mon Sep 17 00:00:00 2001 From: Parsa Nazer Date: Sat, 29 Mar 2025 13:15:05 +0330 Subject: [PATCH] shared task for send notif and sms --- backend/order/models.py | 2 ++ backend/order/signals.py | 38 +++++++++++--------------------------- backend/order/tasks.py | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/backend/order/models.py b/backend/order/models.py index 8d7ba6a..675bd07 100644 --- a/backend/order/models.py +++ b/backend/order/models.py @@ -66,9 +66,11 @@ class OrderModel(models.Model): def save(self, *args, **kwargs): + # genrate order id if not self.pk: last_instance = self.__class__.objects.order_by("pk").last() self.order_id = (last_instance.pk + 1001) if last_instance else 1001 + super().save(*args, **kwargs) diff --git a/backend/order/signals.py b/backend/order/signals.py index 63cacc1..793f46e 100644 --- a/backend/order/signals.py +++ b/backend/order/signals.py @@ -3,6 +3,8 @@ from django.dispatch import receiver from .models import OrderModel from account.models import PushSubscription import ghasedak_sms +from .tasks import send_change_status_notif, send_change_status_sms + @receiver(pre_save, sender=OrderModel) def order_status_changed(sender, instance, **kwargs): @@ -10,36 +12,18 @@ def order_status_changed(sender, instance, **kwargs): previous = OrderModel.objects.get(pk=instance.pk) if previous.status != instance.status: - send_change_status_notif(instance) - send_change_status_sms(instance) + new_status = instance.get_status_display() + send_change_status_notif.delay(instance.pk, new_status) + send_change_status_sms.delay(instance.pk, new_status) + + if previous.status == 'CART' and instance.status == 'ADMIN_PENDING': + # update_cart_price_fields() + # update_sell_data() + # update_quantity() + pass -def send_change_status_notif(instance): - user_subs = PushSubscription.objects.filter(user=instance.user) - for user_sub in user_subs: - try: - user_sub.send_notif(f'سفارش شما به {instance.get_status_display()} تغییر کرد', f'سفارش شما به {instance.get_status_display()} تغییر کرد', ProductImageModel.objects.all().first().image.url) - except: - print('log later send notif error') - - -def send_change_status_sms(instance): - sms_api = ghasedak_sms.Ghasedak(api_key="1227eaaddcba72bcb0169b37032cf16ae9ac6ed8b3b7c2768b74e2ee351d1b52gyRe3AGomZRPTNEd") - - - response = sms_api.send_single_sms( - ghasedak_sms.SendSingleSmsInput( - message=f'سفارش شما به {instance.get_status_display()} تغییر کرد', - receptor=instance.user.phone, - line_number='30005006004095', - client_reference_id=str(instance.user.pk) - ) - ) - if response['statusCode'] == 200: - print('done log later') - else: - print(f'error: {response}') def update_cart_price_fields(order): diff --git a/backend/order/tasks.py b/backend/order/tasks.py index d482965..1a87f1e 100644 --- a/backend/order/tasks.py +++ b/backend/order/tasks.py @@ -4,7 +4,9 @@ from azbankgateways import ( models as bank_models, default_settings as settings, ) - +from .models import OrderModel +from account.models import PushSubscription +import ghasedak_sms from celery import shared_task @@ -23,4 +25,34 @@ def udpate_bank_status(): if bank_record.is_success: logging.debug("This record is verify now.", extra={"pk": bank_record.pk}) - return 'update bank record is done' \ No newline at end of file + return 'update bank record is done' + + +@shared_task +def send_change_status_notif(instance_pk, new_status): + instance = OrderModel.objects.get(pk=instance_pk) + user_subs = PushSubscription.objects.filter(user=instance.user) + for user_sub in user_subs: + try: + user_sub.send_notif(f'سفارش شما به {new_status} تغییر کرد', f'سفارش شما به {new_status} تغییر کرد', ProductImageModel.objects.all().first().image.url) + except: + print('log later send notif error') + +@shared_task +def send_change_status_sms(instance_pk, new_status): + instance = OrderModel.objects.get(pk=instance_pk) + sms_api = ghasedak_sms.Ghasedak(api_key="1227eaaddcba72bcb0169b37032cf16ae9ac6ed8b3b7c2768b74e2ee351d1b52gyRe3AGomZRPTNEd") + + + response = sms_api.send_single_sms( + ghasedak_sms.SendSingleSmsInput( + message=f'سفارش شما به {new_status} تغییر کرد', + receptor=instance.user.phone, + line_number='30005006004095', + client_reference_id=str(instance.user.pk) + ) + ) + if response['statusCode'] == 200: + return 'done log later' + else: + return f'error: {response}' \ No newline at end of file