fix --> structure of species data in main dist batch dashboard
This commit is contained in:
22
apps/tag/services/tag_batch_service.py
Normal file
22
apps/tag/services/tag_batch_service.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from apps.authentication.models import Organization
|
||||
from apps.authentication.services.service import get_all_org_child
|
||||
from apps.tag.models import TagBatch
|
||||
|
||||
|
||||
class TagBatchService:
|
||||
"""
|
||||
services of tag batch
|
||||
"""
|
||||
|
||||
def main_dashboard(self, org: Organization = None):
|
||||
"""
|
||||
dashboard data of batch main page
|
||||
"""
|
||||
|
||||
tag_batches = TagBatch.objects.select_related('organization').prefetch_related('tag')
|
||||
|
||||
if org.type.key != 'ADM':
|
||||
# get batches with org & their child
|
||||
child_org = get_all_org_child(org)
|
||||
child_org.append(org)
|
||||
tag_batches = tag_batches.filter(organization_id__in=[child.id for child in child_org])
|
||||
@@ -191,12 +191,14 @@ class TagDistributionService:
|
||||
species = LiveStockSpecies.objects.values('value')
|
||||
|
||||
for spec in species:
|
||||
items_list.append({
|
||||
spec.get('value'): distributions.aggregate(
|
||||
dist_data = distributions.aggregate(
|
||||
dist_count=Count('id', filter=Q(species_code=spec.get('value'))),
|
||||
tag_count=Sum('distributed_number', filter=Q(species_code=spec.get('value')))
|
||||
),
|
||||
})
|
||||
tag_count=Coalesce(
|
||||
Sum('distributed_number', filter=Q(species_code=spec.get('value'))), 0
|
||||
)
|
||||
)
|
||||
dist_data.update({'species_code': spec.get('value')}) # add species code to data
|
||||
items_list.append(dist_data)
|
||||
|
||||
data.update({'items': items_list})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user