Skip to content

Commit cca52e4

Browse files
feat(user): Add GET api/user/promo/{id} endpoint
Implements a read-only endpoint for authenticated users to retrieve details of a specific promo code by its UUID. - Does not expose the actual promo code value. - Does not activate the promo code. - Includes promo description, company info, image, active status.
1 parent 02a0947 commit cca52e4

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

promo_code/user/serializers.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import rest_framework_simplejwt.token_blacklist.models as tb_models
1010
import rest_framework_simplejwt.tokens
1111

12+
import business.models
1213
import user.constants
1314
import user.models
1415
import user.validators
@@ -244,3 +245,64 @@ def to_representation(self, instance):
244245
if not instance.avatar_url:
245246
data.pop('avatar_url', None)
246247
return data
248+
249+
250+
class UserPromoDetailSerializer(rest_framework.serializers.ModelSerializer):
251+
"""
252+
Serializer for detailed promo-code information
253+
(without revealing the code value).
254+
The output format matches the given example.
255+
"""
256+
257+
promo_id = rest_framework.serializers.UUIDField(
258+
source='id',
259+
read_only=True,
260+
)
261+
company_id = rest_framework.serializers.UUIDField(
262+
source='company.id',
263+
read_only=True,
264+
)
265+
company_name = rest_framework.serializers.CharField(
266+
source='company.name',
267+
read_only=True,
268+
)
269+
active = rest_framework.serializers.BooleanField(
270+
source='is_active',
271+
read_only=True,
272+
)
273+
is_activated_by_user = rest_framework.serializers.SerializerMethodField()
274+
like_count = rest_framework.serializers.SerializerMethodField()
275+
is_liked_by_user = rest_framework.serializers.SerializerMethodField()
276+
comment_count = rest_framework.serializers.SerializerMethodField()
277+
278+
class Meta:
279+
model = business.models.Promo
280+
fields = (
281+
'promo_id',
282+
'company_id',
283+
'company_name',
284+
'description',
285+
'image_url',
286+
'active',
287+
'is_activated_by_user',
288+
'like_count',
289+
'is_liked_by_user',
290+
'comment_count',
291+
)
292+
read_only_fields = fields
293+
294+
def get_is_activated_by_user(self, obj) -> bool:
295+
# TODO:
296+
return False
297+
298+
def get_like_count(self, obj) -> int:
299+
# TODO:
300+
return 0
301+
302+
def get_is_liked_by_user(self, obj) -> bool:
303+
# TODO:
304+
return False
305+
306+
def get_comment_count(self, obj) -> int:
307+
# TODO:
308+
return 0

promo_code/user/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@
2727
user.views.UserProfileView.as_view(),
2828
name='user-profile',
2929
),
30+
django.urls.path(
31+
'promo/<uuid:id>/',
32+
user.views.UserPromoDetailView.as_view(),
33+
name='user-promo-detail',
34+
),
3035
]

promo_code/user/views.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import rest_framework_simplejwt.tokens
66
import rest_framework_simplejwt.views
77

8+
import business.constants
9+
import business.models
810
import user.models
911
import user.serializers
1012

@@ -48,6 +50,7 @@ class UserProfileView(
4850
Retrieve (GET) and partially update (PATCH)
4951
detailed user profile information.
5052
"""
53+
5154
http_method_names = ['get', 'patch', 'options', 'head']
5255
serializer_class = user.serializers.UserProfileSerializer
5356
permission_classes = [rest_framework.permissions.IsAuthenticated]
@@ -57,3 +60,36 @@ def get_object(self):
5760

5861
def patch(self, request, *args, **kwargs):
5962
return self.partial_update(request, *args, **kwargs)
63+
64+
65+
class UserPromoDetailView(rest_framework.generics.RetrieveAPIView):
66+
"""
67+
Retrieve (GET) information about the promo without receiving a promo code.
68+
"""
69+
70+
queryset = (
71+
business.models.Promo.objects.select_related('company')
72+
.prefetch_related(
73+
'unique_codes',
74+
)
75+
.only(
76+
'id',
77+
'company__id',
78+
'company__name',
79+
'description',
80+
'image_url',
81+
'active',
82+
'active_from',
83+
'active_until',
84+
'mode',
85+
'used_count',
86+
)
87+
)
88+
89+
serializer_class = user.serializers.UserPromoDetailSerializer
90+
91+
permission_classes = [
92+
rest_framework.permissions.IsAuthenticated,
93+
]
94+
95+
lookup_field = 'id'

0 commit comments

Comments
 (0)