fix organization pagination - quota distribution
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
from apps.product.web.api.v1 import serializers as product_serializers
|
||||
import datetime
|
||||
|
||||
from apps.product.web.api.v1 import product_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 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
|
||||
|
||||
|
||||
def trash(queryset, pk): # noqa
|
||||
@@ -275,6 +279,27 @@ class IncentivePlanViewSet(viewsets.ModelViewSet): # noqa
|
||||
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,
|
||||
@@ -315,7 +340,122 @@ class QuotaViewSet(viewsets.ModelViewSet): # noqa
|
||||
|
||||
@transaction.atomic
|
||||
def create(self, request, *args, **kwargs):
|
||||
pass
|
||||
""" 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.id
|
||||
|
||||
# create quota
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
quota = serializer.save()
|
||||
|
||||
# create incentive plan
|
||||
if 'incentive_plan_data' in request.data.keys():
|
||||
incentive_plan = CustomOperations().custom_create(
|
||||
request=request,
|
||||
view=QuotaIncentiveAssignmentViewSet(),
|
||||
data_key='incentive_plan_data',
|
||||
additional_data={'quota': quota.id}
|
||||
)
|
||||
else:
|
||||
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': incentive_plan,
|
||||
'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)
|
||||
|
||||
@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),
|
||||
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),
|
||||
many=True
|
||||
)
|
||||
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
@@ -461,3 +601,39 @@ class QuotaLiveStockAllocationViewSet(viewsets.ModelViewSet):
|
||||
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)
|
||||
@@ -57,8 +57,8 @@ class AttributeValueSerializer(serializers.ModelSerializer):
|
||||
""" Custom output """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
if instance.product:
|
||||
representation['product'] = ProductSerializer(instance.product).data
|
||||
if instance.quota:
|
||||
representation['quota'] = QuotaSerializer(instance.quota).data
|
||||
if instance.attribute:
|
||||
representation['attribute'] = AttributeSerializer(instance.attribute).data
|
||||
|
||||
@@ -99,7 +99,7 @@ class SaleUnitSerializer(serializers.ModelSerializer):
|
||||
return representation
|
||||
|
||||
|
||||
class IncentivePlanSerializer(serializers.ModelSerializer):
|
||||
class IncentivePlanSerializer(serializers.ModelSerializer): # noqa
|
||||
class Meta:
|
||||
model = product_models.IncentivePlan
|
||||
fields = '__all__'
|
||||
@@ -117,7 +117,7 @@ class QuotaIncentiveAssignmentSerializer(serializers.ModelSerializer):
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class QuotaBrokerValueSerializer(serializers.ModelSerializer):
|
||||
class QuotaBrokerValueSerializer(serializers.ModelSerializer): # noqa
|
||||
class Meta:
|
||||
model = product_models.QuotaBrokerValue
|
||||
fields = '__all__'
|
||||
@@ -127,3 +127,9 @@ class QuotaLiveStockAllocationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaLivestockAllocation
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class QuotaLiveStockAgeLimitationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaLiveStockAgeLimitation
|
||||
fields = '__all__'
|
||||
66
apps/product/web/api/v1/quota_distribution_api.py
Normal file
66
apps/product/web/api/v1/quota_distribution_api.py
Normal file
@@ -0,0 +1,66 @@
|
||||
from apps.product.web.api.v1 import quota_distribution_serializers as distribution_serializers
|
||||
from apps.product.web.api.v1 import product_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 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
|
||||
|
||||
|
||||
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):
|
||||
queryset = product_models.QuotaDistribution.objects.all()
|
||||
serializer_class = distribution_serializers.QuotaDistributionSerializer
|
||||
|
||||
@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 distribution 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 distribution 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)
|
||||
|
||||
|
||||
23
apps/product/web/api/v1/quota_distribution_serializers.py
Normal file
23
apps/product/web/api/v1/quota_distribution_serializers.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from rest_framework import serializers
|
||||
from apps.product import models as product_models
|
||||
from apps.product.web.api.v1.product_serializers import QuotaSerializer
|
||||
|
||||
|
||||
class QuotaDistributionSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaDistribution
|
||||
fields = '__all__'
|
||||
extra_kwargs = {
|
||||
'assigner_organization': {
|
||||
'required': False
|
||||
}
|
||||
}
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom output of serializer """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
if instance.quota:
|
||||
representation['quota'] = QuotaSerializer(instance.quota).data
|
||||
|
||||
return representation
|
||||
@@ -1,4 +1,5 @@
|
||||
from apps.product.web.api.v1 import api as api_views
|
||||
from apps.product.web.api.v1 import quota_distribution_api as distribution_apis
|
||||
from apps.product.web.api.v1 import product_api as api_views
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from django.urls import path, include
|
||||
|
||||
@@ -10,6 +11,8 @@ router.register(r'attribute_value', api_views.AttributeValueViewSet, basename='a
|
||||
router.register(r'broker', api_views.BrokerViewSet, basename='broker')
|
||||
router.register(r'sale_unit', api_views.SaleUnitViewSet, basename='sale_unit')
|
||||
router.register(r'incentive_plan', api_views.IncentivePlanViewSet, basename='incentive_plan')
|
||||
router.register(r'quota', api_views.QuotaViewSet, basename='quota')
|
||||
router.register(r'quota_distribution', distribution_apis.QuotaDistributionViewSet, basename='quota_distribution')
|
||||
|
||||
urlpatterns = [
|
||||
path('v1/', include(router.urls))
|
||||
|
||||
Reference in New Issue
Block a user