Skip to content

Commit c5926ca

Browse files
refactor(serializers): Introduce base classes to reduce duplication
Refactor user, promo, and comment serializers to improve maintainability and reduce redundant code by using base classes. - Introduce `BaseUserSerializer` to share common user fields and logic between `SignUpSerializer` and `UserProfileSerializer`. - Create `BaseUserPromoSerializer` to consolidate common fields and methods for `PromoFeedSerializer` and `UserPromoDetailSerializer`. - Implement `BaseCommentSerializer` to streamline `CommentSerializer`, `CommentCreateSerializer`, and `CommentUpdateSerializer`. - Simplify the update logic in `UserProfileSerializer` and extract cache invalidation into a private `_invalidate_cache` method.
1 parent b630fcd commit c5926ca

File tree

3 files changed

+124
-197
lines changed

3 files changed

+124
-197
lines changed

promo_code/business/managers.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,31 @@ def create_company(self, email, name, password=None, **extra_fields):
2323

2424
class PromoManager(django.db.models.Manager):
2525
with_related_fields = (
26-
'id',
27-
'company__id',
28-
'company__name',
29-
'description',
30-
'image_url',
31-
'target',
32-
'max_count',
33-
'active_from',
34-
'active_until',
35-
'mode',
36-
'promo_common',
37-
'created_at',
26+
'id',
27+
'company__id',
28+
'company__name',
29+
'description',
30+
'image_url',
31+
'target',
32+
'max_count',
33+
'active_from',
34+
'active_until',
35+
'mode',
36+
'promo_common',
37+
'created_at',
3838
)
3939

4040
def get_queryset(self):
4141
return super().get_queryset()
4242

4343
def with_related(self):
4444
return (
45-
self.select_related('company')
46-
.prefetch_related('unique_codes')
47-
.only(
48-
*self.with_related_fields,
45+
self.select_related('company')
46+
.prefetch_related('unique_codes')
47+
.only(
48+
*self.with_related_fields,
49+
)
4950
)
50-
)
5151

5252
def for_company(self, user):
5353
return self.with_related().filter(company=user)

promo_code/core/serializers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ class BasePromoSerializer(rest_framework.serializers.ModelSerializer):
112112
required=True,
113113
)
114114
target = TargetSerializer(
115-
required=True, allow_null=True,
115+
required=True,
116+
allow_null=True,
116117
)
117118
promo_common = rest_framework.serializers.CharField(
118119
min_length=business.constants.PROMO_COMMON_CODE_MIN_LENGTH,

0 commit comments

Comments
 (0)