HiPhone_BE/market/viewsets.py

116 lines
3.9 KiB
Python

from rest_framework.decorators import action
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from core.mixins import ActionBasedMixin
from core.permissions import IsAuthorOrReadOnly, IsAdminUserOrReadOnly
from market.models import Brand, Product, Post, ItemIssues, Image
from market.serializers import (
BrandSerializer,
ProductSerializer,
PostSerializer,
PostCreateSerializer,
PostListSerializer,
PostUpdateSerializer,
)
from graph.models import Transaction
from graph.methods import create_transaction
class BrandViewset(ActionBasedMixin, ModelViewSet):
queryset = Brand.objects.all()
serializer_class = BrandSerializer
permission_classes = [IsAdminUserOrReadOnly]
pagination_class = None
class ProductViewset(ActionBasedMixin, ModelViewSet):
queryset = Product.objects.all().select_related("brand")
serializer_class = ProductSerializer
serializer_class_map = {
"posts": PostListSerializer,
}
permission_classes = [IsAdminUserOrReadOnly]
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
@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()
.select_related("product", "author", "product__brand")
.prefetch_related("images")
)
serializer_class = PostSerializer
serializer_class_map = {
"list": PostListSerializer,
"create": PostCreateSerializer,
"update": PostUpdateSerializer,
}
permission_classes = [IsAuthenticated, IsAuthorOrReadOnly]
permission_classes_map = {
"list": [AllowAny],
"retrieve": [AllowAny],
"create": [AllowAny],
}
@action(detail=False, methods=["GET"])
def my(self, request):
user = request.user
queryset = user.posts.all()
page = self.paginate_queryset(queryset)
if page is not None:
serializer = PostListSerializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = PostListSerializer(queryset, many=True)
return Response(serializer.data)
def perform_create(self, serializer):
# item_issues = ItemIssues.objects.create()
# serializer.save(author=self.request.user, item_issues=item_issues)
serializer.save(author=self.request.user)
# images = self.request.data.getlist("images")
# for image in images:
# Image.objects.create(post=serializer.instance, image=image)
def partial_update(self, request, *args, **kwargs):
object = self.get_object()
done = request.data.get("done")
if not object.done and done:
create_transaction(object)
elif object.done and not done:
Transaction.objects.filter(post=object).delete()
if object.done != done:
object.done = done
object.save()
return Response(status=200)
def perform_update(self, serializer):
object = self.get_object()
instance = serializer.instance
done = serializer.validated_data.get("done")
if not object.done and done:
create_transaction(instance)
elif object.done and not done:
Transaction.objects.filter(post=object).delete()
return super().perform_update(serializer)