Coverage for apps/organizations/serializers.py: 59%
62 statements
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-23 13:39 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-23 13:39 -0600
1from django.db.models import TextChoices
2from rest_framework import serializers
4from apps.practitioners.models import Practitioner
5from apps.users.models import User
6from apps.users.serializers import ResetPasswordSerializer, UserSerializer
7from apps.users.services import send_welcome_user_with_invitation_email
8from base.mixins import ToRepresentationMixin
10from .models import Affiliation, Organization, OrganizationRole
11from .services import send_invitation_email
13# send_initial_password_set_email
16class OrganizationRoleSerializer(serializers.ModelSerializer):
17 role_type = serializers.CharField(source="role_type.type_of")
19 class Meta:
20 model = OrganizationRole
21 read_only_fields = ["role_type"]
22 fields = ["name", "role_type", "random_slug"]
25class OrganizationSerializer(ToRepresentationMixin):
26 REPRESENTATION_FIELDS = [["roles", OrganizationRoleSerializer, True]]
28 class Meta:
29 model = Organization
30 read_only_fields = ["random_slug", "created_at"]
31 fields = read_only_fields + ["name", "logo", "roles", "order", "is_active"]
33 def create(self, validated_data):
34 user = self.context.get("request").user
35 organization = super().create(validated_data)
37 """
38 # Validate for free practitioners
39 practitioner = getattr(user, "practitioner", None)
40 if not practitioner.is_paid:
41 organizations = Organization.objects.filter(
42 affiliation__user=user,
43 affiliation__is_owner=True,
44 )
45 if organizations.count() > 0:
46 raise serializers.ValidationError(
47 "Free user usage limit exceded (more than 1 organization on dataabase)"
48 )
49 """
51 Affiliation.objects.create(user=user, organization=organization, is_owner=True)
52 return organization
55class AffiliationSerializer(ToRepresentationMixin):
56 """
57 Serailizer for Affiliation
58 """
60 REPRESENTATION_FIELDS = [
61 ["user", UserSerializer, False],
62 ["roles", OrganizationRoleSerializer, True],
63 ["organization", OrganizationSerializer, False],
64 ]
66 class TypeOf(TextChoices):
67 PRACTITIONER = "PRACTITIONER", "Profesionista"
68 ADMINISTRATIVE = "ADMINISTRATIVE", "Personal Administrativo"
70 email = serializers.EmailField(write_only=True)
71 type_of = serializers.ChoiceField(write_only=True, choices=TypeOf.choices)
73 class Meta:
74 model = Affiliation
75 read_only_fields = ["user", "random_slug"]
76 fields = read_only_fields + ["email", "organization", "type_of", "roles", "is_owner"]
78 def create(self, validated_data):
79 email = validated_data.pop("email")
80 type_of = validated_data.pop("type_of")
81 organization = validated_data.pop("organization")
82 roles = validated_data.pop("roles")
84 """
85 # Validate for free practitioners
86 practitioner = getattr(self.context.get("request").user, "practitioner", None)
87 if not practitioner.is_paid:
88 affiliation_count = organization.affiliation_set.count()
89 if affiliation_count > 2:
90 raise serializers.ValidationError(
91 "Free user usage limit exceded (more than 1 affiliation on dataabase)"
92 )
93 """
95 user, user_is_new = User.objects.get_or_create(email=email)
96 affiliation, _ = Affiliation.objects.get_or_create(user=user, organization=organization)
98 if user_is_new:
99 data = {"email": user.email}
100 serializer = ResetPasswordSerializer(data=data)
101 serializer.is_valid(raise_exception=True)
102 password_url, user = serializer.save(data)
103 send_welcome_user_with_invitation_email(user, password_url, affiliation)
104 else:
105 send_invitation_email(user, affiliation.random_slug)
107 if type_of == self.TypeOf.ADMINISTRATIVE:
108 user.onboarding_needed = False
109 user.save()
111 if type_of == self.TypeOf.PRACTITIONER and user_is_new:
112 user.onboarding_needed = True
113 user.save()
114 practitioner, _ = Practitioner.objects.get_or_create(user=user)
116 affiliation.roles.set(roles)
118 return affiliation