Changeset - 9d7af1ccaddd
[Not reviewed]
0 2 0
Ben Sturmfels (bsturmfels) - 9 months ago 2023-07-07 15:35:32
ben@sturm.com.au
Lash up an attendees CSV report
2 files changed with 36 insertions and 1 deletions:
0 comments (0 inline, 0 general)
vendor/registrasion/registrasion/reporting/reports.py
Show inline comments
 
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
vendor/registrasion/registrasion/urls.py
Show inline comments
...
 
@@ -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"),
 
]
 

	
 

	
0 comments (0 inline, 0 general)