Skip to content

Commit b630fcd

Browse files
refactor(business): Streamline promo views and data handling
This commit refactors the `business` app's views and serializers to improve efficiency and clarify the logic for handling promo data. Key changes: - **Simplified Query Validation:** The `CompanyPromoListCreateView` now validates query parameters directly within the `get_queryset` method, removing the need for an intermediate `list` method override and instance variables. - **Optimized Target Updates:** The `PromoDetailSerializer` now correctly handles updates to the `target` field by calling the parent `update` method first and then saving the `target` field specifically. This ensures atomicity and prevents unnecessary model saves. - **Improved QuerySet Definition:** The `PromoManager` now defines `with_related_fields` as a tuple to clearly specify the fields for `only()`, making the query more readable and maintainable.
1 parent 9a70256 commit b630fcd

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

promo_code/business/managers.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,32 @@ def create_company(self, email, name, password=None, **extra_fields):
2222

2323

2424
class PromoManager(django.db.models.Manager):
25+
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',
38+
)
39+
2540
def get_queryset(self):
2641
return super().get_queryset()
2742

2843
def with_related(self):
2944
return (
30-
self.select_related('company')
31-
.prefetch_related('unique_codes')
32-
.only(
33-
'id',
34-
'company',
35-
'description',
36-
'image_url',
37-
'target',
38-
'max_count',
39-
'active_from',
40-
'active_until',
41-
'mode',
42-
'promo_common',
43-
'created_at',
44-
'company__id',
45-
'company__name',
46-
)
45+
self.select_related('company')
46+
.prefetch_related('unique_codes')
47+
.only(
48+
*self.with_related_fields,
4749
)
50+
)
4851

4952
def for_company(self, user):
5053
return self.with_related().filter(company=user)

promo_code/business/serializers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,12 @@ def get_promo_unique(self, obj):
271271
def update(self, instance, validated_data):
272272
target_data = validated_data.pop('target', None)
273273

274-
for attr, value in validated_data.items():
275-
setattr(instance, attr, value)
274+
instance = super().update(instance, validated_data)
276275

277276
if target_data is not None:
278277
instance.target = target_data
278+
instance.save(update_fields=['target'])
279279

280-
instance.save()
281280
return instance
282281

283282

promo_code/business/views.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,21 @@ class CompanyPromoListCreateView(rest_framework.generics.ListCreateAPIView):
7474
rest_framework.permissions.IsAuthenticated,
7575
business.permissions.IsCompanyUser,
7676
]
77-
# Pagination is only needed for GET (listing)
7877
pagination_class = core.pagination.CustomLimitOffsetPagination
7978

80-
_validated_query_params = {}
81-
8279
def get_serializer_class(self):
8380
if self.request.method == 'POST':
8481
return business.serializers.PromoCreateSerializer
8582

8683
return business.serializers.PromoReadOnlySerializer
8784

88-
def list(self, request, *args, **kwargs):
85+
def get_queryset(self):
8986
query_serializer = business.serializers.PromoListQuerySerializer(
90-
data=request.query_params,
87+
data=self.request.query_params,
9188
)
9289
query_serializer.is_valid(raise_exception=True)
93-
self._validated_query_params = query_serializer.validated_data
90+
params = query_serializer.validated_data
9491

95-
return super().list(request, *args, **kwargs)
96-
97-
def get_queryset(self):
98-
params = self._validated_query_params
9992
countries = [c.upper() for c in params.get('countries', [])]
10093
sort_by = params.get('sort_by')
10194

0 commit comments

Comments
 (0)