Coverage for payments/signals.py: 76%
29 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-05-16 12:38 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-05-16 12:38 -0600
1from django.core.files.base import ContentFile
2from django.dispatch import receiver
3from django.db.models import signals, Sum
4from .models import PaymentIntent, ManualPayment, PaymentAllocation, PaymentAllocationExport
5from .resources import PaymentAllocationResource
8@receiver(signals.post_save, sender=PaymentAllocation)
9def update_payment_and_product_charge_on_payment_allocation(sender, instance, created, *args, **kwargs):
10 if created: 10 ↛ exitline 10 didn't return from function 'update_payment_and_product_charge_on_payment_allocation', because the condition on line 10 was never false
11 instance.payment.save()
12 instance.product_charge.save()
15@receiver(signals.pre_save, sender=PaymentIntent)
16@receiver(signals.pre_save, sender=ManualPayment)
17def update_payment_allocated_amount(sender, instance, *args, **kwargs):
18 allocated_amount = (
19 instance.payment_allocations.aggregate(allocated_amount=Sum("amount")).get("allocated_amount") or 0
20 )
21 instance.allocated_amount = allocated_amount
24@receiver(signals.post_save, sender=PaymentIntent)
25@receiver(signals.post_save, sender=ManualPayment)
26def set_concept(sender, instance, created, *args, **kwargs):
27 if created:
28 if instance.concept is None:
29 instance.concept = f"Pago de {instance.membership}"
32@receiver(signals.post_save, sender=PaymentAllocationExport)
33def save_payment_allocation_export_file(sender, instance, created, *args, **kwargs):
34 if created:
35 dataset = PaymentAllocationResource().export(
36 queryset=PaymentAllocation.objects.filter(
37 payment__organization=instance.organization,
38 created_at__date__gte=instance.from_date,
39 created_at__date__lte=instance.to_date,
40 )
41 )
43 from_date_str = instance.from_date.strftime("%Y-%m-%d")
44 to_date_str = instance.to_date.strftime("%Y-%m-%d")
46 csv_file = ContentFile(dataset.export("csv"))
48 instance.export_file.save(f"PaymentAllocation_{from_date_str}_{to_date_str}.csv", csv_file)