list of rancher plans api by rancher id - complete

This commit is contained in:
2025-09-27 16:22:28 +03:30
parent f6f8f4c669
commit 973d1319da
2 changed files with 31 additions and 2 deletions

View File

@@ -1,10 +1,12 @@
from apps.herd.web.api.v1.serializers import HerdSerializer, RancherSerializer
from apps.livestock.web.api.v1.serializers import LiveStockSerializer
from apps.product.web.api.v1.serializers import product_serializers
from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
from apps.core.mixins.search_mixin import DynamicSearchMixin
from apps.authentication.api.v1.api import UserViewSet
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
@@ -14,7 +16,7 @@ from django.db import transaction
from rest_framework import status
class HerdViewSet(viewsets.ModelViewSet, SoftDeleteMixin):
class HerdViewSet(SoftDeleteMixin, viewsets.ModelViewSet):
""" Herd ViewSet """
queryset = Herd.objects.all()
serializer_class = HerdSerializer
@@ -116,7 +118,7 @@ class HerdViewSet(viewsets.ModelViewSet, SoftDeleteMixin):
return self.get_paginated_response(serializer.data)
class RancherViewSet(viewsets.ModelViewSet, DynamicSearchMixin, SoftDeleteMixin):
class RancherViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSearchMixin):
queryset = Rancher.objects.all()
serializer_class = RancherSerializer
search_fields = [
@@ -159,3 +161,20 @@ class RancherViewSet(viewsets.ModelViewSet, DynamicSearchMixin, SoftDeleteMixin)
if page is not None:
serializer = HerdSerializer(page, many=True)
return self.get_paginated_response(serializer.data)
@action(
methods=['get'],
detail=True,
url_path='rancher_plans',
url_name='rancher_plans',
name='rancher_plans'
)
def rancher_incentive_plans(self, request, pk=None):
""" return list of rancher plans with information """
rancher = self.get_object() # rancher object
page = self.paginate_queryset(rancher.plans.all())
if page is not None:
serializer = product_serializers.IncentivePlanRancherSerializer(page, many=True)
return self.get_paginated_response(serializer.data)

View File

@@ -179,3 +179,13 @@ class IncentivePlanRancherSerializer(serializers.ModelSerializer):
raise exceptions.RancherIncentivePlanExists()
return attrs
def to_representation(self, instance: product_models.IncentivePlanRancher):
representations = super().to_representation(instance)
representations['plan_name'] = instance.plan.name
representations['rancher_id'] = instance.rancher.id
representations['livestock_type_name'] = instance.livestock_type.name
representations['allowed_quantity'] = instance.allowed_quantity
return representations