Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions promo_code/user/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import rest_framework_simplejwt.token_blacklist.models as tb_models
import rest_framework_simplejwt.tokens

import business.models
import user.constants
import user.models
import user.validators
Expand Down Expand Up @@ -244,3 +245,64 @@ def to_representation(self, instance):
if not instance.avatar_url:
data.pop('avatar_url', None)
return data


class UserPromoDetailSerializer(rest_framework.serializers.ModelSerializer):
"""
Serializer for detailed promo-code information
(without revealing the code value).
The output format matches the given example.
"""

promo_id = rest_framework.serializers.UUIDField(
source='id',
read_only=True,
)
company_id = rest_framework.serializers.UUIDField(
source='company.id',
read_only=True,
)
company_name = rest_framework.serializers.CharField(
source='company.name',
read_only=True,
)
active = rest_framework.serializers.BooleanField(
source='is_active',
read_only=True,
)
is_activated_by_user = rest_framework.serializers.SerializerMethodField()
like_count = rest_framework.serializers.SerializerMethodField()
is_liked_by_user = rest_framework.serializers.SerializerMethodField()
comment_count = rest_framework.serializers.SerializerMethodField()

class Meta:
model = business.models.Promo
fields = (
'promo_id',
'company_id',
'company_name',
'description',
'image_url',
'active',
'is_activated_by_user',
'like_count',
'is_liked_by_user',
'comment_count',
)
read_only_fields = fields

def get_is_activated_by_user(self, obj) -> bool:
# TODO:
return False

def get_like_count(self, obj) -> int:
# TODO:
return 0

def get_is_liked_by_user(self, obj) -> bool:
# TODO:
return False

def get_comment_count(self, obj) -> int:
# TODO:
return 0
5 changes: 5 additions & 0 deletions promo_code/user/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@
user.views.UserProfileView.as_view(),
name='user-profile',
),
django.urls.path(
'promo/<uuid:id>/',
user.views.UserPromoDetailView.as_view(),
name='user-promo-detail',
),
]
36 changes: 36 additions & 0 deletions promo_code/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import rest_framework_simplejwt.tokens
import rest_framework_simplejwt.views

import business.constants
import business.models
import user.models
import user.serializers

Expand Down Expand Up @@ -48,6 +50,7 @@ class UserProfileView(
Retrieve (GET) and partially update (PATCH)
detailed user profile information.
"""

http_method_names = ['get', 'patch', 'options', 'head']
serializer_class = user.serializers.UserProfileSerializer
permission_classes = [rest_framework.permissions.IsAuthenticated]
Expand All @@ -57,3 +60,36 @@ def get_object(self):

def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)


class UserPromoDetailView(rest_framework.generics.RetrieveAPIView):
"""
Retrieve (GET) information about the promo without receiving a promo code.
"""

queryset = (
business.models.Promo.objects.select_related('company')
.prefetch_related(
'unique_codes',
)
.only(
'id',
'company__id',
'company__name',
'description',
'image_url',
'active',
'active_from',
'active_until',
'mode',
'used_count',
)
)

serializer_class = user.serializers.UserPromoDetailSerializer

permission_classes = [
rest_framework.permissions.IsAuthenticated,
]

lookup_field = 'id'