From 5c5153adb6f381d9521afcd96805719f8f4808fa Mon Sep 17 00:00:00 2001 From: Benjamin Bach Date: Sun, 28 Jul 2024 18:55:17 +0200 Subject: [PATCH] Add signals to update Order and Membership --- src/accounting/signals.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/accounting/signals.py b/src/accounting/signals.py index c3bd259..4e90656 100644 --- a/src/accounting/signals.py +++ b/src/accounting/signals.py @@ -4,6 +4,8 @@ from django.conf import settings from django.core.mail import send_mail from django.db.models.signals import post_save from django.dispatch import receiver +from django.utils import timezone +from membership.models import Membership from . import models @@ -19,3 +21,19 @@ def check_total_amount(sender: models.Payment, instance: models.Payment, **kwarg settings.DEFAULT_FROM_EMAIL, settings.ADMINS, ) + + +@receiver(post_save, sender=models.Payment) +def mark_order_paid(sender: models.Payment, instance: models.Payment, **kwargs: dict) -> None: # noqa: ARG001 + """Mark an order as paid when payment is received.""" + instance.order.is_paid = True + instance.order.save() + + +@receiver(post_save, sender=models.Payment) +def activate_membership(sender: models.Order, instance: models.Order, **kwargs: dict) -> None: # noqa: ARG001 + """Mark a membership as activated when its order is marked as paid.""" + if instance.is_paid: + Membership.objects.filter(order=instance, activated=False, activated_on=None).update( + activated=True, activated_on=timezone.now() + )