0316b20bd531cc8f07e38f31aed1ae323df2a9e2
[edumed.git] / contact / views.py
1 # -*- coding: utf-8 -*-
2 from urllib import unquote
3
4 from django.contrib.auth.decorators import permission_required
5 from django.http import Http404
6 from django.shortcuts import get_object_or_404, redirect, render
7 from django.views.decorators.cache import never_cache
8 from fnpdjango.utils.views import serve_file
9 from honeypot.decorators import check_honeypot
10
11 from .forms import contact_forms, update_forms
12 from .models import Attachment, Contact
13
14
15 @check_honeypot
16 @never_cache
17 def form(request, form_tag, force_enabled=False, contact_id=None, key=None):
18     update = bool(contact_id and key)
19     try:
20         if update and form_tag in update_forms:
21             form_class = update_forms[form_tag]
22         else:
23             form_class = contact_forms[form_tag]
24     except KeyError:
25         raise Http404
26     if not (force_enabled and request.user.is_superuser):
27         disabled = getattr(form_class, 'disabled', False)
28         if disabled:
29             template = getattr(form_class, 'disabled_template', None)
30             if template:
31                 return render(request, template, {'title': form_class.form_title})
32             raise Http404
33     if contact_id:
34         contact = get_object_or_404(Contact, id=contact_id, form_tag=form_tag)
35         if form_tag != 'olimpiada':
36             raise Http404
37         if key != contact.key:
38             raise Http404
39     else:
40         contact = None
41     if request.method == 'POST':
42         form = form_class(request.POST, request.FILES, instance=contact)
43     else:
44         form = form_class(initial=request.GET, instance=contact)
45     if request.method == 'POST':
46         formsets = form.get_formsets(request)
47         if form.is_valid() and all(formset.is_valid() for formset in formsets.itervalues()):
48             contact = form.save(request, formsets.values())
49             if form.result_page:
50                 return redirect('contact_results', contact.id, contact.digest())
51             else:
52                 return redirect('contact_thanks', form_tag)
53     else:
54         formsets = form.get_formsets()
55
56     return render(
57         request, ['contact/%s/form.html' % form_tag, 'contact/form.html'],
58         {'form': form, 'formsets': formsets, 'formset_errors': any(formset.errors for formset in formsets.values())}
59     )
60
61
62 def thanks(request, form_tag):
63     try:
64         form_class = contact_forms[form_tag]
65     except KeyError:
66         raise Http404
67
68     return render(
69         request, ['contact/%s/thanks.html' % form_tag, 'contact/thanks.html'],
70         {'base_template': getattr(form_class, 'base_template', None)})
71
72
73 def results(request, contact_id, digest):
74     contact = get_object_or_404(Contact, id=contact_id)
75     if digest != contact.digest():
76         raise Http404
77     try:
78         form_class = contact_forms[contact.form_tag]
79     except KeyError:
80         raise Http404
81
82     return render(
83         request, 'contact/%s/results.html' % contact.form_tag,
84         {
85             'results': form_class.results(contact),
86             'base_template': getattr(form_class, 'base_template', None),
87         }
88     )
89
90
91 @permission_required('contact.change_attachment')
92 def attachment(request, contact_id, tag):
93     attachment = get_object_or_404(Attachment, contact_id=contact_id, tag=tag)
94     attachment_url = unquote(attachment.file.url)
95     return serve_file(attachment_url)