import --> tag batch main page dashboard
This commit is contained in:
@@ -1,5 +1,10 @@
|
|||||||
|
from django.db.models import Sum, Q
|
||||||
|
from django.db.models.aggregates import Count
|
||||||
|
from django.db.models.functions import Coalesce
|
||||||
|
|
||||||
from apps.authentication.models import Organization
|
from apps.authentication.models import Organization
|
||||||
from apps.authentication.services.service import get_all_org_child
|
from apps.authentication.services.service import get_all_org_child
|
||||||
|
from apps.livestock.models import LiveStockSpecies
|
||||||
from apps.tag.models import TagBatch
|
from apps.tag.models import TagBatch
|
||||||
|
|
||||||
|
|
||||||
@@ -8,7 +13,7 @@ class TagBatchService:
|
|||||||
services of tag batch
|
services of tag batch
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def main_dashboard(self, org: Organization = None):
|
def tag_batch_main_dashboard(self, org: Organization = None):
|
||||||
"""
|
"""
|
||||||
dashboard data of batch main page
|
dashboard data of batch main page
|
||||||
"""
|
"""
|
||||||
@@ -20,3 +25,38 @@ class TagBatchService:
|
|||||||
child_org = get_all_org_child(org)
|
child_org = get_all_org_child(org)
|
||||||
child_org.append(org)
|
child_org.append(org)
|
||||||
tag_batches = tag_batches.filter(organization_id__in=[child.id for child in child_org])
|
tag_batches = tag_batches.filter(organization_id__in=[child.id for child in child_org])
|
||||||
|
|
||||||
|
tag_batch_data = tag_batches.aggregate(
|
||||||
|
batch_count=Count('id'),
|
||||||
|
total_distributed_tags=Coalesce(Sum('total_distributed_tags'), 0),
|
||||||
|
total_remaining_tags=Coalesce(Sum('total_remaining_tags'), 0),
|
||||||
|
tag_count_created_by_batch=Count('tag'),
|
||||||
|
has_distributed_batches_number=Count('id', filter=Q(status='distributed'))
|
||||||
|
)
|
||||||
|
|
||||||
|
species = LiveStockSpecies.objects.values('value')
|
||||||
|
|
||||||
|
data_by_species_list = []
|
||||||
|
for spec in species:
|
||||||
|
tag_batch_data_by_species = tag_batches.aggregate(
|
||||||
|
batch_count=Count('id', species_code=spec.get('value')),
|
||||||
|
total_distributed_tags=Coalesce(
|
||||||
|
Sum('total_distributed_tags', filter=Q(species_code=spec.get('value'))), 0
|
||||||
|
),
|
||||||
|
total_remaining_tags=Coalesce(
|
||||||
|
Sum('total_remaining_tags', filter=Q(species_code=spec.get('value'))), 0
|
||||||
|
),
|
||||||
|
tag_count_created_by_batch=Coalesce(
|
||||||
|
Count('tag', filter=Q(species_code=spec.get('value'))), 0
|
||||||
|
),
|
||||||
|
has_distributed_batches_number=Count(
|
||||||
|
'id', filter=Q(status='distributed', species_code=spec.get('value'))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
tag_batch_data_by_species.update({'species_code': spec.get('value')})
|
||||||
|
|
||||||
|
data_by_species_list.append(tag_batch_data_by_species)
|
||||||
|
|
||||||
|
tag_batch_data.update({'batch_data_by_species': data_by_species_list})
|
||||||
|
|
||||||
|
return tag_batch_data
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
|
|||||||
from apps.tag import exceptions as tag_exceptions
|
from apps.tag import exceptions as tag_exceptions
|
||||||
from apps.tag import models as tag_models
|
from apps.tag import models as tag_models
|
||||||
from apps.tag.models import TagBatch
|
from apps.tag.models import TagBatch
|
||||||
|
from apps.tag.services.tag_batch_service import TagBatchService
|
||||||
from apps.tag.services.tag_distribution_services import TagDistributionService
|
from apps.tag.services.tag_distribution_services import TagDistributionService
|
||||||
from apps.tag.services.tag_services import TagService
|
from apps.tag.services.tag_services import TagService
|
||||||
from common.helpers import get_organization_by_user
|
from common.helpers import get_organization_by_user
|
||||||
@@ -305,7 +306,7 @@ class AllocatedTagsViewSet(SoftDeleteMixin, viewsets.ModelViewSet):
|
|||||||
serializer_class = AllocatedTagsSerializer
|
serializer_class = AllocatedTagsSerializer
|
||||||
|
|
||||||
|
|
||||||
class TagBatchViewSet(BaseViewSet, SoftDeleteMixin, DynamicSearchMixin, viewsets.ModelViewSet):
|
class TagBatchViewSet(BaseViewSet, SoftDeleteMixin, DynamicSearchMixin, TagBatchService, viewsets.ModelViewSet):
|
||||||
queryset = TagBatch.objects.all()
|
queryset = TagBatch.objects.all()
|
||||||
serializer_class = TagBatchSerializer
|
serializer_class = TagBatchSerializer
|
||||||
filter_backends = [SearchFilter]
|
filter_backends = [SearchFilter]
|
||||||
@@ -332,6 +333,23 @@ class TagBatchViewSet(BaseViewSet, SoftDeleteMixin, DynamicSearchMixin, viewsets
|
|||||||
return self.get_paginated_response(serializer.data)
|
return self.get_paginated_response(serializer.data)
|
||||||
return Response(self.serializer_class(queryset).data)
|
return Response(self.serializer_class(queryset).data)
|
||||||
|
|
||||||
|
@action(
|
||||||
|
methods=['get'],
|
||||||
|
detail=False,
|
||||||
|
url_name='main_dashboard',
|
||||||
|
url_path='main_dashboard',
|
||||||
|
name='main_dashboard',
|
||||||
|
)
|
||||||
|
def main_dashboard(self, request):
|
||||||
|
"""
|
||||||
|
dashboard of tag batches main page
|
||||||
|
"""
|
||||||
|
org = get_organization_by_user(request.user)
|
||||||
|
|
||||||
|
dashboard_data = self.tag_batch_main_dashboard(org=org)
|
||||||
|
|
||||||
|
return Response(dashboard_data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
def destroy(self, request, pk=None, *args, **kwargs):
|
def destroy(self, request, pk=None, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
soft delete batch with tag items
|
soft delete batch with tag items
|
||||||
|
|||||||
Reference in New Issue
Block a user