1 # Create your views here.
6 from django.shortcuts import render_to_response
7 from django.template import RequestContext
9 from models import Booking
11 class BookingForm(django.forms.ModelForm):
16 def confirm_booking(booking, template):
17 from django.core.mail import EmailMessage
19 context = {'booking' : booking}
21 rendered = django.template.loader.render_to_string(template, context)
22 (headers, body) = rendered.strip().split('\n\n', 1)
24 # Turn the headers into a dict so EmailMessage can turn them into a
25 # string again. Bit pointless, but it works.
26 # Perhaps we should just use python email stuff directly. OTOH, we
27 # still always need to parse for the From header.
30 # If no From header is present, let EmailMessage do the default
33 for header in headers.split('\n'):
34 (field, value) = header.split(':')
37 elif (field == 'Subject'):
40 # Don't put From and Subject in the dict, else they'll be
42 headers_dict[field] = value
45 # Only setting the From address through headers won't set the
46 # envelope address right.
47 from_email = from_email,
51 headers = headers_dict
56 if request.method == "POST":
57 f = BookingForm(request.POST)
63 confirm_booking(booking, 'tickets/booked.eml')
64 return render_to_response('tickets/booked.html', {'booking' : booking}, context_instance=RequestContext(request))
66 return render_to_response('tickets/bookingform.html', {'form' : f}, context_instance=RequestContext(request))
68 # These two forms are used for entering payment details. They aren't
69 # very different, so it should really be possible to merge them (but
70 # initial attempts to add the confirm field dynamically or setting its
71 # value dynamically, didn't work out all too well, so leave it at this
73 class PaymentForm(django.forms.Form):
74 bookings = django.forms.models.ModelMultipleChoiceField(
75 queryset=Booking.objects.filter(payment__isnull=True).order_by('pk'),
76 widget=django.forms.CheckboxSelectMultiple,
77 label="Reserveringen")
79 class PaymentConfirmForm(django.forms.Form):
80 bookings = django.forms.models.ModelMultipleChoiceField(
81 queryset=Booking.objects.filter(payment__isnull=True).order_by('pk'),
82 widget=django.forms.MultipleHiddenInput,
83 label="Reserveringen")
84 # This field is used to distinguish these two forms
85 confirm = django.forms.BooleanField(initial=True, widget=django.forms.HiddenInput)
87 def payments(request):
90 if request.method != "POST":
91 # First step: Just show an empty form
93 elif not 'confirm' in request.POST:
94 # Second step: Process form and show a summary for confirmation
95 f = PaymentForm(request.POST)
97 bookings = f.cleaned_data['bookings']
98 # Output a confirmation form
99 f = PaymentConfirmForm()
100 f.initial['bookings'] = bookings
103 # Third step: Summary was confirmed, do the work
104 f = PaymentConfirmForm(request.POST)
106 bookings = f.cleaned_data['bookings']
110 b.payment = datetime.datetime.now()
112 confirm_booking(b, 'tickets/payed.eml')
114 # Don't show the form again
116 c['confirmed'] = True
118 # Add the form to show
121 # Add some data about the selected bookings
123 c['bookings'] = bookings
124 c['numtickets'] = sum([b.tickets for b in bookings])
125 c['amount'] = sum([b.price for b in bookings])
127 return render_to_response('tickets/payments.html', c, context_instance=RequestContext(request))