From 9d7af1ccaddd1722627d36177967a60c0c10af54 Mon Sep 17 00:00:00 2001 From: Ben Sturmfels Date: Sat, 8 Jul 2023 01:35:32 +1000 Subject: [PATCH] Lash up an attendees CSV report --- .../registrasion/reporting/reports.py | 34 ++++++++++++++++++- vendor/registrasion/registrasion/urls.py | 3 ++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/vendor/registrasion/registrasion/reporting/reports.py b/vendor/registrasion/registrasion/reporting/reports.py index 9c76d4df..bff98605 100644 --- a/vendor/registrasion/registrasion/reporting/reports.py +++ b/vendor/registrasion/registrasion/reporting/reports.py @@ -1,6 +1,8 @@ import csv +import datetime -from django.contrib.auth.decorators import user_passes_test +from django.contrib.auth.decorators import login_required, user_passes_test +from django.db import connection from django.shortcuts import render from django.http import HttpResponse from django.urls import reverse @@ -365,3 +367,33 @@ def get_all_reports(): ''' Returns all the views that have been registered with @report ''' return list(_all_report_views) + + +@login_required +@user_passes_test(lambda u: u.is_staff) +def attendees_report(request): + query = """ +select prof.name, email, prod.name as ticket, company, free_text_1, free_text_2, country, of_legal_age, dietary_restrictions, accessibility_requirements, gender +from registrasion_invoice i +inner join registrasion_cart c on i.cart_id = c.id +inner join registrasion_productitem pi on c.id = pi.cart_id +inner join registrasion_product prod on pi.product_id = prod.id +inner join auth_user u on i.user_id = u.id +inner join registrasion_attendee a on a.user_id = u.id +inner join registrasion_attendeeprofilebase b on b.attendee_id = a.id +left outer join pinaxcon_registrasion_attendeeprofile prof on b.id = prof.attendeeprofilebase_ptr_id +where prod.category_id = 1 +and i.status = 2 + """ + response = HttpResponse(content_type='text/csv') + filename = 'attendees_report-{}.csv'.format( + datetime.datetime.now().strftime('%Y-%m-%d'), + ) + response['Content-Disposition'] = f'attachment; filename={filename}' + writer = csv.writer(response) + with connection.cursor() as cursor: + cursor.execute(query) + writer.writerow([i[0] for i in cursor.description]) + for row in cursor.fetchall(): + writer.writerow(row) + return response diff --git a/vendor/registrasion/registrasion/urls.py b/vendor/registrasion/registrasion/urls.py index 48daad12..2ad31803 100644 --- a/vendor/registrasion/registrasion/urls.py +++ b/vendor/registrasion/registrasion/urls.py @@ -2,6 +2,7 @@ from .reporting import views as rv from django.conf.urls import include from django.conf.urls import url +from django.urls import path from .views import ( amend_registration, @@ -24,6 +25,7 @@ from .views import ( review, voucher_code, ) +from .reporting.reports import attendees_report public = [ @@ -85,6 +87,7 @@ reports = [ rv.speaker_registrations, name="speaker_registrations", ), + path("attendees/", attendees_report, name="attendees_report"), ]