1 from urllib import unquote
2 from django.contrib.auth.decorators import permission_required
3 from django.http import Http404, HttpResponse
4 from django.shortcuts import get_object_or_404, redirect, render
5 from django.utils.translation import ugettext_lazy as _
6 from fnpdjango.utils.views import serve_file
7 from .forms import contact_forms
8 from .models import Attachment, Contact
11 def form(request, form_tag, key=None):
13 form_class = contact_forms[form_tag]
16 if request.method == 'POST':
17 form = form_class(request.POST, request.FILES, key=key)
20 return redirect('contact_thanks', form_tag)
23 c = Contact.objects.get(form_tag=form_class.save_as_tag, key=key)
24 except Contact.DoesNotExist:
26 initial = dict(c.body)
27 initial['contact'] = c.contact
28 for att in c.attachment_set.all():
29 initial[att.tag] = att.file
30 form = form_class(initial=initial, key=key)
33 return render(request,
34 ['contact/%s/form.html' % form_tag, 'contact/form.html'],
39 def thanks(request, form_tag):
40 if form_tag not in contact_forms:
43 return render(request,
44 ['contact/%s/thanks.html' % form_tag, 'contact/thanks.html']
48 @permission_required('contact.change_attachment')
49 def attachment(request, contact_id, tag):
50 attachment = get_object_or_404(Attachment, contact_id=contact_id, tag=tag)
51 return serve_file(unquote(attachment.file.url))