Lash up an attendees CSV report

This commit is contained in:
Ben Sturmfels 2023-07-08 01:35:32 +10:00
parent b5022e8b42
commit 9d7af1ccad
Signed by: bsturmfels
GPG key ID: 023C05E2C9C068F0
2 changed files with 36 additions and 1 deletions

View file

@ -1,6 +1,8 @@
import csv 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.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from django.urls import reverse from django.urls import reverse
@ -365,3 +367,33 @@ def get_all_reports():
''' Returns all the views that have been registered with @report ''' ''' Returns all the views that have been registered with @report '''
return list(_all_report_views) 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

View file

@ -2,6 +2,7 @@ from .reporting import views as rv
from django.conf.urls import include from django.conf.urls import include
from django.conf.urls import url from django.conf.urls import url
from django.urls import path
from .views import ( from .views import (
amend_registration, amend_registration,
@ -24,6 +25,7 @@ from .views import (
review, review,
voucher_code, voucher_code,
) )
from .reporting.reports import attendees_report
public = [ public = [
@ -85,6 +87,7 @@ reports = [
rv.speaker_registrations, rv.speaker_registrations,
name="speaker_registrations", name="speaker_registrations",
), ),
path("attendees/", attendees_report, name="attendees_report"),
] ]