change bug of permissions list
This commit is contained in:
@@ -8,7 +8,7 @@ from rest_framework import status
|
||||
from django.db import transaction
|
||||
|
||||
|
||||
def trash(queryset, pk):
|
||||
def trash(queryset, pk): # noqa
|
||||
""" sent object to trash """
|
||||
obj = queryset.get(id=pk)
|
||||
obj.trash = True
|
||||
@@ -90,4 +90,156 @@ class ReferenceProductViewSet(viewsets.ModelViewSet):
|
||||
delete(self.queryset, pk)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
class AttributeViewSet(viewsets.ModelViewSet):
|
||||
""" attributes of reference product """
|
||||
|
||||
queryset = product_models.Attribute.objects.all()
|
||||
serializer_class = product_serializers.AttributeSerializer
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent attribute to trash """
|
||||
try:
|
||||
trash(self.queryset, pk)
|
||||
except APIException as e:
|
||||
return Response(e, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=True,
|
||||
url_name='delete',
|
||||
url_path='delete',
|
||||
name='delete'
|
||||
)
|
||||
@transaction.atomic
|
||||
def delete(self, request, pk=None):
|
||||
""" Full delete of attribute object """
|
||||
try:
|
||||
delete(self.queryset, pk)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
class AttributeValueViewSet(viewsets.ModelViewSet):
|
||||
""" apis for attribute values of child products """ # noqa
|
||||
|
||||
queryset = product_models.AttributeValue.objects.all()
|
||||
serializer_class = product_serializers.AttributeValueSerializer
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent attribute value to trash """
|
||||
try:
|
||||
trash(self.queryset, pk)
|
||||
except APIException as e:
|
||||
return Response(e, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=True,
|
||||
url_name='delete',
|
||||
url_path='delete',
|
||||
name='delete'
|
||||
)
|
||||
@transaction.atomic
|
||||
def delete(self, request, pk=None):
|
||||
""" Full delete of attribute value object """
|
||||
try:
|
||||
delete(self.queryset, pk)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
class BrokerViewSet(viewsets.ModelViewSet):
|
||||
""" apis of product brokers """ # noqa
|
||||
|
||||
queryset = product_models.Broker.objects.all()
|
||||
serializer_class = product_serializers.BrokerSerializer
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent broker to trash """
|
||||
try:
|
||||
trash(self.queryset, pk)
|
||||
except APIException as e:
|
||||
return Response(e, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=True,
|
||||
url_name='delete',
|
||||
url_path='delete',
|
||||
name='delete'
|
||||
)
|
||||
@transaction.atomic
|
||||
def delete(self, request, pk=None):
|
||||
""" Full delete of broker object """
|
||||
try:
|
||||
delete(self.queryset, pk)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
class SaleUnitViewSet(viewsets.ModelViewSet):
|
||||
""" apis of unit of sale for products """ # noqa
|
||||
|
||||
queryset = product_models.SaleUnit.objects.all()
|
||||
serializer_class = product_serializers.SaleUnitSerializer
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent unit sale to trash """
|
||||
try:
|
||||
trash(self.queryset, pk)
|
||||
except APIException as e:
|
||||
return Response(e, status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=True,
|
||||
url_name='delete',
|
||||
url_path='delete',
|
||||
name='delete'
|
||||
)
|
||||
@transaction.atomic
|
||||
def delete(self, request, pk=None):
|
||||
""" Full delete of unit sale object """
|
||||
try:
|
||||
delete(self.queryset, pk)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from rest_framework import serializers
|
||||
from apps.product import models as product_models
|
||||
from apps.authorization.api.v1 import serializers as authorize_serializers
|
||||
|
||||
|
||||
class ReferenceProductSerializer(serializers.ModelSerializer):
|
||||
@@ -25,3 +26,77 @@ class ProductSerializer(serializers.ModelSerializer):
|
||||
representation['reference'] = ReferenceProductSerializer(instance.reference).data
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
class AttributeSerializer(serializers.ModelSerializer):
|
||||
""" serialize attributes of reference product """
|
||||
|
||||
class Meta:
|
||||
model = product_models.Attribute
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if instance.reference_product:
|
||||
representation['reference_product'] = ReferenceProductSerializer(
|
||||
instance.reference_product
|
||||
).data
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
class AttributeValueSerializer(serializers.ModelSerializer):
|
||||
""" serialize attribute values for child products """
|
||||
|
||||
class Meta:
|
||||
model = product_models.AttributeValue
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom output """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
if instance.product:
|
||||
representation['product'] = ProductSerializer(instance.product).data
|
||||
if instance.attribute:
|
||||
representation['attribute'] = AttributeSerializer(instance.attribute).data
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
class BrokerSerializer(serializers.ModelSerializer):
|
||||
""" serialize product broker """
|
||||
|
||||
class Meta:
|
||||
model = product_models.Broker
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if instance.reference_product:
|
||||
representation['reference_product'] = ReferenceProductSerializer(
|
||||
instance.reference_product
|
||||
).data
|
||||
if instance.organization_relations:
|
||||
representation['organization_relations'] = authorize_serializers.UserRelationSerializer(
|
||||
instance.organization_relations
|
||||
).data
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
class SaleUnitSerializer(serializers.ModelSerializer):
|
||||
""" serialize unit of products for sale """
|
||||
|
||||
class Meta:
|
||||
model = product_models.SaleUnit
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if instance.reference_product:
|
||||
representation['reference_product'] = ReferenceProductSerializer(
|
||||
instance.reference_product
|
||||
).data
|
||||
|
||||
return representation
|
||||
|
||||
@@ -5,6 +5,10 @@ from django.urls import path, include
|
||||
router = DefaultRouter()
|
||||
router.register(r'product', api_views.ProductViewSet, basename='product')
|
||||
router.register(r'reference', api_views.ReferenceProductViewSet, basename='reference')
|
||||
router.register(r'attribute', api_views.AttributeViewSet, basename='attribute')
|
||||
router.register(r'attribute_value', api_views.AttributeValueViewSet, basename='attribute_value')
|
||||
router.register(r'broker', api_views.BrokerViewSet, basename='broker')
|
||||
router.register(r'sale_unit', api_views.SaleUnitViewSet, basename='sale_unit')
|
||||
|
||||
urlpatterns = [
|
||||
path('v1/', include(router.urls))
|
||||
|
||||
Reference in New Issue
Block a user