Lack of information is better that untrue information
[edumed.git] / contact / views.py
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
8
9
10 def form(request, form_tag):
11     try:
12         form_class = contact_forms[form_tag]
13     except KeyError:
14         raise Http404
15     if getattr(form_class, 'disabled', False):
16         raise Http404
17     if request.method == 'POST':
18         form = form_class(request.POST, request.FILES)
19         formsets = []
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():
24                 valid = False
25             formsets.append(fset)
26         if valid:
27             form.save(request, formsets)
28             return redirect('contact_thanks', form_tag)
29     else:
30         form = form_class(initial=request.GET)
31         formsets = []
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}
37             )
38
39
40 def thanks(request, form_tag):
41     try:
42         form_class = contact_forms[form_tag]
43     except KeyError:
44         raise Http404
45
46     return render(request,
47                 ['contact/%s/thanks.html' % form_tag, 'contact/thanks.html'],
48                 dict(base_template = getattr(form_class, 'base_template', None))
49             )
50
51
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)