Skip to content

Commit

Permalink
admin_action (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
mittal-ishaan authored Oct 29, 2024
1 parent e6fb6c8 commit dc59634
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion home/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
For more information please see: https://docs.djangoproject.com/en/4.1/ref/contrib/admin/
"""

import csv

from django.contrib import admin
from django.http import HttpResponse
from import_export.admin import ImportExportMixin, ImportExportModelAdmin
Expand Down Expand Up @@ -466,7 +468,7 @@ def get_queryset(self, request):
def name(self, obj):
return obj.email.name

actions = ["export_as_csv", "disapprove", "approve"]
actions = ["export_as_csv", "disapprove", "approve", "export_rebate_total"]

@admin.action(description="Disapprove the students")
def disapprove(self, request, queryset):
Expand Down Expand Up @@ -496,6 +498,21 @@ def export_as_csv(self, request, queryset):
response["Content-Disposition"] = 'attachment; filename="Rebate.csv"'
return response

def export_rebate_total(modeladmin, request, queryset):
total_days = 0
for obj in queryset:
total_days += (obj.end_date - obj.start_date).days + 1

# Create the HttpResponse object with the appropriate CSV header.
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = 'attachment; filename="Rebate.csv"'

writer = csv.writer(response)
writer.writerow(["Total Days"])
writer.writerow([total_days])

return response

export_as_csv.short_description = "Export Rebate details to CSV"


Expand Down

0 comments on commit dc59634

Please sign in to comment.