From 973d1319da5d61df552732da8d7ee6a489645a54 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Sat, 27 Sep 2025 16:22:28 +0330 Subject: [PATCH] list of rancher plans api by rancher id - complete --- apps/herd/web/api/v1/api.py | 23 +++++++++++++++++-- .../api/v1/serializers/product_serializers.py | 10 ++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/apps/herd/web/api/v1/api.py b/apps/herd/web/api/v1/api.py index b364e82..462e650 100644 --- a/apps/herd/web/api/v1/api.py +++ b/apps/herd/web/api/v1/api.py @@ -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) diff --git a/apps/product/web/api/v1/serializers/product_serializers.py b/apps/product/web/api/v1/serializers/product_serializers.py index e17610a..7025fda 100644 --- a/apps/product/web/api/v1/serializers/product_serializers.py +++ b/apps/product/web/api/v1/serializers/product_serializers.py @@ -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