admin_views.py 5.85 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import csv
import json

from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import permission_required
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.shortcuts import get_object_or_404, render
from django.utils import timezone
from django.utils.text import slugify
from django.utils.translation import pgettext_lazy
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.http import require_http_methods

from events.decorators import organiser_only
from .models import Event, Registration


@staff_member_required
@permission_required('events.change_event')
@organiser_only
def details(request, event_id):
    event = get_object_or_404(Event, pk=event_id)

    return render(request, 'events/admin/details.html', {
        'event': event,
    })


@staff_member_required
@permission_required('events.change_event')
@organiser_only
@require_http_methods(["POST"])
def change_registration(request, event_id, action=None):
    data = {
        'success': True
    }

    try:
        id = request.POST.get("id", -1)
        obj = Registration.objects.get(event=event_id, pk=id)
41
42
43
        if action == 'present':
            checked = json.loads(request.POST.get("checked"))
            if checked is not None:
44
                obj.present = checked
45
46
47
48
49
50
                obj.save()
        elif action == 'payment':
            value = request.POST.get("value")
            if value is not None:
                obj.payment = value
                obj.save()
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    except Registration.DoesNotExist:
        data['success'] = False

    return JsonResponse(data)


@staff_member_required
@permission_required('events.change_event')
@organiser_only
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', 'email', 'paid', 'present',
         'status', 'phone number'] +
        [field.name for field in extra_fields] +
        ['date', 'date cancelled'])

    rows = []
    if event.price == 0:
        header_fields.remove('paid')
    for i, registration in enumerate(registrations):
        if registration.member:
            name = registration.member.get_full_name()
        else:
            name = registration.name
        status = pgettext_lazy('registration status', 'registered')
        cancelled = None
        if registration.date_cancelled:

            if registration.is_late_cancellation():
                status = pgettext_lazy('registration status',
                                       'late cancellation')
            else:
                status = pgettext_lazy('registration status', 'cancelled')
            cancelled = timezone.localtime(registration.date_cancelled)

        elif registration.queue_position:
            status = pgettext_lazy('registration status', 'waiting')
        data = {
            'name': name,
            'date': timezone.localtime(registration.date
                                       ).strftime("%Y-%m-%d %H:%m"),
            'present': _('Yes') if registration.present else '',
            'phone number': (registration.member.phone_number
                             if registration.member
                             else ''),
            'email': (registration.member.user.email
                      if registration.member
                      else ''),
            'status': status,
            'date cancelled': cancelled,
        }
        if event.price > 0:
107
            if registration.payment == Registration.PAYMENT_CASH:
108
                data['paid'] = _('Cash')
109
110
            elif registration.payment == Registration.PAYMENT_CARD:
                data['paid'] = _('Card')
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
            else:
                data['paid'] = _('No')

        data.update({field['field'].name: field['value'] for field in
                     registration.information_fields})
        rows.append(data)

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

    rows = sorted(rows,
                  key=lambda row:
                  (row['status'] == pgettext_lazy('registration status',
                                                  'late cancellation'),
                   row['date']),
                  reverse=True,
                  )

    for row in rows:
        writer.writerow(row)

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


@staff_member_required
@permission_required('events.change_event')
def export_email(request, event_id):
    event = get_object_or_404(Event, pk=event_id)
    registrations = event.registration_set.filter(
        date_cancelled=None).prefetch_related('member__user')
    registrations = registrations[:event.max_participants]
    addresses = [r.member.user.email for r in registrations if r.member]
    no_addresses = [r.name for r in registrations if not r.member]
    return render(request, 'events/admin/email_export.html',
                  {'event': event, 'addresses': addresses,
                   'no_addresses': no_addresses})


@staff_member_required
@permission_required('events.change_event')
@organiser_only
def all_present(request, event_id):
    event = get_object_or_404(Event, pk=event_id)

    if event.max_participants is None:
        registrations_query = event.registration_set.filter(
            date_cancelled=None)
    else:
        registrations_query = (event.registration_set
                               .filter(date_cancelled=None)
                               .order_by('date')[:event.max_participants])

    event.registration_set.filter(pk__in=registrations_query).update(
167
        present=True, payment=Registration.PAYMENT_CASH)
168
169

    return HttpResponseRedirect('/events/admin/{}'.format(event_id))