views.py 3.02 KB
Newer Older
Thom Wiggers's avatar
Thom Wiggers committed
1 2 3 4
import csv
from datetime import timedelta

from django.http import HttpResponse
Thom Wiggers's avatar
Thom Wiggers committed
5 6 7
from django.shortcuts import render, get_object_or_404
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import permission_required
Thom Wiggers's avatar
Thom Wiggers committed
8 9
from django.utils import timezone
from django.utils.text import slugify
Thom Wiggers's avatar
Thom Wiggers committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

from .models import Event


@staff_member_required
@permission_required('events.change_event')
def admin_details(request, event_id):
    event = get_object_or_404(Event, pk=event_id)
    n = event.max_participants
    registrations = list(event.registration_set.filter(date_cancelled=None))
    cancellations = event.registration_set.exclude(date_cancelled=None)
    return render(request, 'events/admin/details.html', {
        'event': event,
        'registrations': registrations[:n],
        'waiting': registrations[n:] if n else [],
        'cancellations': cancellations,
    })
Thom Wiggers's avatar
Thom Wiggers committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87


@staff_member_required
@permission_required('events.change_event')
def export(request, event_id):
    event = get_object_or_404(Event, pk=event_id)
    extra_fields = event.registrationinformationfield_set.all()
    registrations = event.registration_set.all()

    header_fields = (
        ['name', 'date', 'email'] + [field.name for field in extra_fields] +
        ['present', 'paid', 'status', 'date_cancelled'])

    rows = []
    capacity = event.max_participants
    for i, registration in enumerate(registrations):
        if registration.member:
            name = registration.member.get_full_name()
        else:
            name = registration.name
        status = 'registered'
        cancelled = None
        if registration.date_cancelled:
            if capacity is not None:
                capacity += 1
            status = 'cancelled'
            cancelled = timezone.localtime(registration.date_cancelled)
        elif capacity and i >= capacity:
            status = 'waiting'
        data = {
            'name': name,
            'date': timezone.localtime(registration.date),
            'paid': registration.paid,
            'present': registration.present,
            'email': (registration.member.user.email
                      if registration.member
                      else ''),
            'status': status,
            'date_cancelled': cancelled,
        }
        data.update({field['field'].name: field['value'] for field in
                     registration.registration_information()})
        rows.append(data)

    response = HttpResponse(content_type='text/csv')
    writer = csv.DictWriter(response, header_fields)
    writer.writeheader()

    def order(item):
        if item['status'] == 'cancelled':
            return item['date'] + timedelta(days=10000)
        elif item['status'] == 'registered':
            return item['date'] - timedelta(days=10000)
        else:
            return item['date']
    for row in sorted(rows, key=order):
        writer.writerow(row)

    response['Content-Disposition'] = (
        'attachment; filename="{}.csv"'.format(slugify(event.title)))
    return response