1 # Create your views here.
4 from django.shortcuts import render_to_response
5 from django.template import RequestContext
7 from models import Booking
9 class BookingForm(django.forms.ModelForm):
13 def confirm_booking(booking):
14 from django.core.mail import EmailMessage
16 context = {'booking' : booking}
18 rendered = django.template.loader.render_to_string('tickets/booked.eml', context)
19 (headers, body) = rendered.strip().split('\n\n', 1)
21 # Turn the headers into a dict so EmailMessage can turn them into a
22 # string again. Bit pointless, but it works.
23 # Perhaps we should just use python email stuff directly. OTOH, we
24 # still always need to parse for the From header.
27 # If no From header is present, let EmailMessage do the default
30 for header in headers.split('\n'):
31 (field, value) = header.split(':')
34 elif (field == 'Subject'):
37 # Don't put From and Subject in the dict, else they'll be
39 headers_dict[field] = value
42 # Only setting the From address through headers won't set the
43 # envelope address right.
44 from_email = from_email,
48 headers = headers_dict
53 if request.method == "POST":
54 f = BookingForm(request.POST)
60 confirm_booking(booking)
61 return render_to_response('tickets/booked.html', {'booking' : booking}, context_instance=RequestContext(request))
63 return render_to_response('tickets/bookingform.html', {'form' : f}, context_instance=RequestContext(request))