diff --git a/padelclub_backend/settings.py b/padelclub_backend/settings.py index ed2fe42..5bb9987 100644 --- a/padelclub_backend/settings.py +++ b/padelclub_backend/settings.py @@ -159,11 +159,7 @@ AUTHENTICATION_BACKENDS = [ ] CSRF_COOKIE_SECURE = True # if using HTTPS -if DEBUG: # Development environment - SESSION_COOKIE_SECURE = False -else: # Production environment - SESSION_COOKIE_SECURE = True - +SESSION_COOKIE_SECURE = True LOGGING = { 'version': 1, diff --git a/shop/signals.py b/shop/signals.py index 025c7a5..f449550 100644 --- a/shop/signals.py +++ b/shop/signals.py @@ -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()} diff --git a/tournaments/admin.py b/tournaments/admin.py index 1248c10..e06bb99 100644 --- a/tournaments/admin.py +++ b/tournaments/admin.py @@ -13,7 +13,7 @@ class CustomUserAdmin(UserAdmin): form = CustomUserChangeForm add_form = CustomUserCreationForm model = CustomUser - search_fields = ('username', 'email', 'phone', 'first_name', 'last_name', 'licence_id') + search_fields = ['username', 'email', 'phone', 'first_name', 'last_name', 'licence_id'] list_display = ['email', 'first_name', 'last_name', 'username', 'licence_id', 'date_joined', 'latest_event_club_name', 'is_active', 'event_count', 'origin'] list_filter = ['is_active', 'origin'] @@ -83,7 +83,7 @@ class RoundAdmin(SyncedObjectAdmin): class PlayerRegistrationAdmin(SyncedObjectAdmin): list_display = ['first_name', 'last_name', 'licence_id', 'rank'] - search_fields = ('id', 'first_name', 'last_name', 'licence_id__icontains') + search_fields = ['id', 'first_name', 'last_name', 'licence_id__icontains'] list_filter = ['registered_online', TeamScoreTournamentListFilter] ordering = ['last_name', 'first_name'] raw_id_fields = ['team_registration'] # Add this line @@ -111,9 +111,9 @@ class GroupStageAdmin(SyncedObjectAdmin): class ClubAdmin(SyncedObjectAdmin): list_display = ['name', 'acronym', 'city', 'creator', 'events_count', 'broadcast_code'] - search_fields = ('name', 'acronym', 'city') + search_fields = ['name', 'acronym', 'city'] ordering = ['creator'] - raw_id_fields = ['creator'] + raw_id_fields = ['creator', 'related_user'] class PurchaseAdmin(SyncedObjectAdmin): list_display = ['id', 'user', 'product_id', 'quantity', 'purchase_date', 'revocation_date', 'expiration_date'] @@ -150,7 +150,7 @@ class UnregisteredTeamAdmin(admin.ModelAdmin): class UnregisteredPlayerAdmin(admin.ModelAdmin): list_display = ['first_name', 'last_name', 'licence_id'] - search_fields = ('first_name', 'last_name') + search_fields = ['first_name', 'last_name'] list_filter = [] ordering = ['last_name', 'first_name']