2023-05-03 14:23:44 +09:00
|
|
|
from rest_framework.decorators import action
|
2023-05-17 17:13:47 +09:00
|
|
|
from rest_framework.permissions import AllowAny, IsAuthenticated
|
2023-05-03 14:23:44 +09:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.viewsets import ModelViewSet
|
|
|
|
|
|
|
|
from core.mixins import ActionBasedMixin
|
2023-05-17 16:10:25 +09:00
|
|
|
from core.permissions import IsAuthorOrReadOnly, IsAdminUserOrReadOnly
|
2023-11-13 19:52:35 +09:00
|
|
|
from market.models import Brand, Product, Post, ItemIssues, Image
|
2023-05-03 14:23:44 +09:00
|
|
|
from market.serializers import (
|
|
|
|
BrandSerializer,
|
|
|
|
ProductSerializer,
|
|
|
|
PostSerializer,
|
2023-06-19 03:56:23 +09:00
|
|
|
PostCreateSerializer,
|
2023-06-16 16:36:27 +09:00
|
|
|
PostListSerializer,
|
2023-11-09 19:41:37 +09:00
|
|
|
PostUpdateSerializer,
|
2023-05-03 14:23:44 +09:00
|
|
|
)
|
2023-06-23 02:09:45 +09:00
|
|
|
from graph.models import Transaction
|
|
|
|
from graph.methods import create_transaction
|
2023-05-03 14:23:44 +09:00
|
|
|
|
|
|
|
|
|
|
|
class BrandViewset(ActionBasedMixin, ModelViewSet):
|
|
|
|
queryset = Brand.objects.all()
|
|
|
|
serializer_class = BrandSerializer
|
2023-05-17 16:10:25 +09:00
|
|
|
permission_classes = [IsAdminUserOrReadOnly]
|
2023-05-16 21:49:52 +09:00
|
|
|
pagination_class = None
|
2023-05-03 14:23:44 +09:00
|
|
|
|
|
|
|
|
|
|
|
class ProductViewset(ActionBasedMixin, ModelViewSet):
|
2023-06-19 03:56:23 +09:00
|
|
|
queryset = Product.objects.all().select_related("brand")
|
2023-05-03 14:23:44 +09:00
|
|
|
serializer_class = ProductSerializer
|
|
|
|
serializer_class_map = {
|
2023-06-23 02:09:45 +09:00
|
|
|
"posts": PostListSerializer,
|
2023-05-03 14:23:44 +09:00
|
|
|
}
|
2023-05-17 16:10:25 +09:00
|
|
|
permission_classes = [IsAdminUserOrReadOnly]
|
2023-05-03 14:23:44 +09:00
|
|
|
|
2023-10-16 22:53:14 +09:00
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
queryset = self.filter_queryset(self.get_queryset())
|
|
|
|
serializer = self.get_serializer(queryset, many=True)
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
2023-05-03 14:23:44 +09:00
|
|
|
@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):
|
2023-05-18 11:02:46 +09:00
|
|
|
queryset = (
|
|
|
|
Post.objects.all()
|
2023-06-19 03:56:23 +09:00
|
|
|
.select_related("product", "author", "product__brand")
|
2023-05-18 11:02:46 +09:00
|
|
|
.prefetch_related("images")
|
2023-05-17 17:13:47 +09:00
|
|
|
)
|
2023-05-03 14:23:44 +09:00
|
|
|
serializer_class = PostSerializer
|
2023-06-16 16:36:27 +09:00
|
|
|
serializer_class_map = {
|
|
|
|
"list": PostListSerializer,
|
2023-06-19 03:56:23 +09:00
|
|
|
"create": PostCreateSerializer,
|
2023-11-09 19:41:37 +09:00
|
|
|
"update": PostUpdateSerializer,
|
2023-06-16 16:36:27 +09:00
|
|
|
}
|
2023-05-03 14:23:44 +09:00
|
|
|
permission_classes = [IsAuthenticated, IsAuthorOrReadOnly]
|
|
|
|
permission_classes_map = {
|
|
|
|
"list": [AllowAny],
|
|
|
|
"retrieve": [AllowAny],
|
2023-06-23 02:09:45 +09:00
|
|
|
"create": [AllowAny],
|
2023-05-03 14:23:44 +09:00
|
|
|
}
|
|
|
|
|
2023-11-01 01:14:02 +09:00
|
|
|
@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)
|
|
|
|
|
2023-05-03 14:23:44 +09:00
|
|
|
def perform_create(self, serializer):
|
2023-11-13 19:52:35 +09:00
|
|
|
# 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)
|
2023-11-09 19:41:37 +09:00
|
|
|
|
|
|
|
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)
|
2023-06-23 02:09:45 +09:00
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
object = self.get_object()
|
|
|
|
instance = serializer.instance
|
2023-11-09 19:41:37 +09:00
|
|
|
done = serializer.validated_data.get("done")
|
|
|
|
if not object.done and done:
|
2023-06-23 02:09:45 +09:00
|
|
|
create_transaction(instance)
|
2023-11-09 19:41:37 +09:00
|
|
|
elif object.done and not done:
|
|
|
|
Transaction.objects.filter(post=object).delete()
|
2023-06-23 02:09:45 +09:00
|
|
|
return super().perform_update(serializer)
|