remove variation coeffisiont sale unti - change structure
This commit is contained in:
0
apps/product/web/api/v1/viewsets/__init__.py
Normal file
0
apps/product/web/api/v1/viewsets/__init__.py
Normal file
819
apps/product/web/api/v1/viewsets/product_api.py
Normal file
819
apps/product/web/api/v1/viewsets/product_api.py
Normal file
@@ -0,0 +1,819 @@
|
||||
import datetime
|
||||
|
||||
from apps.product.web.api.v1.serializers import product_serializers as product_serializers
|
||||
from apps.product.exceptions import QuotaExpiredTimeException
|
||||
from apps.product.services import get_products_in_warehouse
|
||||
from common.helpers import get_organization_by_user
|
||||
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 common.tools import CustomOperations
|
||||
from rest_framework import viewsets
|
||||
from rest_framework import status
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
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
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
""" custom list view """ #
|
||||
|
||||
queryset = self.filter_queryset(self.get_queryset().order_by('-create_date')) # noqa
|
||||
|
||||
page = self.paginate_queryset(queryset)
|
||||
if page is not None:
|
||||
serializer = self.get_serializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
serializer = self.get_serializer(queryset, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
@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=['get'],
|
||||
detail=False,
|
||||
url_path='active_plans',
|
||||
url_name='active_plans',
|
||||
name='active_plans'
|
||||
)
|
||||
@transaction.atomic
|
||||
def active_plans(self, request):
|
||||
""" return active incentive plans """
|
||||
|
||||
today = datetime.datetime.now().date()
|
||||
user_relations = product_models.UserRelations.objects.filter(user=request.user).first()
|
||||
incentive_plans = user_relations.incentive_plans.filter(
|
||||
Q(is_time_unlimited=False) |
|
||||
Q(start_date_limit__lte=today, end_date_limit__gte=today)
|
||||
)
|
||||
|
||||
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):
|
||||
""" custom create quota """
|
||||
|
||||
# get user relations data like organization
|
||||
user_relation = request.user.user_relation.all().first()
|
||||
|
||||
# add user relation to data
|
||||
request.data['registerer_organization'] = user_relation.organization.id
|
||||
|
||||
# create quota
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
quota = serializer.save()
|
||||
quota.remaining_weight = quota.quota_weight
|
||||
|
||||
# create incentive plan
|
||||
plans_list = []
|
||||
if 'incentive_plan_data' in request.data.keys():
|
||||
for plan in request.data['incentive_plan_data']:
|
||||
plan.update({'quota': quota.id})
|
||||
incentive_plan = CustomOperations().custom_create(
|
||||
request=request,
|
||||
view=QuotaIncentiveAssignmentViewSet(),
|
||||
data_key='incentive_plan_data',
|
||||
data=plan
|
||||
)
|
||||
plans_list.append(incentive_plan)
|
||||
|
||||
# create product price attributes for quota
|
||||
attributes_value_list = []
|
||||
if 'price_attributes_data' in request.data.keys():
|
||||
for attr in request.data['price_attributes_data']:
|
||||
attr.update({'quota': quota.id})
|
||||
attributes = CustomOperations().custom_create(
|
||||
request=request,
|
||||
view=AttributeValueViewSet(),
|
||||
data=attr
|
||||
)
|
||||
attributes_value_list.append(attributes)
|
||||
# create product broker values for quota
|
||||
broker_data_list = []
|
||||
if 'broker_data' in request.data.keys():
|
||||
for broker in request.data['broker_data']:
|
||||
broker.update({'quota': quota.id})
|
||||
broker_value = CustomOperations().custom_create(
|
||||
request=request,
|
||||
view=QuotaBrokerValueViewSet(),
|
||||
data=broker
|
||||
)
|
||||
broker_data_list.append(broker_value)
|
||||
|
||||
# create livestock allocations to quota
|
||||
allocations_list = []
|
||||
if 'livestock_allocation_data' in request.data.keys():
|
||||
for ls_alloc in request.data['livestock_allocation_data']:
|
||||
ls_alloc.update({'quota': quota.id})
|
||||
allocations = CustomOperations().custom_create(
|
||||
request=request,
|
||||
view=QuotaLiveStockAllocationViewSet(),
|
||||
data=ls_alloc
|
||||
)
|
||||
allocations_list.append(allocations)
|
||||
# create livestock age limits for quota
|
||||
livestock_age_limits = []
|
||||
if 'livestock_age_limitations' in request.data.keys():
|
||||
for age_limit in request.data['livestock_age_limitations']:
|
||||
age_limit.update({'quota': quota.id})
|
||||
age_limit_creation_object = CustomOperations().custom_create(
|
||||
request=request,
|
||||
view=QuotaLiveStockAgeLimitation(),
|
||||
data=age_limit
|
||||
)
|
||||
livestock_age_limits.append(age_limit_creation_object)
|
||||
|
||||
data = {
|
||||
'quota': serializer.data,
|
||||
'incentive_plan': plans_list, # noqa
|
||||
'attribute_values': attributes_value_list,
|
||||
'broker_values': broker_data_list,
|
||||
'live_stock_allocations': allocations_list,
|
||||
'livestock_age_limitations': livestock_age_limits
|
||||
}
|
||||
|
||||
# call save method to generate id & calculate quota final price
|
||||
quota.save(calculate_final_price=True)
|
||||
return Response(data, status=status.HTTP_201_CREATED)
|
||||
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
@transaction.atomic
|
||||
def update(self, request, pk=None, *args, **kwargs):
|
||||
# get user relations data like organization
|
||||
user_relation = request.user.user_relation.all().first()
|
||||
|
||||
# add user relation to data
|
||||
request.data['registerer_organization'] = user_relation.organization.id
|
||||
|
||||
# create quota
|
||||
serializer = self.serializer_class(data=request.data, instance=self.get_object(), partial=True)
|
||||
if serializer.is_valid():
|
||||
quota = serializer.save()
|
||||
|
||||
# create incentive plan
|
||||
plans_list = []
|
||||
if 'incentive_plan_data' in request.data.keys():
|
||||
for plan in request.data['incentive_plan_data']:
|
||||
plan.update({'quota': quota.id})
|
||||
incentive_plan = CustomOperations().custom_update(
|
||||
request=request,
|
||||
view=QuotaIncentiveAssignmentViewSet(),
|
||||
data_key='incentive_plan_data',
|
||||
obj_id=plan['id'],
|
||||
data=plan
|
||||
)
|
||||
plans_list.append(incentive_plan)
|
||||
|
||||
# create product price attributes for quota
|
||||
attributes_value_list = [] # noqa
|
||||
if 'price_attributes_data' in request.data.keys():
|
||||
for attr in request.data['price_attributes_data']:
|
||||
attr.update({'quota': quota.id})
|
||||
attributes = CustomOperations().custom_update(
|
||||
request=request,
|
||||
view=AttributeValueViewSet(),
|
||||
obj_id=attr['id'],
|
||||
data=attr
|
||||
)
|
||||
attributes_value_list.append(attributes)
|
||||
# create product broker values for quota
|
||||
broker_data_list = []
|
||||
if 'broker_data' in request.data.keys():
|
||||
for broker in request.data['broker_data']:
|
||||
broker.update({'quota': quota.id})
|
||||
broker_value = CustomOperations().custom_update(
|
||||
request=request,
|
||||
view=QuotaBrokerValueViewSet(),
|
||||
obj_id=broker['id'],
|
||||
data=broker
|
||||
)
|
||||
broker_data_list.append(broker_value)
|
||||
|
||||
# create livestock allocations to quota
|
||||
allocations_list = []
|
||||
if 'livestock_allocation_data' in request.data.keys():
|
||||
for ls_alloc in request.data['livestock_allocation_data']:
|
||||
ls_alloc.update({'quota': quota.id})
|
||||
allocations = CustomOperations().custom_update(
|
||||
request=request,
|
||||
view=QuotaLiveStockAllocationViewSet(),
|
||||
obj_id=ls_alloc['id'],
|
||||
data=ls_alloc
|
||||
)
|
||||
allocations_list.append(allocations)
|
||||
# create livestock age limits for quota
|
||||
livestock_age_limits = []
|
||||
if 'livestock_age_limitations' in request.data.keys():
|
||||
for age_limit in request.data['livestock_age_limitations']:
|
||||
age_limit.update({'quota': quota.id})
|
||||
age_limit_creation_object = CustomOperations().custom_update(
|
||||
request=request,
|
||||
view=QuotaLiveStockAgeLimitation(),
|
||||
obj_id=age_limit['id'],
|
||||
data=age_limit
|
||||
)
|
||||
livestock_age_limits.append(age_limit_creation_object)
|
||||
|
||||
data = {
|
||||
'quota': serializer.data,
|
||||
'incentive_plan': plans_list, # noqa
|
||||
'attribute_values': attributes_value_list,
|
||||
'broker_values': broker_data_list,
|
||||
'live_stock_allocations': allocations_list,
|
||||
'livestock_age_limitations': livestock_age_limits
|
||||
}
|
||||
|
||||
# call save method to generate id & calculate quota final price
|
||||
quota.save(calculate_final_price=True)
|
||||
return Response(data, status=status.HTTP_201_CREATED)
|
||||
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
@action(
|
||||
methods=['patch'],
|
||||
detail=True,
|
||||
url_path='close',
|
||||
url_name='close',
|
||||
name='close'
|
||||
)
|
||||
@transaction.atomic
|
||||
def close(self, request, pk=None):
|
||||
""" to close quota """
|
||||
quota = self.get_object()
|
||||
|
||||
# check quota expired time
|
||||
if not quota.is_in_valid_time():
|
||||
raise QuotaExpiredTimeException()
|
||||
|
||||
if quota.is_closed:
|
||||
raise APIException("این سهمیه قبلا بسته شده است", status.HTTP_400_BAD_REQUEST) # noqa
|
||||
|
||||
quota.is_closed = True
|
||||
quota.closed_at = datetime.now()
|
||||
quota.save()
|
||||
|
||||
return Response(status.HTTP_200_OK)
|
||||
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=False,
|
||||
url_name='list_for_assigner',
|
||||
url_path='list_for_assigner',
|
||||
name='list_for_assigner'
|
||||
)
|
||||
def quotas_list_for_assigner(self, request):
|
||||
""" list of quotas for creator """
|
||||
|
||||
assigner = product_models.UserRelations.objects.filter(user=request.user).first()
|
||||
serializers = self.serializer_class(
|
||||
self.queryset.filter(registerer_organization=assigner.organization),
|
||||
many=True
|
||||
).data
|
||||
return Response(serializers.data, status=status.HTTP_200_OK)
|
||||
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=False,
|
||||
url_name='list_for_assigned',
|
||||
url_path='list_for_assigned',
|
||||
name='list_for_assigned'
|
||||
)
|
||||
def quotas_list_for_assigned(self, request):
|
||||
""" list of quotas for assigned organizations """
|
||||
|
||||
assigned = product_models.UserRelations.objects.filter(user=request.user).first()
|
||||
serializer = self.serializer_class(
|
||||
self.queryset.filter(assigned_organizations=assigned.organization),
|
||||
many=True
|
||||
)
|
||||
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=False,
|
||||
url_path='quotas_statistics',
|
||||
url_name='quotas_statistics',
|
||||
name='quotas_statistics'
|
||||
)
|
||||
@transaction.atomic
|
||||
def quotas_statistics_by_product(self, request):
|
||||
""" quota statistics of each product in organization warehouse """
|
||||
|
||||
product_data = []
|
||||
|
||||
organization = get_organization_by_user(request.user)
|
||||
products = get_products_in_warehouse(organization.id)
|
||||
|
||||
for product in products:
|
||||
product_data.append(product.quota_information())
|
||||
|
||||
return Response(product_data, status=status.HTTP_200_OK)
|
||||
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=True,
|
||||
url_path='quotas_information',
|
||||
url_name='quotas_information',
|
||||
name='quotas_information'
|
||||
)
|
||||
@transaction.atomic
|
||||
def quotas_information_by_product(self, request, pk=None):
|
||||
""" get quotas information of a product """
|
||||
|
||||
quotas = self.queryset.select_related('product').filter(
|
||||
product_id=pk, is_closed=False
|
||||
)
|
||||
|
||||
try:
|
||||
quota_serializer = self.serializer_class(quotas, many=True).data
|
||||
return Response(quota_serializer, status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
raise APIException(detail="data error", code=400)
|
||||
|
||||
@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): # noqa
|
||||
""" 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)
|
||||
|
||||
|
||||
class QuotaLiveStockAgeLimitation(viewsets.ModelViewSet):
|
||||
queryset = product_models.QuotaLiveStockAgeLimitation.objects.all() # noqa
|
||||
serializer_class = product_serializers.QuotaLiveStockAgeLimitationSerializer
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent quota livestock age limitation 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 age limitation 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)
|
||||
112
apps/product/web/api/v1/viewsets/quota_distribution_api.py
Normal file
112
apps/product/web/api/v1/viewsets/quota_distribution_api.py
Normal file
@@ -0,0 +1,112 @@
|
||||
from apps.product.web.api.v1.serializers import quota_distribution_serializers as distribution_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 QuotaDistributionViewSet(viewsets.ModelViewSet):
|
||||
""" quota distribution apis """
|
||||
|
||||
queryset = product_models.QuotaDistribution.objects.all()
|
||||
serializer_class = distribution_serializers.QuotaDistributionSerializer
|
||||
|
||||
@transaction.atomic
|
||||
def create(self, request, *args, **kwargs):
|
||||
""" create distribution for organizations users """
|
||||
|
||||
# get distributed assigner user
|
||||
try:
|
||||
assigner_user = product_models.UserRelations.objects.filter(
|
||||
user=request.user
|
||||
).first()
|
||||
except APIException as e:
|
||||
raise APIException("unauthorized", code=status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
request.data.update({'assigner_organization': assigner_user.organization.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 update(self, request, *args, **kwargs):
|
||||
""" Custom update of quota distribution """
|
||||
|
||||
partial = kwargs.pop('partial', False)
|
||||
instance = self.get_object()
|
||||
serializer = self.get_serializer(instance, data=request.data, partial=partial)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
self.perform_update(serializer)
|
||||
|
||||
if getattr(instance, '_prefetched_objects_cache', None):
|
||||
# If 'prefetch_related' has been applied to a queryset, we need to
|
||||
# forcibly invalidate the prefetch cache on the instance.
|
||||
instance._prefetched_objects_cache = {}
|
||||
|
||||
return Response(serializer.data)
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash',
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent quota distribution to trash """
|
||||
quota_distribution = self.get_object()
|
||||
|
||||
# check if distribution has inventory entry
|
||||
if quota_distribution.inventory_entry.exists():
|
||||
raise APIException(
|
||||
"امکان حذف این توزیع وجود ندارد. ورود به انبار برای آن ثبت شده است", # noqa
|
||||
status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
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 distribution object """
|
||||
quota_distribution = self.get_object()
|
||||
|
||||
# check if distribution has inventory entry
|
||||
if quota_distribution.inventory_entry.exists():
|
||||
raise APIException(
|
||||
"امکان حذف این توزیع وجود ندارد. ورود به انبار برای آن ثبت شده است", # noqa
|
||||
status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
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)
|
||||
Reference in New Issue
Block a user