diff options
Diffstat (limited to 'restaurant_orders/core/tasks.py')
| -rw-r--r-- | restaurant_orders/core/tasks.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/restaurant_orders/core/tasks.py b/restaurant_orders/core/tasks.py new file mode 100644 index 0000000..f1c0192 --- /dev/null +++ b/restaurant_orders/core/tasks.py | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | from celery import shared_task | ||
| 2 | |||
| 3 | from django.core import serializers | ||
| 4 | |||
| 5 | from restaurant_orders.consumers import NotificationsConsumer | ||
| 6 | |||
| 7 | from core.utils import Orders, SendMail, SendSms | ||
| 8 | import re | ||
| 9 | |||
| 10 | |||
| 11 | def send_notification(is_success, message, user_pk): | ||
| 12 | status = NotificationsConsumer.OK if is_success else NotificationsConsumer.ERROR | ||
| 13 | NotificationsConsumer.send_notifications( | ||
| 14 | user_pk, | ||
| 15 | status, | ||
| 16 | message | ||
| 17 | ) | ||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | @shared_task | ||
| 22 | def create_order_and_send_notification(order, items, is_email=None, is_sms=None, user_pk=None): | ||
| 23 | order = [obj for obj in serializers.deserialize('json', order)] | ||
| 24 | if len(order) != 1: | ||
| 25 | return | ||
| 26 | order = order[0].object | ||
| 27 | |||
| 28 | phone = order.billing.get('phone') | ||
| 29 | email = order.billing.get('email') | ||
| 30 | new_order = Orders(order.restaurant, order.billing).create_custom_order(items) | ||
| 31 | |||
| 32 | |||
| 33 | # if new_order is None: | ||
| 34 | # send_notification(False, | ||
| 35 | # "Niestety nie udalo sie skontaktowac z restauracja, prosze sprowbowac ponownie pozniej.", | ||
| 36 | # user_pk) | ||
| 37 | # return | ||
| 38 | |||
| 39 | EMAIL_REGEX = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' | ||
| 40 | |||
| 41 | if is_sms: # TODO: Make regex for sms | ||
| 42 | sms = SendSms(new_order).send() | ||
| 43 | send_notification(*sms, user_pk) | ||
| 44 | |||
| 45 | if is_email and re.fullmatch(EMAIL_REGEX, str(email)): | ||
| 46 | mail = SendMail(new_order).send() | ||
| 47 | send_notification(*mail, user_pk) | ||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | |||
