diff --git a/home/admin.py b/home/admin.py index fbcce07..fa6dbab 100644 --- a/home/admin.py +++ b/home/admin.py @@ -7,7 +7,7 @@ import csv from django.contrib import admin -from django.http import HttpResponse +from django.http import HttpRequest, HttpResponse from import_export.admin import ImportExportMixin, ImportExportModelAdmin from home.models import ( @@ -49,7 +49,7 @@ UnregisteredStudentResource, ) from .utils.django_email_server import caterer_mail, long_rebate_query_mail -from .utils.month import fill_periods +from .utils.month import fill_periods, map_periods_to_long_rebate from .utils.rebate_bills_saver import ( fix_all_bills, save_long_bill, @@ -353,7 +353,14 @@ class about_Admin(ImportExportModelAdmin, admin.ModelAdmin): }, ), ) - actions = ["export_as_csv", "disapprove", "approve", "send_mail", "clean"] + actions = [ + "export_as_csv", + "disapprove", + "approve", + "send_mail", + "clean", + "get_rebate_days_per_caterer", + ] @admin.action(description="Disapprove the students") def disapprove(self, request, queryset): @@ -393,20 +400,13 @@ def send_mail(self, request, queryset): for obj in queryset: long_rebate_query_mail(obj.start_date, obj.end_date, obj.email.email) - # @admin.action(description="Clean left long rebate data") - # def clean(self, request, queryset): - # """ - # Clean left long rebate data - # """ - # for obj in queryset: - # if(obj.approved==True): - # if(obj.end_date>datetime.date(2023,12,7)): - # print(obj.end_date) - # obj.approved=False - # obj.save() - # print(obj.approved) - # obj.approved=True - # obj.save() + @admin.action(description="Get total rebate days per caterer") + def get_rebate_days_per_caterer(self, request, queryset: list[LongRebate]): + longRebates = [] + for obj in queryset: + if obj.approved: + longRebates.append(obj) + return map_periods_to_long_rebate(longRebates, request.user) @admin.register(Rebate) @@ -454,7 +454,7 @@ class about_Admin(ImportExportModelAdmin, admin.ModelAdmin): ), ) - def get_queryset(self, request): + def get_queryset(self, request: HttpRequest): qs = super().get_queryset(request) if request.user.is_superuser: return qs diff --git a/home/utils/month.py b/home/utils/month.py index 80ce68c..56b10fd 100644 --- a/home/utils/month.py +++ b/home/utils/month.py @@ -1,7 +1,13 @@ -from datetime import timedelta +import csv +from datetime import date, timedelta + +from django.contrib.auth.base_user import AbstractBaseUser +from django.contrib.auth.models import AnonymousUser +from django.db.models import Q +from django.http import HttpResponse from ..models.allocation import Allocation, Period -from ..models.students import Student +from ..models.students import LongRebate, Student def fill_periods(email, start_date, end_date): @@ -29,3 +35,52 @@ def fill_periods(email, start_date, end_date): print(f"{period_no}: {days} days") return days_per_period + + +def map_periods_to_long_rebate( + longRebate: list[LongRebate], user: AbstractBaseUser | AnonymousUser +): + periods = Period.objects.filter(semester__name="Autumn 2024") + period_to_long_rebate_map = {period.Sno: {} for period in periods} + periods_to_email = {period.Sno: [] for period in periods} + for rebate in longRebate: + for period in periods: + if ( + rebate.start_date <= period.end_date + and rebate.end_date > period.start_date + ): + start_date: date = max(rebate.start_date, period.start_date) + end_date: date = min(rebate.end_date, period.end_date) + if period_to_long_rebate_map[period.Sno].get(rebate.email) is None: + period_to_long_rebate_map[period.Sno][rebate.email] = 0 + period_to_long_rebate_map[period.Sno][rebate.email] += ( + end_date - start_date + ).days + 1 + periods_to_email[period.Sno].append(rebate.email) + + response = HttpResponse(content_type="text/csv") + response["Content-Disposition"] = 'attachment; filename="Rebate.csv"' + + writer = csv.writer(response) + writer.writerow(["Email", "Period", "caterer", "Days"]) + + for period_no, emails in periods_to_email.items(): + period = periods[period_no - 1] + allocations = Allocation.objects.filter(Q(email__in=emails), period=period) + for allocation in allocations: + if ( + not user.is_superuser + and not user.groups.filter(name="College Administration") + and not user.username == allocation.caterer.name + ): + continue + writer.writerow( + [ + allocation.email, + period_no, + allocation.caterer.name, + period_to_long_rebate_map[period_no][allocation.email], + ] + ) + + return response