Skip to content

Commit

Permalink
Set automatically correct promotion_type during coupon creation
Browse files Browse the repository at this point in the history
  • Loading branch information
earlinn committed Feb 2, 2024
1 parent 04a2ac2 commit 3250390
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
11 changes: 2 additions & 9 deletions backend/api/products_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
Tag,
)

COUPON_PROMOTION_TYPE_API_ERROR_MESSAGE = (
f"Указан неверный тип промоакции, нужно выбрать {Promotion.COUPON}."
)
RATING_DECIMAL_PLACES = 1


Expand Down Expand Up @@ -516,6 +513,8 @@ def setup_eager_loading(cls, queryset, user):
class CouponSerializer(serializers.ModelSerializer):
"""Serializer for coupons representation."""

promotion_type = serializers.ReadOnlyField()

class Meta:
model = Coupon
fields = (
Expand All @@ -533,12 +532,6 @@ class Meta:
"image",
)

def validate_promotion_type(self, value):
"""Checks that promotion_type is correct."""
if value != Promotion.COUPON:
raise serializers.ValidationError(COUPON_PROMOTION_TYPE_API_ERROR_MESSAGE)
return value


class CouponApplySerializer(serializers.ModelSerializer):
"""Serializer to apply coupon promoaction to the order."""
Expand Down
12 changes: 12 additions & 0 deletions backend/api/products_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,12 @@ class CouponViewSet(DestroyWithPayloadMixin, viewsets.ModelViewSet):
serializer_class = CouponSerializer
permission_classes = [IsAdminOrReadOnly]

@transaction.atomic
def perform_create(self, serializer):
"""Sets the correct promotion_type during coupon creation."""
serializer.save(promotion_type=Promotion.COUPON)
return super().perform_create(serializer)


@method_decorator(
name="list",
Expand Down Expand Up @@ -664,13 +670,19 @@ def get_queryset(self):

@transaction.atomic
def perform_create(self, serializer):
"""
Sets the correct category for the given subcategory during product creation.
"""
subcategory_id = serializer._kwargs["data"]["subcategory"]
subcategory = Subcategory.objects.get(id=subcategory_id)
serializer.save(category=subcategory.parent_category)
return super().perform_create(serializer)

@transaction.atomic
def perform_update(self, serializer):
"""
Sets the correct category for the given subcategory during product editing.
"""
subcategory_id = serializer._kwargs["data"].get("subcategory")
if subcategory_id:
subcategory = Subcategory.objects.get(id=subcategory_id)
Expand Down
2 changes: 2 additions & 0 deletions backend/api/reviews_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ def get_queryset(self):
).select_related("product", "author")

def perform_create(self, serializer):
"""Sets the correct author and product during review creation."""
product = get_object_or_404(Product, pk=self.kwargs.get("product_id"))
serializer.save(author=self.request.user, product=product)

def perform_update(self, serializer):
"""Updates pub_date and was_edited fields during review editing."""
serializer.save(pub_date=timezone.now(), was_edited=True)
return super().perform_update(serializer)

Expand Down

0 comments on commit 3250390

Please sign in to comment.