Skip to content

Commit

Permalink
Create api endpoint to get, post, patch and delete coupons
Browse files Browse the repository at this point in the history
  • Loading branch information
earlinn committed Feb 1, 2024
1 parent 4faf17a commit 29335d6
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
30 changes: 30 additions & 0 deletions backend/api/products_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
Tag,
)

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


Expand Down Expand Up @@ -510,6 +513,33 @@ def setup_eager_loading(cls, queryset, user):
)


class CouponSerializer(serializers.ModelSerializer):
"""Serializer for coupons representation."""

class Meta:
model = Coupon
fields = (
"id",
"code",
"name",
"slug",
"promotion_type",
"discount",
"is_active",
"is_constant",
"start_time",
"end_time",
"conditions",
"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
67 changes: 67 additions & 0 deletions backend/api/products_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
CategoryCreateSerializer,
CategorySerializer,
ComponentSerializer,
CouponSerializer,
FavoriteProductCreateSerializer,
FavoriteProductDeleteSerializer,
FavoriteProductSerializer,
Expand All @@ -40,6 +41,7 @@
from products.models import (
Category,
Component,
Coupon,
FavoriteProduct,
Producer,
Product,
Expand Down Expand Up @@ -511,6 +513,71 @@ class PromotionViewSet(DestroyWithPayloadMixin, viewsets.ModelViewSet):
permission_classes = [IsAdminOrReadOnly]


@method_decorator(
name="list",
decorator=swagger_auto_schema(
operation_summary="List all coupons",
operation_description="Returns a list of all the coupons",
responses={200: CouponSerializer},
),
)
@method_decorator(
name="retrieve",
decorator=swagger_auto_schema(
operation_summary="Get coupon by id",
operation_description="Retrieves a coupon by its id",
responses={200: CouponSerializer, 404: ErrorResponse404Serializer},
),
)
@method_decorator(
name="create",
decorator=swagger_auto_schema(
operation_summary="Create coupon",
operation_description="Creates a coupon (admin only)",
responses={
201: CouponSerializer,
400: ValidationErrorResponseSerializer,
401: ErrorResponse401Serializer,
403: ErrorResponse403Serializer,
},
),
)
@method_decorator(
name="partial_update",
decorator=swagger_auto_schema(
operation_summary="Edit coupon",
operation_description="Edits a coupon by its id (admin only)",
responses={
200: CouponSerializer,
400: ValidationErrorResponseSerializer,
401: ErrorResponse401Serializer,
403: ErrorResponse403Serializer,
404: ErrorResponse404Serializer,
},
),
)
@method_decorator(
name="destroy",
decorator=swagger_auto_schema(
operation_summary="Delete coupon",
operation_description="Deletes a coupon by its id (admin only)",
responses={
200: STATUS_200_RESPONSE_ON_DELETE_IN_DOCS,
401: ErrorResponse401Serializer,
403: ErrorResponse403Serializer,
404: ErrorResponse404Serializer,
},
),
)
class CouponViewSet(DestroyWithPayloadMixin, viewsets.ModelViewSet):
"""Viewset for coupons."""

http_method_names = ["get", "post", "patch", "delete"]
queryset = Coupon.objects.all()
serializer_class = CouponSerializer
permission_classes = [IsAdminOrReadOnly]


@method_decorator(
name="list",
decorator=swagger_auto_schema(
Expand Down
2 changes: 2 additions & 0 deletions backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .products_views import (
CategoryViewSet,
ComponentViewSet,
CouponViewSet,
FavoriteProductViewSet,
ProducerViewSet,
ProductViewSet,
Expand All @@ -33,6 +34,7 @@
router.register("tags", TagViewSet)
router.register("producers", ProducerViewSet)
router.register("promotions", PromotionViewSet)
router.register("coupons", CouponViewSet)
router.register("products", ProductViewSet)
router.register(
r"products/(?P<product_id>\d+)/reviews", ReviewViewSet, basename="reviews"
Expand Down
2 changes: 1 addition & 1 deletion backend/products/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
from core.models import CategoryModel
from users.models import User

MAX_PROMOTIONS_NUMBER = 1
COUPON_PROMOTION_TYPE_ERROR_MESSAGE = (
'Указан неверный тип промоакции, нужно выбрать "Промокод".'
)
INCORRECT_COUPON_APPLY_ERROR = (
"Промокод не может быть применен к отдельному товару, "
"он применяется к Корзине в целом."
)
MAX_PROMOTIONS_NUMBER = 1


class Category(CategoryModel):
Expand Down

0 comments on commit 29335d6

Please sign in to comment.