Coverage for organizations/signals.py: 61%
31 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-03-29 14:03 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-03-29 14:03 -0600
1from django.dispatch import receiver
2from django.db.models.signals import post_save, pre_save, post_delete
3from app.facturapi import FacturapiUserClient
4from invoices.utils import remove_social_regime_from_business_name
5from .models import PanelLanguage, OrganizationModule, Organization, OrganizationTaxInformation
8@receiver(post_save, sender=Organization)
9def create_default_settings(sender, instance, created, **kwargs):
10 """
11 Create Base settings for Panel Language and Modules
12 """
13 if created: 13 ↛ exitline 13 didn't return from function 'create_default_settings', because the condition on line 13 was never false
14 for choice in PanelLanguage.SystemWordChoices.options():
15 PanelLanguage.objects.get_or_create(
16 organization=instance,
17 system_word=choice.value,
18 verbose_name=choice.default_single,
19 plural_verbose_name=choice.default_plural,
20 )
22 for choice in OrganizationModule.ModuleChoices.options():
23 OrganizationModule.objects.get_or_create(organization=instance, module=choice.value)
26@receiver(post_save, sender=Organization)
27def create_facturapi_organization(sender, instance, created, **kwargs):
28 """
29 Create Facturapi Organization
30 """
31 if created and instance.facturapi_id is None: 31 ↛ 32line 31 didn't jump to line 32, because the condition on line 31 was never true
32 facturapi = FacturapiUserClient()
33 response = facturapi.Organization.create(instance.name)
34 facturapi_id = response.get("id")
35 facturapi_secret_key = facturapi.Organization.update_secret_key(facturapi_id)
36 instance.facturapi_id = facturapi_id
37 instance.facturapi_secret_key = facturapi_secret_key
38 instance.save()
41@receiver(post_delete, sender=Organization)
42def delete_facturapi_organization(sender, instance, **kwargs):
43 """
44 Delete Facturapi Organization
45 """
46 facturapi = FacturapiUserClient()
47 facturapi.Organization.delete(instance.facturapi_id)
50@receiver(post_save, sender=OrganizationTaxInformation)
51def set_legal_name(sender, instance, created, *args, **kwargs):
52 if created:
53 instance.legal_name = remove_social_regime_from_business_name(instance.legal_name)
54 instance.save()