diff --git a/home/admin.py b/home/admin.py index fa6dbab..c87dcb5 100644 --- a/home/admin.py +++ b/home/admin.py @@ -466,9 +466,15 @@ def get_queryset(self, request: HttpRequest): @admin.display(description="name") def name(self, obj): - return obj.email.name + return getattr(obj.email, "name", None) - actions = ["export_as_csv", "disapprove", "approve", "export_rebate_total"] + actions = [ + "export_as_csv", + "disapprove", + "approve", + "export_rebate_total", + "find_overlapping_records", + ] @admin.action(description="Disapprove the students") def disapprove(self, request, queryset): @@ -501,6 +507,8 @@ def export_as_csv(self, request, queryset): def export_rebate_total(modeladmin, request, queryset): total_days = 0 for obj in queryset: + if obj.start_date > obj.end_date: + continue total_days += (obj.end_date - obj.start_date).days + 1 # Create the HttpResponse object with the appropriate CSV header. @@ -513,6 +521,32 @@ def export_rebate_total(modeladmin, request, queryset): return response + def find_overlapping_records(self, request, queryset): + response = HttpResponse(content_type="text/csv") + response["Content-Disposition"] = 'attachment; filename="Rebate.csv"' + + writer = csv.writer(response) + for obj in queryset: + if obj.start_date > obj.end_date: + obj.delete() + rebates = ( + Rebate.objects.filter(email=obj.email) + .filter(start_date__lte=obj.end_date) + .filter(end_date__gte=obj.start_date) + .exclude(pk=obj.pk) + ) + for rebate in rebates: + writer.writerow( + [ + rebate.email, + rebate.start_date, + rebate.end_date, + getattr(rebate.email, "name", None), + ] + ) + + return response + export_as_csv.short_description = "Export Rebate details to CSV"