from apps.product.web.api.v1 import serializers as product_serializers from rest_framework.exceptions import APIException from apps.product import models as product_models from rest_framework.response import Response from rest_framework.decorators import action from rest_framework import viewsets from rest_framework import status from django.db import transaction def trash(queryset, pk): # noqa """ sent object to trash """ obj = queryset.get(id=pk) obj.trash = True obj.save() def delete(queryset, pk): """ full delete object """ obj = queryset.get(id=pk) obj.delete() class ProductCategoryViewSet(viewsets.ModelViewSet): queryset = product_models.ProductCategory.objects.all() serializer_class = product_serializers.ProductCategorySerializer @action( methods=['put'], detail=True, url_path='trash', url_name='trash', name='trash', ) @transaction.atomic def trash(self, request, pk=None): """ Sent product 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 product 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 ProductViewSet(viewsets.ModelViewSet): queryset = product_models.Product.objects.all() serializer_class = product_serializers.ProductSerializer @action( methods=['put'], detail=True, url_path='trash', url_name='trash', name='trash', ) @transaction.atomic def trash(self, request, pk=None): """ Sent product 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 product 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 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) class IncentivePlanViewSet(viewsets.ModelViewSet): # noqa """ apis for incentive plan """ queryset = product_models.IncentivePlan.objects.all() serializer_class = product_serializers.IncentivePlanSerializer @transaction.atomic def create(self, request, *args, **kwargs): """ custom incentive plan create object """ # set incentive plans with user relations like organization user_relation = product_models.UserRelations.objects.filter(user=request.user).first() request.data['registering_organization'] = user_relation.id serializer = self.serializer_class(data=request.data) if serializer.is_valid(raise_exception=True): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN) @transaction.atomic def list(self, request, *args, **kwargs): """ get incentive plans by user relations like organization """ user_relations = product_models.UserRelations.objects.filter(user=request.user).first() incentive_plans = user_relations.incentive_plans.all() serializer = self.serializer_class(incentive_plans, many=True) return Response(serializer.data, status=status.HTTP_200_OK) @action( methods=['put'], detail=True, url_path='trash', url_name='trash', name='trash', ) @transaction.atomic def trash(self, request, pk=None): """ Sent incentive plan 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 incentive plan 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 QuotaViewSet(viewsets.ModelViewSet): # noqa """ apis for product quota """ queryset = product_models.Quota.objects.all() serializer_class = product_serializers.QuotaSerializer @transaction.atomic def create(self, request, *args, **kwargs): pass @action( methods=['put'], detail=True, url_path='trash', url_name='trash', name='trash', ) @transaction.atomic def trash(self, request, pk=None): """ Sent quota 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 quota 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 QuotaIncentiveAssignmentViewSet(viewsets.ModelViewSet): """ apis for incentive assignment """ queryset = product_models.QuotaIncentiveAssignment.objects.all() serializer_class = product_serializers.QuotaIncentiveAssignmentSerializer @action( methods=['put'], detail=True, url_path='trash', url_name='trash', name='trash', ) @transaction.atomic def trash(self, request, pk=None): """ Sent quota incentive assignment 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 quota incentive assignment 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 QuotaBrokerValueViewSet(viewsets.ModelViewSet): # noqa """ apis for quota broker value """ queryset = product_models.QuotaBrokerValue.objects.all() serializer_class = product_serializers.QuotaBrokerValueSerializer @action( methods=['put'], detail=True, url_path='trash', url_name='trash', name='trash', ) @transaction.atomic def trash(self, request, pk=None): """ Sent quota broker 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 quota broker 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 QuotaLiveStockAllocationViewSet(viewsets.ModelViewSet): """ apis for quota livestock allocation """ queryset = product_models.QuotaLivestockAllocation.objects.all() serializer_class = product_serializers.QuotaLiveStockAllocationSerializer @action( methods=['put'], detail=True, url_path='trash', url_name='trash', name='trash', ) @transaction.atomic def trash(self, request, pk=None): """ Sent quota livestock allocation 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 quota livestock allocation 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)