diff --git a/market/serializers.py b/market/serializers.py new file mode 100644 index 0000000..4a11c8f --- /dev/null +++ b/market/serializers.py @@ -0,0 +1,64 @@ +from rest_framework import serializers +from rest_framework.serializers import ModelSerializer + +from market.models import ( + Brand, + Product, + ProductColor, + ProductStorage, + Post, + Image, +) + + +class BrandSerializer(ModelSerializer): + class Meta: + model = Brand + fields = "__all__" + + +class ProductColorSerializer(ModelSerializer): + class Meta: + model = ProductColor + fields = "__all__" + + +class ProductStorageSerializer(ModelSerializer): + class Meta: + model = ProductStorage + fields = "__all__" + + +class ProductSerializer(ModelSerializer): + colors = ProductColorSerializer(many=True, read_only=True) + storages = ProductStorageSerializer(many=True, read_only=True) + + class Meta: + model = Product + fields = ("id", "name", "brand", "colors", "storages") + + +class ImageSerializer(ModelSerializer): + class Meta: + model = Image + fields = ("image",) + + +class PostSerializer(ModelSerializer): + nickname = serializers.CharField(source="author.nickname") + images = ImageSerializer(many=True, read_only=True) + + class Meta: + model = Post + fields = ( + "id", + "product", + "color", + "storage", + "price", + "text", + "nickname", + "status", + "written_at", + "images", + ) diff --git a/market/viewsets.py b/market/viewsets.py new file mode 100644 index 0000000..2fe20d4 --- /dev/null +++ b/market/viewsets.py @@ -0,0 +1,71 @@ +from rest_framework.decorators import action +from rest_framework.permissions import AllowAny, IsAdminUser, IsAuthenticated +from rest_framework.response import Response +from rest_framework.viewsets import ModelViewSet + +from core.mixins import ActionBasedMixin +from core.permissions import IsAuthorOrReadOnly +from market.models import Brand, Product, Post +from market.serializers import ( + BrandSerializer, + ProductSerializer, + PostSerializer, +) + + +class BrandViewset(ActionBasedMixin, ModelViewSet): + queryset = Brand.objects.all() + serializer_class = BrandSerializer + serializer_class_map = { + "products": ProductSerializer, + } + permission_classes = [IsAdminUser] + permission_classes_map = { + "list": [AllowAny], + "retrieve": [AllowAny], + } + + @action(detail=True, methods=["GET"]) + def products(self, request, pk): + brand = self.get_object() + serializer = self.get_serializer(brand.products.all(), many=True) + return Response(serializer.data) + + +class ProductViewset(ActionBasedMixin, ModelViewSet): + queryset = Product.objects.all() + serializer_class = ProductSerializer + serializer_class_map = { + "posts": PostSerializer, + } + permission_classes = [IsAdminUser] + permission_classes_map = { + "list": [AllowAny], + "retrieve": [AllowAny], + } + + @action(detail=True, methods=["GET"]) + def posts(self, request, pk): + product = self.get_object() + queryset = product.posts.all() + + page = self.paginate_queryset(queryset) + if page is not None: + serializer = self.get_serializer(page, many=True) + return self.get_paginated_response(serializer.data) + + serializer = self.get_serializer(queryset, many=True) + return Response(serializer.data) + + +class PostViewset(ActionBasedMixin, ModelViewSet): + queryset = Post.objects.all() + serializer_class = PostSerializer + permission_classes = [IsAuthenticated, IsAuthorOrReadOnly] + permission_classes_map = { + "list": [AllowAny], + "retrieve": [AllowAny], + } + + def perform_create(self, serializer): + serializer.save(author=self.request.user)