Skip to content

Commit

Permalink
CRON endpoint for Donors export
Browse files Browse the repository at this point in the history
  • Loading branch information
danniel committed Jan 31, 2024
1 parent 3599c2b commit 6c5f1d9
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions backend/donations/views/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,64 @@ def get(self, request):


class CustomExport(Handler):
pass
def get(self, request):
current_year = timezone.now().year
start_arg = request.GET.get("start")
end_arg = request.GET.get("end")

if not start_arg or not end_arg:
return HttpResponse("Missing start and end from URL. Format: ?start=23-1&end=19-5")

start_arg = start_arg.split("-")
end_arg = end_arg.split("-")
query_start = datetime(current_year, int(start_arg[1]), int(start_arg[0]), 0, 0, 59)
query_end = datetime(current_year, int(end_arg[1]), int(end_arg[0]), 23, 59, 59)

donors = (
Donor.objects.filter(date_created__gte=query_start, date_created__lte=query_end).select_related("ngo").all()
)

fields = (
"id",
"last_name",
"first_name",
"email",
"has_signed",
"pdf_file",
"ngo__name",
"ngo__email",
"ngo__is_accepting_forms",
)

logger.info("Found {} donations".format(len(donors)))

response = HttpResponse(
content_type="text/csv",
headers={"Content-Disposition": 'attachment; filename="export_donor.csv"'},
)

writer = csv.writer(response, quoting=csv.QUOTE_ALL)
writer.writerow(fields)

for donor in donors:
if donor.ngo:
writer.writerow(
[
donor.id,
donor.first_name,
donor.last_name,
donor.email,
donor.has_signed,
donor.pdf_file.url if donor.pdf_file else donor.pdf_url,
donor.ngo.name,
donor.ngo.email,
donor.ngo.is_accepting_forms,
]
)
else:
logger.warn("Could not find ngo for donation, ID: {}".format(donor.id))

return response


class NgoExport(Handler):
Expand All @@ -64,7 +121,7 @@ def get(self, request):

response = HttpResponse(
content_type="text/csv",
headers={"Content-Disposition": 'attachment; filename="ngo_export.csv"'},
headers={"Content-Disposition": 'attachment; filename="export_ngo.csv"'},
)

writer = csv.writer(response, quoting=csv.QUOTE_ALL)
Expand Down

0 comments on commit 6c5f1d9

Please sign in to comment.