Coverage for apps/receptions/views.py: 72%
30 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-10-12 15:59 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-10-12 15:59 -0600
1from rest_framework.mixins import CreateModelMixin as Create
2from rest_framework.mixins import DestroyModelMixin as Delete
3from rest_framework.mixins import ListModelMixin as List
4from rest_framework.mixins import RetrieveModelMixin as Detail
5from rest_framework.validators import ValidationError
6from rest_framework.viewsets import GenericViewSet
8from apps.users.mixins import AdminMixin
9from apps.users.permissions import IsEmployee
11from .serializers import OrderReceptionCommentSerializer, OrderReceptionImageSerializer, OrderReceptionSerializer
14class AdminOrderReceptionViewSet(AdminMixin, GenericViewSet, Create, Detail, List, Delete):
15 """
16 Viewset for Order Reception
17 """
19 serializer_class = OrderReceptionSerializer
20 filterset_fields = {
21 "order": ["exact"],
22 "order__supplier": ["exact"],
23 "order__week": ["exact"],
24 "order__week__period": ["exact"],
25 }
26 search_fields = ["order__order_id"]
29class AdminOrderReceptionCommentViewSet(AdminMixin, GenericViewSet, Create, List, Delete):
30 """
31 Viewset for Order Reception Comment
32 """
34 serializer_class = OrderReceptionCommentSerializer
35 filterset_fields = {"order_reception": ["exact"]}
37 def get_queryset(self):
38 return super().get_queryset().filter(parent_comment__isnull=True)
40 def perform_create(self, serializer):
41 serializer.save(author=self.request.user)
43 def perform_destroy(self, instance):
44 if instance.author != self.request.user:
45 raise ValidationError({"detail": "Solo se pueden eliminar comentarios tuyos."})
46 if instance.children_comments.exists():
47 raise ValidationError({"detail": "No se puede eliminar un comentario con respuestas."})
48 super().perform_destroy(instance)
51class AdminOrderReceptionImageViewSet(AdminMixin, GenericViewSet, Create, List, Delete):
52 """
53 ViewSet for Order Reception Image
54 """
56 serializer_class = OrderReceptionImageSerializer
57 filterset_fields = {"order_reception": ["exact"]}
58 permission_classes = [IsEmployee]