Coverage for apps/suppliers/views.py: 78%
21 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.exceptions import ValidationError
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.viewsets import GenericViewSet
10from apps.users.mixins import AdminMixin
12from .filters import SupplierFilter
13from .serializers import SupplierSerializer
16class AdminSupplierViewSet(AdminMixin, GenericViewSet, Create, List, Detail, Update, Delete):
17 """
18 Supplier ViewSet
19 """
21 serializer_class = SupplierSerializer
22 filterset_class = SupplierFilter
23 search_fields = ["name"]
25 def destroy(self, request, *args, **kwargs):
26 supplier = self.get_object()
27 try:
28 return super().destroy(request)
29 except ProtectedError:
30 return ValidationError(
31 {"detail": f"No se puede eliminar el proveedor {supplier.name} porque esta referenciado en ordenes"}
32 )