|
|
|
|
@ -1,4 +1,4 @@ |
|
|
|
|
from django.db.models.signals import post_save, post_delete |
|
|
|
|
from django.db.models.signals import pre_save, post_delete |
|
|
|
|
from django.dispatch import receiver |
|
|
|
|
from django.core.mail import send_mail |
|
|
|
|
from django.conf import settings |
|
|
|
|
@ -8,18 +8,38 @@ from django.db import transaction |
|
|
|
|
from django.contrib.auth.signals import user_logged_in |
|
|
|
|
from .cart import transfer_cart |
|
|
|
|
|
|
|
|
|
@receiver([post_save, post_delete], sender=Order) |
|
|
|
|
@receiver([pre_save, post_delete], sender=Order) |
|
|
|
|
def send_order_notification(sender, instance, **kwargs): |
|
|
|
|
"""Send an email notification when an order is created, updated, or deleted.""" |
|
|
|
|
transaction.on_commit(lambda: _send_order_email(instance, **kwargs)) |
|
|
|
|
# For pre_save, we need to check if the instance exists in the database |
|
|
|
|
if kwargs.get('signal', None) == pre_save: |
|
|
|
|
try: |
|
|
|
|
# Get the current instance from the database |
|
|
|
|
old_instance = Order.objects.get(pk=instance.pk) |
|
|
|
|
# Only send notification if status has changed |
|
|
|
|
if old_instance.status != instance.status: |
|
|
|
|
# Execute on commit to ensure DB consistency |
|
|
|
|
transaction.on_commit(lambda: _send_order_email(instance, old_status=old_instance.status, **kwargs)) |
|
|
|
|
except Order.DoesNotExist: |
|
|
|
|
# This is a new instance (creation) |
|
|
|
|
# You might want to handle creation differently or just pass |
|
|
|
|
pass |
|
|
|
|
else: |
|
|
|
|
# Handle post_delete |
|
|
|
|
transaction.on_commit(lambda: _send_order_email(instance, **kwargs)) |
|
|
|
|
|
|
|
|
|
def _send_order_email(instance, **kwargs): |
|
|
|
|
def _send_order_email(instance, old_status=None, **kwargs): |
|
|
|
|
# Skip processing for PENDING orders |
|
|
|
|
if instance.status == OrderStatus.PENDING: |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
# Determine action type |
|
|
|
|
action = _determine_action_type(kwargs) |
|
|
|
|
if 'signal' in kwargs and kwargs['signal'] == post_delete: |
|
|
|
|
action = "DELETED" |
|
|
|
|
elif old_status is None: |
|
|
|
|
action = "CREATED" |
|
|
|
|
else: |
|
|
|
|
action = "UPDATED" |
|
|
|
|
if action in ["DELETED", "CREATED"]: |
|
|
|
|
return # No emails for these actions |
|
|
|
|
|
|
|
|
|
@ -34,15 +54,6 @@ def _send_order_email(instance, **kwargs): |
|
|
|
|
if order_details['customer_email']: |
|
|
|
|
_send_customer_notification(instance, order_details, items_list) |
|
|
|
|
|
|
|
|
|
def _determine_action_type(kwargs): |
|
|
|
|
"""Determine the action type from signal kwargs.""" |
|
|
|
|
if 'signal' in kwargs and kwargs['signal'] == post_delete: |
|
|
|
|
return "DELETED" |
|
|
|
|
elif kwargs.get('created', False): |
|
|
|
|
return "CREATED" |
|
|
|
|
else: |
|
|
|
|
return "UPDATED" |
|
|
|
|
|
|
|
|
|
def _get_order_details(instance): |
|
|
|
|
"""Extract and build order details dictionary.""" |
|
|
|
|
# Get customer info |
|
|
|
|
@ -122,14 +133,17 @@ def _send_internal_notification(instance, action, order_details, items_list): |
|
|
|
|
|
|
|
|
|
# Build price information with coupon details if applicable |
|
|
|
|
price_info = f"Prix total: {order_details['total_price']}€" |
|
|
|
|
server = "" |
|
|
|
|
if settings.DEBUG: |
|
|
|
|
server = "DEBUG: " |
|
|
|
|
|
|
|
|
|
if order_details['has_coupon']: |
|
|
|
|
price_info = f""" |
|
|
|
|
Prix total: {order_details['total_price']}€ |
|
|
|
|
{order_details['coupon_info']} |
|
|
|
|
Réduction: -{order_details['discount_amount']}€ |
|
|
|
|
Montant payé: {order_details['final_price']}€""" |
|
|
|
|
|
|
|
|
|
subject = f"Commande #{order_details['order_id']} {action_fr}: {order_details['status_fr']}" |
|
|
|
|
subject = f"{server}Commande #{order_details['order_id']} {action_fr}: {order_details['status_fr']}" |
|
|
|
|
message = f""" |
|
|
|
|
La commande #{order_details['order_id']} a été {action_fr.lower()} |
|
|
|
|
|
|
|
|
|
|