From b571c6b8471d0986c14486e503cb2e2a60e35725 Mon Sep 17 00:00:00 2001 From: basant-gautam Date: Sat, 10 Oct 2026 12:24:23 +0530 Subject: [PATCH 1/2] feat(database): add backend APIs for batch and branch wise student count --- .../academic_information/api/urls.py | 3 +- .../academic_information/api/views.py | 66 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/FusionIIIT/applications/academic_information/api/urls.py b/FusionIIIT/applications/academic_information/api/urls.py index 426733998..bae911081 100644 --- a/FusionIIIT/applications/academic_information/api/urls.py +++ b/FusionIIIT/applications/academic_information/api/urls.py @@ -69,5 +69,6 @@ # url(r'^grades',views.grades_api,name='grades-get-api'), - # url(r'^spi',views.spi_api,name='spi-get-api') + url(r'^database/batch-wise-student-count/$', views.database_batch_wise_student_count, name='database_batch_wise_student_count'), + url(r'^database/branch-wise-student-count/$', views.database_branch_wise_student_count, name='database_branch_wise_student_count'), ] diff --git a/FusionIIIT/applications/academic_information/api/views.py b/FusionIIIT/applications/academic_information/api/views.py index bb02a443a..fdc00c3c8 100644 --- a/FusionIIIT/applications/academic_information/api/views.py +++ b/FusionIIIT/applications/academic_information/api/views.py @@ -2234,3 +2234,69 @@ def assign_section(request): return Response({'detail': f'Section updated for {updated} student(s).', 'updated': updated, 'section': section or None}) + +@api_view(['GET']) +@permission_classes([IsAuthenticated]) +@authentication_classes([TokenAuthentication]) +@role_required(DATABASE_REPORT_ROLES) +def database_batch_wise_student_count(request): + """Batch wise student count showing male, female, and total for batches.""" + from applications.globals.programme_scope import scopes_for, scope_via_student + from django.db.models import Count, Q + + students = scope_via_student( + Student.objects.all(), scopes_for(request.user), 'id' + ) + # Filter for batches 2020 to 2026 + students = students.filter(batch__gte=2020, batch__lte=2026) + + counts = students.values('batch').annotate( + male=Count('id', filter=Q(id__sex='M')), + female=Count('id', filter=Q(id__sex='F')), + total=Count('id') + ).order_by('batch') + + rows = [] + for count in counts: + batch = count['batch'] + rows.append({ + 'batch': f"{batch}-{str(batch+1)[2:]}", + 'male': count['male'], + 'female': count['female'], + 'total': count['total'], + }) + return Response({'rows': rows, 'batches': list(counts.values_list('batch', flat=True))}) + +@api_view(['GET']) +@permission_classes([IsAuthenticated]) +@authentication_classes([TokenAuthentication]) +@role_required(DATABASE_REPORT_ROLES) +def database_branch_wise_student_count(request): + """Branch wise student count showing male, female, and total for each batch and branch.""" + from applications.globals.programme_scope import scopes_for, scope_via_student + from django.db.models import Count, Q + + students = scope_via_student( + Student.objects.all(), scopes_for(request.user), 'id' + ) + students = students.filter(batch__gte=2020, batch__lte=2026) + + counts = students.values('batch', 'programme', 'batch_id__discipline__acronym').annotate( + male=Count('id', filter=Q(id__sex='M')), + female=Count('id', filter=Q(id__sex='F')), + total=Count('id') + ).order_by('batch', 'programme', 'batch_id__discipline__acronym') + + rows = [] + for count in counts: + batch = count['batch'] + programme = count['programme'] or 'Unknown' + discipline = count['batch_id__discipline__acronym'] or 'General' + rows.append({ + 'batch': f"{batch}-{str(batch+1)[2:]}", + 'branch': f"{programme} {discipline}", + 'male': count['male'], + 'female': count['female'], + 'total': count['total'], + }) + return Response({'rows': rows}) From 5e66c349c3b62ac4fb645339c8583aef623af11f Mon Sep 17 00:00:00 2001 From: basant-gautam Date: Sat, 10 Oct 2026 12:38:04 +0530 Subject: [PATCH 2/2] fix(database): remove upper limit of 2026 for batches to make reports future-proof --- FusionIIIT/applications/academic_information/api/views.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/FusionIIIT/applications/academic_information/api/views.py b/FusionIIIT/applications/academic_information/api/views.py index fdc00c3c8..e519fcd0d 100644 --- a/FusionIIIT/applications/academic_information/api/views.py +++ b/FusionIIIT/applications/academic_information/api/views.py @@ -2247,8 +2247,8 @@ def database_batch_wise_student_count(request): students = scope_via_student( Student.objects.all(), scopes_for(request.user), 'id' ) - # Filter for batches 2020 to 2026 - students = students.filter(batch__gte=2020, batch__lte=2026) + # Filter for batches 2020 onwards + students = students.filter(batch__gte=2020) counts = students.values('batch').annotate( male=Count('id', filter=Q(id__sex='M')), @@ -2279,7 +2279,8 @@ def database_branch_wise_student_count(request): students = scope_via_student( Student.objects.all(), scopes_for(request.user), 'id' ) - students = students.filter(batch__gte=2020, batch__lte=2026) + # Filter for batches 2020 onwards + students = students.filter(batch__gte=2020) counts = students.values('batch', 'programme', 'batch_id__discipline__acronym').annotate( male=Count('id', filter=Q(id__sex='M')),