feat: added lots of tests
This commit is contained in:
+40
-1
@@ -1,4 +1,9 @@
|
||||
from django.db.models.deletion import ProtectedError
|
||||
from django.utils.text import gettext_lazy as _
|
||||
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
from rest_framework.exceptions import ValidationError
|
||||
from rest_framework.permissions import IsAdminUser
|
||||
|
||||
from shop.api.v1.serializers import (
|
||||
ProductSerializer,
|
||||
@@ -9,6 +14,8 @@ from shop.api.v1.serializers import (
|
||||
OrderLineSerializer,
|
||||
ProviderSerializer,
|
||||
ProductBatchSerializer,
|
||||
CreateProductSerializer,
|
||||
CreateProductPriceSerializer,
|
||||
)
|
||||
from shop.models import (
|
||||
Product,
|
||||
@@ -21,15 +28,47 @@ from shop.models import (
|
||||
ProductBatch,
|
||||
)
|
||||
|
||||
from shop.api.v1.permissions import ProductPermissions, ProductPricePermissions
|
||||
|
||||
|
||||
class ProductViewSet(ModelViewSet):
|
||||
serializer_class = ProductSerializer
|
||||
queryset = Product.objects.all()
|
||||
queryset = Product.objects.prefetch_related("prices").all()
|
||||
permission_classes = (
|
||||
IsAdminUser,
|
||||
ProductPermissions,
|
||||
)
|
||||
|
||||
def get_serializer_class(self):
|
||||
if self.action == "create":
|
||||
return CreateProductSerializer
|
||||
return self.serializer_class
|
||||
|
||||
def perform_destroy(self, instance):
|
||||
try:
|
||||
super().perform_destroy(instance)
|
||||
except ProtectedError:
|
||||
raise ValidationError(_("No se puede borrar el producto"))
|
||||
|
||||
|
||||
class ProductPriceViewSet(ModelViewSet):
|
||||
serializer_class = ProductPriceSerializer
|
||||
queryset = ProductPrice.objects.all()
|
||||
permission_classes = (
|
||||
IsAdminUser,
|
||||
ProductPricePermissions,
|
||||
)
|
||||
|
||||
def get_queryset(self):
|
||||
return super().get_queryset().filter(product_id=self.kwargs.get("product_id"))
|
||||
|
||||
def get_serializer_class(self):
|
||||
if self.action == "create":
|
||||
return CreateProductPriceSerializer
|
||||
return self.serializer_class
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(product_id=self.kwargs.get("product_id"))
|
||||
|
||||
|
||||
class ProductBatchViewSet(ModelViewSet):
|
||||
|
||||
Reference in New Issue
Block a user