Coverage for memberships/signals.py: 70%
25 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-06-07 11:59 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-06-07 11:59 -0600
1from django.core.files.base import ContentFile
2from django.utils import timezone
3from django.dispatch import receiver
4from django.db.models.signals import post_save
5from .models import MembershipEmail, Affiliation, MembershipPeriod, MembershipExport, Membership
6from .resources import MembershipResource
9@receiver(post_save, sender=Affiliation)
10def create_membership_email(sender, instance, created, **kwargs):
11 """
12 Create membership email for user in affiliation
13 """
14 if created:
15 MembershipEmail.objects.create(
16 membership=instance.membership,
17 affiliation=instance,
18 email=instance.user.email,
19 )
22@receiver(post_save, sender=MembershipPeriod)
23def set_membership_active_period(sender, instance, created, **kwargs):
24 """
25 Set membership active_period
26 """
27 if created: 27 ↛ exitline 27 didn't return from function 'set_membership_active_period', because the condition on line 27 was never false
28 now = timezone.datetime.today().date()
29 if instance.period_start <= now and instance.period_end >= now: 29 ↛ exitline 29 didn't return from function 'set_membership_active_period', because the condition on line 29 was never false
30 instance.membership.active_period = instance
31 instance.membership.save()
34@receiver(post_save, sender=MembershipExport)
35def save_affiliation_export_file(sender, instance, created, *args, **kwargs):
36 if created:
37 dataset = MembershipResource().export(
38 queryset=Membership.objects.filter(
39 organization=instance.organization,
40 created_at__date__gte=instance.from_date,
41 created_at__date__lte=instance.to_date,
42 )
43 )
45 from_date_str = instance.from_date.strftime("%Y-%m-%d")
46 to_date_str = instance.to_date.strftime("%Y-%m-%d")
48 csv_file = ContentFile(dataset.export("csv"))
50 instance.export_file.save(f"Membership_{from_date_str}_{to_date_str}.csv", csv_file)