Coverage for apps/organizations/signals.py: 38%
30 statements
« prev ^ index » next coverage.py v6.4.4, created at 2024-05-10 10:18 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2024-05-10 10:18 -0600
1from django.db.models.signals import post_save, pre_save
2from django.dispatch import receiver
4from apps.catalogues.models import PaymentMethod, RoleType
5from apps.payments.models import OrganizationPaymentMethod
6from apps.practitioners.models import Practitioner
8from .models import Affiliation, Organization, OrganizationRole
11@receiver(pre_save, sender=OrganizationRole)
12def set_name_of_organization_role(sender, instance, **kwargs):
13 instance.name = instance.name or instance.role_type.name
16@receiver(post_save, sender=Organization)
17def create_default_roles(sender, instance, created, **kwargs):
18 if created:
19 pass
22@receiver(post_save, sender=Organization)
23def create_default_payment_methods(sender, instance, created, **kwargs):
24 if created:
25 payment_methods = PaymentMethod.objects.filter(is_auto_installable=True)
26 for payment_method in payment_methods:
27 OrganizationPaymentMethod.objects.create(
28 organization=instance, name=payment_method.name, sat_id=payment_method.sat_id
29 )
32@receiver(post_save, sender=Organization)
33def create_default_org_roles(sender, instance, created, **kwargs):
34 if created:
35 default_role_types = RoleType.objects.filter(is_auto_installable=True)
36 for default_role_type in default_role_types:
37 organization_role = OrganizationRole.objects.create(
38 organization=instance, role_type=default_role_type, name=default_role_type.name
39 )
42@receiver(post_save, sender=Practitioner)
43def create_org_for_newly_created_practitioners(sender, instance, created, **kwargs):
44 if created:
45 new_org = Organization.objects.create(name=f"Clínica de {instance.full_name}")
47 affiliation = Affiliation.objects.create(
48 user=instance.user, organization=new_org, is_owner=True, status=Affiliation.Status.ACCEPTED
49 )