Coverage for apps/organizations/serializers.py: 94%
78 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-10-09 09:43 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-10-09 09:43 -0600
1from rest_framework import serializers
2from rest_polymorphic.serializers import PolymorphicSerializer
4from apps.campaigns.models import CampaignClaim
5from apps.catalogues.serializers import FontPolymorphicSerializer, TemplateSerializer
6from apps.rewards.models import RewardClaim
7from base.serializers import ToRepresentationMixin
9from .models import (
10 Organization,
11 OrganizationCampaignLabel,
12 OrganizationCarousel,
13 OrganizationFile,
14 OrganizationImage,
15 OrganizationSiteLabel,
16 OrganizationSocial,
17)
20class OrganizationSiteLabelSerializer(serializers.ModelSerializer):
21 """
22 Serializer for Organization Labels
23 """
25 class Meta:
26 model = OrganizationSiteLabel
27 read_only_fields = ["random_slug", "label"]
28 fields = read_only_fields + ["organization", "singular", "plural"]
29 extra_kwargs = {"organization": {"write_only": True}}
32class OrganizationCampaignLabelSerializer(serializers.ModelSerializer):
33 """
34 Serializer for Organization Labels
35 """
37 class Meta:
38 model = OrganizationCampaignLabel
39 read_only_fields = ["random_slug", "label"]
40 fields = read_only_fields + ["organization", "singular", "plural"]
41 extra_kwargs = {"organization": {"write_only": True}}
44class OrganizationFileSerializer(serializers.ModelSerializer):
45 """
46 Serializer for Organization File
47 """
49 class Meta:
50 model = OrganizationFile
51 fields = ["random_slug", "name", "file", "organization"]
54class OrganizationImageSerializer(serializers.ModelSerializer):
55 """
56 Serializer for Organization Image
57 """
59 class Meta:
60 model = OrganizationImage
61 fields = ["random_slug", "name", "image", "organization"]
64class OrganizationUploadPolymorphicSerializer(PolymorphicSerializer):
65 """
66 Polymorphic serializer for Organization uploads
67 """
69 model_serializer_mapping = {
70 OrganizationFile: OrganizationFileSerializer,
71 OrganizationImage: OrganizationImageSerializer,
72 }
75class OrganizationSocialSerializer(serializers.ModelSerializer):
76 """
77 Serializer for Organization Social
78 """
80 class Meta:
81 model = OrganizationSocial
82 read_only_fields = ["random_slug"]
83 fields = read_only_fields + ["organization", "link", "social"]
86class OrganizationCarouselSerializer(ToRepresentationMixin):
87 """
88 Serializer for Organizatino Carousel Images
89 """
91 REPRESENTATION_FIELDS = [("image", OrganizationImageSerializer, False)]
93 class Meta:
94 model = OrganizationCarousel
95 read_only_fields = [
96 "random_slug",
97 ]
98 fields = read_only_fields + [
99 "organization",
100 "image",
101 "heading",
102 "primary_button_cta",
103 "secondary_button_cta",
104 "primary_button_text",
105 "secondary_button_text",
106 ]
109class BasicOrganizationSerializer(serializers.ModelSerializer):
110 """
111 Serializer for Organization with minimum data
112 """
114 class Meta:
115 model = Organization
116 read_only_fields = ["random_slug", "name"]
117 fields = read_only_fields
120class OrganizationSerializer(ToRepresentationMixin):
121 """
122 Serializer for Organization
123 """
125 REPRESENTATION_FIELDS = [
126 ("socials", OrganizationSocialSerializer, True),
127 ("logo", OrganizationUploadPolymorphicSerializer, False),
128 ("font", FontPolymorphicSerializer, False),
129 ("template", TemplateSerializer, False),
130 ("carousel", OrganizationCarouselSerializer, True),
131 ("parent", BasicOrganizationSerializer, False),
132 ("site_labels", OrganizationSiteLabelSerializer, True),
133 ("campaign_labels", OrganizationCampaignLabelSerializer, True),
134 ]
136 class Meta:
137 model = Organization
138 read_only_fields = [
139 "random_slug",
140 ]
141 fields = read_only_fields + [
142 "socials",
143 "font",
144 "template",
145 "logo",
146 "carousel",
147 "site_labels",
148 "campaign_labels",
149 "parent",
150 "domain",
151 "name",
152 "terms_and_conditions",
153 "privacy_policy",
154 "primary_color",
155 "secondary_color",
156 "contact_address",
157 "button_border_radius",
158 "is_active",
159 "show_confetti",
160 ]
163class OrganizationTreeSerializer(serializers.ModelSerializer):
164 """
165 Serializer for Organization formatted as tree
166 """
168 children = serializers.SerializerMethodField()
170 class Meta:
171 model = Organization
172 read_only_fields = ["random_slug", "is_active"]
173 fields = read_only_fields + ["parent", "name", "children"]
175 def get_children(self, instance):
176 qs = Organization.objects.filter(parent=instance)
177 serializer = OrganizationTreeSerializer(qs, many=True)
178 return serializer.data
181class OrganizationStatsSerializer(serializers.ModelSerializer):
182 """
183 Serializer for Organization stats
184 """
186 fan_visits = serializers.SerializerMethodField()
187 fan_registration = serializers.SerializerMethodField()
188 most_claimed_campaigns = serializers.SerializerMethodField()
189 most_claimed_rewards = serializers.SerializerMethodField()
191 class Meta:
192 model = Organization
193 read_only_fields = ["random_slug", "name", "domain"]
194 fields = read_only_fields + ["fan_visits", "fan_registration", "most_claimed_campaigns", "most_claimed_rewards"]
196 def get_fan_visits(self, instance: Organization):
197 return 0
199 def get_fan_registration(self, instance: Organization):
200 return 0
202 def get_most_claimed_campaigns(self, instance: Organization):
203 return CampaignClaim.objects.filter(campaign__organization=instance).count()
205 def get_most_claimed_rewards(self, instance: Organization):
206 return RewardClaim.objects.filter(reward__organization=instance).count()