feat: added brand model
This commit is contained in:
@@ -5,7 +5,7 @@ from shop.models import Product
|
||||
|
||||
|
||||
class ProductFilter(django_filters.FilterSet):
|
||||
tags = django_filters.BaseInFilter(field_name='tags', label=_('Etiquetas'))
|
||||
tags = django_filters.BaseInFilter(field_name="tags", label=_("Etiquetas"))
|
||||
|
||||
class Meta:
|
||||
model = Product
|
||||
|
||||
@@ -9,6 +9,7 @@ from shop.api.v1.viewsets import (
|
||||
ProviderViewSet,
|
||||
ProductBatchViewSet,
|
||||
TagViewSet,
|
||||
BrandViewSet,
|
||||
)
|
||||
|
||||
|
||||
@@ -19,6 +20,7 @@ router = DefaultRouter()
|
||||
|
||||
router.register("taxes", TaxViewSet)
|
||||
router.register("tags", TagViewSet)
|
||||
router.register("brands", BrandViewSet)
|
||||
router.register("customers", CustomerViewSet)
|
||||
router.register("providers", ProviderViewSet)
|
||||
router.register("products", ProductViewSet)
|
||||
|
||||
@@ -12,6 +12,7 @@ from shop.models import (
|
||||
ProductBatch,
|
||||
Provider,
|
||||
Tag,
|
||||
Brand,
|
||||
)
|
||||
|
||||
|
||||
@@ -111,7 +112,7 @@ class CreateProductSerializer(serializers.ModelSerializer):
|
||||
url=validated_data.get("url"),
|
||||
)
|
||||
|
||||
tags = validated_data.get('tags')
|
||||
tags = validated_data.get("tags")
|
||||
for tag in tags:
|
||||
instance.tags.add(tag)
|
||||
|
||||
@@ -201,6 +202,15 @@ class ProviderSerializer(serializers.ModelSerializer):
|
||||
)
|
||||
|
||||
|
||||
class BrandSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Brand
|
||||
fields = (
|
||||
"id",
|
||||
"name",
|
||||
)
|
||||
|
||||
|
||||
class OrderSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Order
|
||||
@@ -250,5 +260,3 @@ class OrderLineSerializer(serializers.ModelSerializer):
|
||||
"taxes",
|
||||
"tax_value",
|
||||
)
|
||||
|
||||
|
||||
|
||||
+23
-4
@@ -22,7 +22,9 @@ from shop.api.v1.serializers import (
|
||||
CreateProductSerializer,
|
||||
CreateProductPriceSerializer,
|
||||
TagSerializer,
|
||||
ListProductSerializer, UpdateProductSerializer,
|
||||
ListProductSerializer,
|
||||
UpdateProductSerializer,
|
||||
BrandSerializer,
|
||||
)
|
||||
from shop.models import (
|
||||
Product,
|
||||
@@ -34,6 +36,7 @@ from shop.models import (
|
||||
Provider,
|
||||
ProductBatch,
|
||||
Tag,
|
||||
Brand,
|
||||
)
|
||||
|
||||
from shop.api.v1.permissions import ProductPermissions, ProductPricePermissions
|
||||
@@ -41,12 +44,17 @@ from shop.api.v1.permissions import ProductPermissions, ProductPricePermissions
|
||||
|
||||
class ProductViewSet(ModelViewSet):
|
||||
serializer_class = ProductSerializer
|
||||
queryset = Product.objects.prefetch_related("prices").prefetch_related("tags").prefetch_related("images").all()
|
||||
queryset = (
|
||||
Product.objects.prefetch_related("prices")
|
||||
.prefetch_related("tags")
|
||||
.prefetch_related("images")
|
||||
.all()
|
||||
)
|
||||
permission_classes = (
|
||||
IsAdminUser,
|
||||
ProductPermissions,
|
||||
)
|
||||
search_fields = ('name', )
|
||||
search_fields = ("name",)
|
||||
filterset_class = ProductFilter
|
||||
|
||||
def get_serializer_class(self):
|
||||
@@ -54,7 +62,7 @@ class ProductViewSet(ModelViewSet):
|
||||
return CreateProductSerializer
|
||||
elif self.action == "list":
|
||||
return ListProductSerializer
|
||||
elif self.action in ('update', 'partial_update'):
|
||||
elif self.action in ("update", "partial_update"):
|
||||
return UpdateProductSerializer
|
||||
return self.serializer_class
|
||||
|
||||
@@ -88,21 +96,25 @@ class ProductPriceViewSet(ModelViewSet):
|
||||
class ProductBatchViewSet(ModelViewSet):
|
||||
serializer_class = ProductBatchSerializer
|
||||
queryset = ProductBatch.objects.all()
|
||||
permission_classes = (IsAdminUser,)
|
||||
|
||||
|
||||
class CustomerViewSet(ModelViewSet):
|
||||
serializer_class = CustomerSerializer
|
||||
queryset = Customer.objects.all()
|
||||
permission_classes = (IsAdminUser,)
|
||||
|
||||
|
||||
class ProviderViewSet(ModelViewSet):
|
||||
serializer_class = ProviderSerializer
|
||||
queryset = Provider.objects.all()
|
||||
permission_classes = (IsAdminUser,)
|
||||
|
||||
|
||||
class TaxViewSet(ModelViewSet):
|
||||
serializer_class = TaxSerializer
|
||||
queryset = Tax.objects.all()
|
||||
permission_classes = (IsAdminUser,)
|
||||
|
||||
|
||||
class OrderViewSet(ModelViewSet):
|
||||
@@ -160,3 +172,10 @@ class OrderLineViewSet(ModelViewSet):
|
||||
class TagViewSet(ModelViewSet):
|
||||
serializer_class = TagSerializer
|
||||
queryset = Tag.objects.all()
|
||||
permission_classes = (IsAdminUser,)
|
||||
|
||||
|
||||
class BrandViewSet(ModelViewSet):
|
||||
serializer_class = BrandSerializer
|
||||
queryset = Brand.objects.all()
|
||||
permission_classes = (IsAdminUser,)
|
||||
|
||||
Reference in New Issue
Block a user