Skip to content

Commit

Permalink
add action to clean short rebate
Browse files Browse the repository at this point in the history
  • Loading branch information
mittal-ishaan committed Nov 13, 2024
1 parent c23f2de commit 3b87b57
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions home/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -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"


Expand Down

0 comments on commit 3b87b57

Please sign in to comment.