Changes to payment models #32

Merged
valberg merged 35 commits from benjaoming/membersystem:payment-updates into main 2024-08-03 17:55:33 +00:00
Showing only changes of commit 5c5153adb6 - Show all commits

View file

@ -4,6 +4,8 @@ from django.conf import settings
from django.core.mail import send_mail from django.core.mail import send_mail
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
from django.utils import timezone
from membership.models import Membership
from . import models from . import models
@ -19,3 +21,19 @@ def check_total_amount(sender: models.Payment, instance: models.Payment, **kwarg
settings.DEFAULT_FROM_EMAIL, settings.DEFAULT_FROM_EMAIL,
settings.ADMINS, 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()
)