1 from django.contrib.auth.decorators import permission_required
2 from django.http import Http404, HttpResponse
3 from django.shortcuts import get_object_or_404, redirect, render
4 from django.utils.translation import ugettext_lazy as _
5 from fnpdjango.utils.views import serve_file
6 from .forms import contact_forms
7 from .models import Attachment
10 def form(request, form_tag):
12 form_class = contact_forms[form_tag]
15 if getattr(form_class, 'disabled', False):
17 if request.method == 'POST':
18 form = form_class(request.POST, request.FILES)
20 valid = form.is_valid()
21 for formset in getattr(form, 'form_formsets', ()):
22 fset = formset(request.POST, request.FILES)
23 if not fset.is_valid():
27 form.save(request, formsets)
28 return redirect('contact_thanks', form_tag)
30 form = form_class(initial=request.GET)
32 for formset in getattr(form, 'form_formsets', ()):
33 formsets.append(formset())
34 return render(request,
35 ['contact/%s/form.html' % form_tag, 'contact/form.html'],
36 {'form': form, 'formsets': formsets}
40 def thanks(request, form_tag):
42 form_class = contact_forms[form_tag]
46 return render(request,
47 ['contact/%s/thanks.html' % form_tag, 'contact/thanks.html'],
48 dict(base_template = getattr(form_class, 'base_template', None))
52 @permission_required('contact.change_attachment')
53 def attachment(request, contact_id, tag):
54 attachment = get_object_or_404(Attachment, contact_id=contact_id, tag=tag)
55 return serve_file(attachment.file.url)