Coverage for memberships/serializers/contact.py: 97%
51 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 rest_framework import serializers
2from ..models import Membership, MembershipEmail, MembershipPhone, MembershipAddress
5class MembershipEmailSerializer(serializers.ModelSerializer):
6 """
7 Serializer for MembershipEmail
8 """
10 from_user = serializers.SerializerMethodField(read_only=True)
12 class Meta:
13 model = MembershipEmail
14 fields = ["random_slug", "membership", "email", "type_of", "from_user"]
15 extra_kwargs = {"random_slug": {"read_only": True}, "membership": {"write_only": True, "required": False}}
17 def get_from_user(self, instance):
18 return instance.affiliation is not None
20 def validate_email(self, value):
21 initial = self.get_initial()
22 if initial.get("membership"):
23 if MembershipEmail.objects.filter(membership=initial.get("membership"), email=value): 23 ↛ 24line 23 didn't jump to line 24, because the condition on line 23 was never true
24 raise serializers.ValidationError("Contact email must be unique")
25 return value
28class MembershipPhoneSerializer(serializers.ModelSerializer):
29 """
30 Serializer for MembershipPhone
31 """
33 class Meta:
34 model = MembershipPhone
35 fields = ["random_slug", "membership", "phone", "type_of"]
36 extra_kwargs = {"random_slug": {"read_only": True}, "membership": {"write_only": True, "required": False}}
39class MembershipAddressSerializer(serializers.ModelSerializer):
40 """
41 Serializer for MembershipAddress
42 """
44 raw = serializers.CharField(required=True, write_only=True)
45 street_number = serializers.CharField(required=False, write_only=True)
46 internal_number = serializers.CharField(required=False, write_only=True)
47 route = serializers.CharField(required=False, write_only=True)
48 locality = serializers.CharField(required=False, write_only=True)
49 postal_code = serializers.CharField(required=False, write_only=True)
50 state = serializers.CharField(required=False, write_only=True)
51 state_code = serializers.CharField(required=False, write_only=True)
52 country = serializers.CharField(required=False, write_only=True)
53 country_code = serializers.CharField(required=False, write_only=True)
54 address = serializers.CharField(read_only=True, source="address.raw")
56 class Meta:
57 model = MembershipAddress
58 read_only_fields = ["address"]
59 fields = read_only_fields + [
60 "random_slug",
61 "membership",
62 "raw",
63 "street_number",
64 "internal_number",
65 "route",
66 "locality",
67 "postal_code",
68 "state",
69 "state_code",
70 "country",
71 "country_code",
72 "type_of",
73 ]
74 extra_kwargs = {"random_slug": {"read_only": True}, "membership": {"write_only": True, "required": False}}
76 def create(self, validated_data):
77 membership = validated_data.pop("membership")
78 type_of = validated_data.pop("type_of")
79 internal_number = validated_data.pop("internal_number")
80 membership_address = MembershipAddress.objects.create(
81 membership=membership, type_of=type_of, internal_number=internal_number, address=validated_data
82 )
83 return membership_address
86class MembershipContactSerializer(serializers.ModelSerializer):
87 emails = MembershipEmailSerializer(many=True)
88 phones = MembershipPhoneSerializer(many=True)
89 addresses = MembershipAddressSerializer(many=True)
91 class Meta:
92 model = Membership
93 fields = ["emails", "phones", "addresses"]