parent
1a181f7d0e
commit
4345e5a8fd
@ -0,0 +1,124 @@ |
||||
from django.db.models.signals import post_save, post_delete |
||||
from django.dispatch import receiver |
||||
from django.core.mail import send_mail |
||||
from django.conf import settings |
||||
from django.urls import reverse |
||||
from .models import Order, OrderItem, OrderStatus |
||||
from django.db import transaction |
||||
|
||||
@receiver([post_save, post_delete], sender=Order) |
||||
def send_order_notification(sender, instance, **kwargs): |
||||
print("send_order_notification") |
||||
""" |
||||
Send an email notification when an order is created, updated, or deleted. |
||||
""" |
||||
# Use transaction.on_commit to ensure database operations are complete |
||||
transaction.on_commit(lambda: _send_order_email(instance, **kwargs)) |
||||
|
||||
def _send_order_email(instance, **kwargs): |
||||
created = kwargs.get('created', False) |
||||
update_fields = kwargs.get('update_fields', None) |
||||
|
||||
# Determine the action type |
||||
if 'signal' in kwargs and kwargs['signal'] == post_delete: |
||||
action = "DELETED" |
||||
return |
||||
elif created: |
||||
action = "CREATED" |
||||
return |
||||
else: |
||||
action = "UPDATED" |
||||
print('updated', update_fields) |
||||
if update_fields and 'status' in update_fields: |
||||
action = "UPDATED" |
||||
|
||||
# Get order details |
||||
order_id = instance.id |
||||
status = instance.status |
||||
|
||||
print("status", status) |
||||
if status == OrderStatus.PENDING: |
||||
return |
||||
|
||||
total_price = instance.total_price |
||||
|
||||
# Generate admin URL |
||||
admin_url = f"{settings.SITE_URL}{reverse('admin:shop_order_change', args=[order_id])}" |
||||
|
||||
# Get customer info |
||||
if instance.user: |
||||
customer_info = f"Utilisateur: {instance.user.email}" |
||||
elif instance.guest_user: |
||||
customer_info = f"Invité: {instance.guest_user.email} ({instance.guest_user.phone})" |
||||
else: |
||||
customer_info = "Client inconnu" |
||||
|
||||
# Construire les détails des articles - obtenir une requête fraîche des articles de la commande |
||||
items_list = "" |
||||
if action != "DELETED": |
||||
# Utiliser une requête fraîche pour s'assurer d'avoir les données les plus récentes |
||||
order_items = OrderItem.objects.filter(order_id=order_id).select_related('product', 'color', 'size') |
||||
for item in order_items: |
||||
color = item.color.name if item.color else "N/A" |
||||
size = item.size.name if item.size else "N/A" |
||||
item_line = f"- {item.quantity}x {item.product.title} (Couleur: {color}, Taille: {size}, Prix: {item.price}€)\n" |
||||
items_list += item_line |
||||
|
||||
# Composer l'email |
||||
if action == "CREATED": |
||||
action_fr = "CRÉÉE" |
||||
elif action == "UPDATED": |
||||
action_fr = "MISE À JOUR" |
||||
elif action == "DELETED": |
||||
action_fr = "SUPPRIMÉE" |
||||
else: |
||||
action_fr = action |
||||
|
||||
# Traduire le statut actuel |
||||
status_fr_map = { |
||||
"PENDING": "EN ATTENTE", |
||||
"PAID": "PAYÉE", |
||||
"SHIPPED": "EXPÉDIÉE", |
||||
"DELIVERED": "LIVRÉE", |
||||
"CANCELED": "ANNULÉE" |
||||
} |
||||
status_fr = status_fr_map.get(status, status) |
||||
|
||||
# Traduire le statut de paiement |
||||
payment_status_fr_map = { |
||||
"UNPAID": "NON PAYÉE", |
||||
"PAID": "PAYÉE", |
||||
"FAILED": "ÉCHOUÉE" |
||||
} |
||||
payment_status_fr = payment_status_fr_map.get(instance.payment_status, instance.payment_status) |
||||
|
||||
subject = f"Commande #{order_id} {action_fr}: {status_fr}" |
||||
message = f""" |
||||
La commande #{order_id} a été {action_fr.lower()} |
||||
|
||||
Statut: {status_fr} |
||||
Statut de paiement: {payment_status_fr} |
||||
Prix total: {total_price}€ |
||||
|
||||
{customer_info} |
||||
|
||||
Articles: |
||||
{items_list} |
||||
|
||||
Voir la commande dans le panneau d'administration: {admin_url} |
||||
|
||||
Ceci est un message automatique. Merci de ne pas répondre. |
||||
""" |
||||
|
||||
# Send email |
||||
recipient_list = [email for name, email in settings.MANAGERS] |
||||
if not recipient_list: |
||||
recipient_list = [settings.DEFAULT_FROM_EMAIL] |
||||
|
||||
send_mail( |
||||
subject=subject, |
||||
message=message, |
||||
from_email=settings.DEFAULT_FROM_EMAIL, |
||||
recipient_list=recipient_list, |
||||
fail_silently=False, |
||||
) |
||||
Loading…
Reference in new issue