1 # Create your views here.
6 from django.shortcuts import render_to_response
7 from django.template import RequestContext
8 from django.contrib.auth.decorators import permission_required
10 from models import Booking, TICKET_PRICE, SHOW_CHOICES
12 class BookingForm(django.forms.ModelForm):
17 def confirm_booking(booking, template):
18 from django.core.mail import EmailMessage
20 context = {'booking' : booking}
22 rendered = django.template.loader.render_to_string(template, context)
23 (headers, body) = rendered.strip().split('\n\n', 1)
25 # Turn the headers into a dict so EmailMessage can turn them into a
26 # string again. Bit pointless, but it works.
27 # Perhaps we should just use python email stuff directly. OTOH, we
28 # still always need to parse for the From header.
31 # If no From header is present, let EmailMessage do the default
34 for header in headers.split('\n'):
35 (field, value) = header.split(':')
38 elif (field == 'Subject'):
41 # Don't put From and Subject in the dict, else they'll be
43 headers_dict[field] = value
46 # Only setting the From address through headers won't set the
47 # envelope address right.
48 from_email = from_email,
52 headers = headers_dict
57 if request.method == "POST":
58 f = BookingForm(request.POST)
64 confirm_booking(booking, 'tickets/booked.eml')
65 return render_to_response('tickets/booked.html', {'booking' : booking}, context_instance=RequestContext(request))
67 return render_to_response('tickets/bookingform.html', {'form' : f, 'price' : TICKET_PRICE}, context_instance=RequestContext(request))
69 # These two forms are used for entering payment details. They aren't
70 # very different, so it should really be possible to merge them (but
71 # initial attempts to add the confirm field dynamically or setting its
72 # value dynamically, didn't work out all too well, so leave it at this
74 class PaymentForm(django.forms.Form):
75 bookings = django.forms.models.ModelMultipleChoiceField(
76 queryset=Booking.objects.filter(payment__isnull=True).order_by('pk'),
77 widget=django.forms.CheckboxSelectMultiple,
78 label="Reserveringen")
80 class PaymentConfirmForm(django.forms.Form):
81 bookings = django.forms.models.ModelMultipleChoiceField(
82 queryset=Booking.objects.filter(payment__isnull=True).order_by('pk'),
83 widget=django.forms.MultipleHiddenInput,
84 label="Reserveringen")
85 # This field is used to distinguish these two forms
86 confirm = django.forms.BooleanField(initial=True, widget=django.forms.HiddenInput)
88 @permission_required('tickets.change_booking')
89 def payments(request):
92 if request.method != "POST":
93 # First step: Just show an empty form
95 elif not 'confirm' in request.POST:
96 # Second step: Process form and show a summary for confirmation
97 f = PaymentForm(request.POST)
99 bookings = f.cleaned_data['bookings']
100 # Output a confirmation form
101 f = PaymentConfirmForm()
102 f.initial['bookings'] = bookings
105 # Third step: Summary was confirmed, do the work
106 f = PaymentConfirmForm(request.POST)
108 bookings = f.cleaned_data['bookings']
112 b.payment = datetime.datetime.now()
114 confirm_booking(b, 'tickets/payed.eml')
116 # Don't show the form again
118 c['confirmed'] = True
120 # Add the form to show
123 # Add some data about the selected bookings
125 c['bookings'] = bookings
126 c['numtickets'] = sum([b.tickets for b in bookings])
127 c['amount'] = sum([b.price for b in bookings])
129 return render_to_response('tickets/payments.html', c, context_instance=RequestContext(request))
131 @permission_required('tickets.change_booking')
132 def bookings(request):
134 for (show, show_desc) in SHOW_CHOICES:
135 bookings = Booking.objects.filter(show=show)
136 # shows is a list of (show_desc, infodict) tuples
137 shows.append((show_desc, {
138 'bookings' : bookings,
139 'tickets' : sum([b.tickets for b in bookings]),
140 'payed' : sum([b.tickets for b in bookings if b.payment is not None]),
143 c = { 'shows' : shows }
144 return render_to_response('tickets/bookings.html', c, context_instance=RequestContext(request))