From 5868149996cb9d05f059a760c5828845ff12c9e4 Mon Sep 17 00:00:00 2001 From: Pancham Agarwal Date: Sun, 4 Feb 2024 04:22:55 +0530 Subject: [PATCH] added year filter + bug fix --- apps/nightpass/views.py | 18 +++++++++--------- apps/users/admin.py | 24 ++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/apps/nightpass/views.py b/apps/nightpass/views.py index cdcbdd4..de802df 100644 --- a/apps/nightpass/views.py +++ b/apps/nightpass/views.py @@ -96,29 +96,29 @@ def generate_pass(request, campus_resource): student_year = sem_vs_year[user.student.semester] if student_year == 1: student_year_pass_count = NightPass.objects.filter(valid=True, campus_resource=campus_resource, - user__student__semester__in=[1,2]).count() - if student_year_pass_count > Settings.first_year: + user__student__semester__in=['1','2']).count() + if student_year_pass_count >= Settings.first_year: return HttpResponse(json.dumps(data)) elif student_year == 2: student_year_pass_count = NightPass.objects.filter(valid=True, campus_resource=campus_resource, - user__student__semester__in=[3,4]).count() - if student_year_pass_count > Settings.second_year: + user__student__semester__in=['3','4']).count() + if student_year_pass_count >= Settings.second_year: return HttpResponse(json.dumps(data)) elif student_year == 3: student_year_pass_count = NightPass.objects.filter(valid=True, campus_resource=campus_resource, - user__student__semester__in=[5,6]).count() - if student_year_pass_count > Settings.third_year: + user__student__semester__in=['5','6']).count() + if student_year_pass_count >= Settings.third_year: return HttpResponse(json.dumps(data)) elif student_year == 4: student_year_pass_count = NightPass.objects.filter(valid=True, campus_resource=campus_resource, - user__student__semester__in=[7,8]).count() - if student_year_pass_count > Settings.fourth_year: + user__student__semester__in=['7','8']).count() + if student_year_pass_count >= Settings.fourth_year: return HttpResponse(json.dumps(data)) if Settings.enable_hostel_limits: student_hostel_pass_count = NightPass.objects.filter(valid=True, campus_resource=campus_resource, user__student__hostel=user.student.hostel).count() - if student_hostel_pass_count > user.student.hostel.max_students_allowed: + if student_hostel_pass_count >= user.student.hostel.max_students_allowed: data = { 'status':False, 'message':'All slots are booked for today!' diff --git a/apps/users/admin.py b/apps/users/admin.py index b0b378f..e37452d 100644 --- a/apps/users/admin.py +++ b/apps/users/admin.py @@ -12,13 +12,33 @@ from datetime import date, datetime import io - +class YearWiseFilter(admin.SimpleListFilter): + title = 'Year' + parameter_name = 'year' + + def lookups(self, request, model_admin): + return ( + ('1', '1st Year'), + ('2', '2nd Year'), + ('3', '3rd Year'), + ('4', '4th Year'), + ) + + def queryset(self, request, queryset): + if self.value() == '1': + return queryset.filter(user__student__semester__in=['1','2']) + if self.value() == '2': + return queryset.filter(user__student__semester__in=['3','4']) + if self.value() == '3': + return queryset.filter(user__student__semester__in=['5','6']) + if self.value() == '4': + return queryset.filter(user__student__semester__in=['7','8']) class NightPassAdmin(admin.ModelAdmin): list_display = ( 'name','user','hostel','date', 'campus_resource','hostel_check_out', 'check_in', 'check_out', 'hostel_check_in', 'defaulter') search_fields = ('user__student__name','user__student__registration_number','user__student__email') actions = ['export_as_xlsx'] - list_filter = (('date', DateRangeFilter),'campus_resource','user__student__gender','user__student__hostel', 'defaulter', 'check_in', 'check_out') + list_filter = (('date', DateRangeFilter),'campus_resource','user__student__gender','user__student__hostel', YearWiseFilter,'defaulter', 'check_in', 'check_out') autocomplete_fields = ('user', 'campus_resource') readonly_fields = ('pass_id', 'check_in_time', 'check_out_time', 'hostel_checkout_time', 'hostel_checkin_time')