import - rancher organization linked deploy --> with dashboards of cooperative management oage

This commit is contained in:
2025-12-22 14:38:52 +03:30
parent 65a826ab7f
commit 5eb810893b
9 changed files with 272 additions and 24 deletions

View File

@@ -5,13 +5,17 @@ from rest_framework.decorators import action
from rest_framework.response import Response
from apps.authentication.api.v1.api import UserViewSet
from apps.authentication.models import Organization
from apps.core.api import BaseViewSet
from apps.core.mixins.search_mixin import DynamicSearchMixin
from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
from apps.herd.models import Herd, Rancher
from apps.herd.models import Herd, Rancher, RancherOrganizationLink
from apps.herd.services.rancher_dashboard_service import RancherDashboardService
from apps.herd.web.api.v1.serializers import HerdSerializer, RancherSerializer
from apps.herd.services.rancher_org_link_services import RancherOrganizationService
from apps.herd.web.api.v1.serializers import HerdSerializer, RancherSerializer, RancherOrganizationLinkSerializer
from apps.livestock.web.api.v1.serializers import LiveStockSerializer
from apps.product.models import OrganizationQuotaStats
from apps.product.services.quota_dashboard_service import QuotaDashboardService
from apps.product.web.api.v1.serializers import product_serializers
from common.helpers import get_organization_by_user
from common.tools import CustomOperations
@@ -262,36 +266,105 @@ class RancherViewSet(BaseViewSet, RancherDashboardService, SoftDeleteMixin, view
)
return Response(rancher_dashboard_data)
class RancherOrganizationLinkViewSet(
BaseViewSet,
SoftDeleteMixin,
viewsets.ModelViewSet,
RancherOrganizationService,
QuotaDashboardService,
DynamicSearchMixin
):
queryset = RancherOrganizationLink.objects.select_related('organization', 'rancher')
serializer_class = RancherOrganizationLinkSerializer
search_fields = [
"rancher__ranching_farm",
"rancher__first_name",
"rancher__last_name",
"rancher__mobile",
"rancher__national_code",
"rancher__birthdate",
"rancher__nationality",
"rancher__address",
"rancher__province__name",
"rancher__city__name",
"organization__name"
]
@action(
methods=['get'],
detail=False,
name='org_linked_rancher_list',
url_name='org_linked_rancher_list',
url_path='org_linked_rancher_list',
)
def org_linked_rancher_list(self, request):
"""
list of organizations with rancher information
"""
org = get_organization_by_user(request.user)
result = self.orgs_linked_rancher(org=org)
return Response(result)
@action(
methods=['get'],
detail=True,
url_name='org_ranchers',
url_path='org_ranchers',
name='org_ranchers'
)
def org_ranchers(self, request):
def org_ranchers(self, request, pk=None):
"""
list of ranchers by organization
"""
queryset = self.get_queryset(visibility_by_org_scope=True)
org_id = pk
queryset = self.get_queryset().filter(organization_id=org_id)
page = self.paginate_queryset(queryset)
if page is not None: # noqa
serializer = product_serializers.IncentivePlanRancherSerializer(page, many=True)
serializer = self.serializer_class(page, many=True)
return self.get_paginated_response(serializer.data)
return Response(status=status.HTTP_200_OK)
@action(
methods=['get'],
detail=False,
url_name='org_ranchers_dashboard',
url_path='org_ranchers_dashboard',
name='org_ranchers_dashboard'
detail=True,
url_name='org_ranchers_quota_dashboard',
url_path='org_ranchers_quota_dashboard',
name='org_ranchers_quota_dashboard'
)
def org_ranchers_dashboard(self, request):
def org_ranchers_quota_dashboard(self, request, pk=None):
"""
dashboard of ranchers report
dashboard of Org ranchers report by quota
"""
org_obj = Organization.objects.get(id=pk)
dashboard_data = self.get_dashboard(self, org=org_obj)
queryset = self.get_queryset(visibility_by_org_scope=True)
return Response(dashboard_data)
@action(
methods=['get'],
detail=True,
url_name='org_ranchers_product_dashboard',
url_path='org_ranchers_product_dashboard',
name='org_ranchers_product_dashboard'
)
def org_ranchers_product_dashboard(self, request, pk=None):
"""
dashboard of Org ranchers report by quota
"""
org_obj = Organization.objects.get(id=pk)
# get organization quota stats and get product ids
org_quota_stat = OrganizationQuotaStats.objects.select_related('organization', 'quota').filter(
organization=org_obj,
quota__is_closed=False
)
products = {f'{stat.quota.product.name}': stat.quota.product.id for stat in org_quota_stat}
dashboard_data = self.get_dashboard_by_product(self, organization=org_obj, products=products)
return Response(dashboard_data)

View File

@@ -1,16 +1,17 @@
from rest_framework import serializers
from apps.authentication.api.v1.serializers.serializer import (
UserSerializer,
OrganizationSerializer,
ProvinceSerializer,
CitySerializer
)
from apps.herd.exception import HerdCapacityException
from apps.herd.models import Herd, Rancher
from rest_framework import serializers
from apps.herd.models import Herd, Rancher, RancherOrganizationLink
class HerdSerializer(serializers.ModelSerializer):
""" Herd Serializer """
class Meta:
model = Herd
fields = '__all__'
@@ -49,14 +50,38 @@ class RancherSerializer(serializers.ModelSerializer):
representation = super().to_representation(instance)
representation['province'] = {
'id': instance.province.id,
'name': instance.province.name
}
if instance.province:
representation['province'] = {
'id': instance.province.id,
'name': instance.province.name
}
representation['city'] = {
'id': instance.city.id,
'name': instance.city.name
}
if instance.city:
representation['city'] = {
'id': instance.city.id,
'name': instance.city.name
}
return representation
class RancherOrganizationLinkSerializer(serializers.ModelSerializer):
class Meta:
model = RancherOrganizationLink
fields = '__all__'
def to_representation(self, instance):
representation = super().to_representation(instance)
if instance.rancher:
representation['rancher'] = RancherSerializer(instance.rancher).data
if instance.organization:
representation['organization'] = {
"id": instance.organization.id,
"name": instance.organization.name,
"province": instance.organization.province.name,
"city": instance.organization.city.name
}
return representation

View File

@@ -1,12 +1,14 @@
from django.urls import path, include
from rest_framework import routers
from .api import HerdViewSet, RancherViewSet
from .api import HerdViewSet, RancherViewSet, RancherOrganizationLinkViewSet
router = routers.DefaultRouter()
router.register('herd', HerdViewSet, basename='herd')
router.register('rancher', RancherViewSet, basename='rancher')
router.register('rancher_org_link', RancherOrganizationLinkViewSet, basename='rancher_org_link')
urlpatterns = [
path('api/v1/', include(router.urls))
]