create rancher incentive plan structure - add pos device main sheba
This commit is contained in:
@@ -2,9 +2,10 @@ from rest_framework import serializers
|
||||
from apps.product import models as product_models
|
||||
from apps.authorization.api.v1 import serializers as authorize_serializers
|
||||
from apps.authentication.api.v1.serializers.serializer import OrganizationSerializer, OrganizationTypeSerializer
|
||||
from apps.product import exceptions
|
||||
|
||||
|
||||
class ProductCategorySerializer(serializers.ModelSerializer):
|
||||
class ProductCategorySerializer(serializers.ModelSerializer): # noqa
|
||||
class Meta:
|
||||
model = product_models.ProductCategory
|
||||
fields = '__all__'
|
||||
@@ -161,3 +162,20 @@ class IncentivePlanRancherSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.IncentivePlanRancher
|
||||
fields = '__all__'
|
||||
|
||||
def validate(self, attrs):
|
||||
""" validate incentive plans of a rancher """
|
||||
|
||||
rancher_plans_data = self.context['request'].data['data']
|
||||
|
||||
# check if rancher with same plan & livestock exists
|
||||
if not self.instance:
|
||||
for plan in rancher_plans_data:
|
||||
if product_models.IncentivePlanRancher.objects.filter(
|
||||
plan_id=plan['plan'],
|
||||
rancher_id=plan['rancher'],
|
||||
livestock_type_id=plan['livestock_type'],
|
||||
).exists():
|
||||
raise exceptions.RancherIncentivePlanExists()
|
||||
|
||||
return attrs
|
||||
|
||||
@@ -446,3 +446,15 @@ class IncentivePlanViewSet(viewsets.ModelViewSet, SoftDeleteMixin, DynamicSearch
|
||||
class IncentivePlanRancherViewSet(viewsets.ModelViewSet, SoftDeleteMixin, DynamicSearchMixin):
|
||||
queryset = product_models.IncentivePlanRancher.objects.all()
|
||||
serializer_class = product_serializers.IncentivePlanRancherSerializer
|
||||
search_fields = []
|
||||
|
||||
@transaction.atomic
|
||||
def create(self, request, *args, **kwargs):
|
||||
""" create rancher incentive plans by livestock type count """
|
||||
|
||||
serializer = self.serializer_class(
|
||||
data=request.data['data'], many=True, context={'request': request}
|
||||
)
|
||||
if serializer.is_valid(raise_exception=True):
|
||||
serializer.save()
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
|
||||
Reference in New Issue
Block a user