56 lines
2.2 KiB
Python
56 lines
2.2 KiB
Python
from background_task import background
|
||
from django.conf import settings
|
||
import logging
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
@background(schedule=0)
|
||
def send_order_notification_background(order_id):
|
||
"""Асинхронная отправка уведомлений о заказе через background-tasks."""
|
||
from .utils.email_notifications import send_order_notification
|
||
from products.models import Order
|
||
try:
|
||
order = Order.objects.select_related('product', 'configuration').get(id=order_id)
|
||
success = send_order_notification(order)
|
||
if success:
|
||
order.notification_sent = True
|
||
order.save(update_fields=['notification_sent'])
|
||
return success
|
||
except Order.DoesNotExist:
|
||
logger.error(f"Order #{order_id} not found")
|
||
return False
|
||
|
||
@background(schedule=0)
|
||
def send_multiple_order_notifications_background(order_ids):
|
||
"""Отправка уведомлений для нескольких заказов."""
|
||
from .utils.email_notifications import send_multiple_order_notifications
|
||
from products.models import Order
|
||
orders = Order.objects.filter(id__in=order_ids)
|
||
return send_multiple_order_notifications(orders)
|
||
|
||
try:
|
||
from celery import shared_task
|
||
except ImportError:
|
||
shared_task = None
|
||
|
||
if shared_task is not None:
|
||
@shared_task
|
||
def send_order_notification_celery(order_id):
|
||
from .utils.email_notifications import send_order_notification
|
||
from products.models import Order
|
||
try:
|
||
order = Order.objects.select_related('product', 'configuration').get(id=order_id)
|
||
success = send_order_notification(order)
|
||
if success:
|
||
order.notification_sent = True
|
||
order.save(update_fields=['notification_sent'])
|
||
return success
|
||
except Order.DoesNotExist:
|
||
return False
|
||
|
||
@shared_task
|
||
def send_multiple_order_notifications_celery(order_ids):
|
||
from .utils.email_notifications import send_multiple_order_notifications
|
||
from products.models import Order
|
||
orders = Order.objects.filter(id__in=order_ids)
|
||
return send_multiple_order_notifications(orders) |