small translation fix
[prawokultury.git] / contact / views.py
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
9
10
11 def form(request, form_tag, key=None):
12     try:
13         form_class = contact_forms[form_tag]
14     except KeyError:
15         raise Http404
16     if request.method == 'POST':
17         form = form_class(request.POST, request.FILES, key=key)
18         if form.is_valid():
19             form.save(request)
20             return redirect('contact_thanks', form_tag)
21     elif key:
22         try:
23             c = Contact.objects.get(form_tag=form_class.save_as_tag, key=key)
24         except Contact.DoesNotExist:
25             raise Http404
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)
31     else:
32         form = form_class()
33     return render(request,
34                 ['contact/%s/form.html' % form_tag, 'contact/form.html'],
35                 {'form': form}
36             )
37
38
39 def thanks(request, form_tag):
40     if form_tag not in contact_forms:
41         raise Http404
42
43     return render(request,
44                 ['contact/%s/thanks.html' % form_tag, 'contact/thanks.html']
45             )
46
47
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))