Coverage for apps/orders/views.py: 82%
47 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-10-27 13:33 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-10-27 13:33 -0600
1from rest_framework import status
2from rest_framework.decorators import action
3from rest_framework.mixins import CreateModelMixin as Create
4from rest_framework.mixins import DestroyModelMixin as Delete
5from rest_framework.mixins import ListModelMixin as List
6from rest_framework.mixins import RetrieveModelMixin as Detail
7from rest_framework.mixins import UpdateModelMixin as Update
8from rest_framework.response import Response
9from rest_framework.validators import ValidationError
10from rest_framework.viewsets import GenericViewSet
12from apps.users.mixins import AdminMixin
13from apps.users.permissions import IsEmployee
15from .filters import OrderFilter
16from .serializers import (
17 OrderCommentSerializer,
18 OrderIconSerializer,
19 OrderItemSerializer,
20 OrderItemSizeSerializer,
21 OrderSerializer,
22 OrderSplitSerializer,
23)
26class AdminOrderViewSet(AdminMixin, GenericViewSet, Create, List, Detail, Update, Delete):
27 """
28 ViewSet for Order
29 """
31 serializer_class = OrderSerializer
32 filterset_class = OrderFilter
33 search_fields = ["order_id"]
35 @action(methods=["POST"], detail=True)
36 def split(self, request, *args, **kwargs):
37 order = self.get_object()
38 request.data["order"] = order.pk
39 serializer = OrderSplitSerializer(data=request.data)
40 serializer.is_valid(raise_exception=True)
41 new_order = serializer.save()
42 serializer = OrderSerializer(new_order)
43 return Response(serializer.data, status=status.HTTP_201_CREATED)
46class AdminOrderItemViewSet(AdminMixin, GenericViewSet, Update, Delete):
47 """
48 ViewSet for OrderItem
49 """
51 serializer_class = OrderItemSerializer
54class AdminOrderItemSizeViewSet(AdminMixin, GenericViewSet, Update, Delete):
55 """
56 ViewSet for OrderItemSize
57 """
59 serializer_class = OrderItemSizeSerializer
62class AdminOrderIconViewset(AdminMixin, GenericViewSet, Update):
63 """
64 ViewSet for OrderIcon
65 """
67 serializer_class = OrderIconSerializer
70class AdminOrderCommentViewSet(AdminMixin, GenericViewSet, Create, List, Delete):
71 """
72 Viewset for Order Comment
73 """
75 serializer_class = OrderCommentSerializer
76 filterset_fields = {"order": ["exact"]}
77 permission_classes = [IsEmployee]
79 def get_queryset(self):
80 return super().get_queryset().filter(parent_comment__isnull=True)
82 def perform_create(self, serializer):
83 serializer.save(author=self.request.user)
85 def perform_destroy(self, instance):
86 if instance.author != self.request.user:
87 raise ValidationError({"detail": "Solo se pueden eliminar comentarios tuyos."})
88 if instance.children_comments.exists():
89 raise ValidationError({"detail": "No se puede eliminar un comentario con respuestas."})
90 super().perform_destroy(instance)