Coverage for apps/catalogues/views.py: 79%
41 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 django.db.models import ProtectedError
2from rest_framework.decorators import action
3from rest_framework.exceptions import ValidationError
4from rest_framework.mixins import CreateModelMixin as Create
5from rest_framework.mixins import DestroyModelMixin as Delete
6from rest_framework.mixins import ListModelMixin as List
7from rest_framework.mixins import RetrieveModelMixin as Detail
8from rest_framework.mixins import UpdateModelMixin as Update
9from rest_framework.response import Response
10from rest_framework.viewsets import GenericViewSet
12from apps.users.mixins import AdminMixin
14from .serializers import (
15 BulkPeriodCreateSerializer,
16 ColorSerializer,
17 MaterialSerializer,
18 PeriodSerializer,
19 SizeCategorySerializer,
20 SoleSerializer,
21)
24class AdminPeriodViewSet(AdminMixin, GenericViewSet, Create, List, Detail, Update, Delete):
25 """
26 Period ViewSet
27 """
29 serializer_class = PeriodSerializer
30 filterset_fields = {"start_date": ["gte", "year"], "end_date": ["lte"]}
31 search_fields = ["name"]
33 @action(methods=["GET"], detail=False)
34 def current(self, request, *args, **kwargs):
35 period = self.serializer_class.Meta.model.objects.current()
36 serializer = self.serializer_class(period)
37 return Response(serializer.data)
39 @action(methods=["POST"], detail=False, url_path="bulkcreate")
40 def bulk_create(self, request, *args, **kwars):
41 serializer = BulkPeriodCreateSerializer(data=request.data)
42 serializer.is_valid(raise_exception=True)
43 self.perform_create(serializer)
44 headers = self.get_success_headers(serializer.data)
45 return Response(serializer.data, headers=headers)
48class AdminSizeCategoryViewSet(AdminMixin, GenericViewSet, Create, List, Detail, Update, Delete):
49 """
50 Size Category ViewSet
51 """
53 serializer_class = SizeCategorySerializer
55 def destroy(self, request, *args, **kwargs):
56 try:
57 return super().destroy(request)
58 except ProtectedError as e:
59 raise ValidationError(
60 {
61 "error": "ProtectedError",
62 "object": self.serializer_class(self.get_object()).data,
63 "dependets": [
64 {"random_slug": dependent.pk, "name": dependent, "type": type(dependent)._meta.verbose_name}
65 for dependent in e.protected_objects
66 ],
67 }
68 )
71class AdminColorViewSet(AdminMixin, GenericViewSet, Create, List, Update, Delete):
72 """
73 Color ViewSet
74 """
76 serializer_class = ColorSerializer
79class AdminMaterialViewSet(AdminMixin, GenericViewSet, Create, List, Update, Delete):
80 """
81 Material ViewSet
82 """
84 serializer_class = MaterialSerializer
87class AdminSoleViewSet(AdminMixin, GenericViewSet, Create, List, Update, Delete):
88 """
89 Sole ViewSet
90 """
92 serializer_class = SoleSerializer