Removing empty lines from admin report csv file
[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         template = getattr(form_class, 'disabled_template', None)
17         if template:
18             return render(request, template)
19         raise Http404
20     if request.method == 'POST':
21         form = form_class(request.POST, request.FILES)
22         formsets = []
23         valid = form.is_valid()
24         for formset in getattr(form, 'form_formsets', ()):
25             fset = formset(request.POST, request.FILES)
26             if not fset.is_valid():
27                 valid = False
28             formsets.append(fset)
29         if valid:
30             form.save(request, formsets)
31             return redirect('contact_thanks', form_tag)
32     else:
33         form = form_class(initial=request.GET)
34         formsets = []
35         for formset in getattr(form, 'form_formsets', ()):
36             formsets.append(formset())
37     return render(request,
38                 ['contact/%s/form.html' % form_tag, 'contact/form.html'],
39                 {'form': form, 'formsets': formsets}
40             )
41
42
43 def thanks(request, form_tag):
44     try:
45         form_class = contact_forms[form_tag]
46     except KeyError:
47         raise Http404
48
49     return render(request,
50                 ['contact/%s/thanks.html' % form_tag, 'contact/thanks.html'],
51                 dict(base_template = getattr(form_class, 'base_template', None))
52             )
53
54
55 @permission_required('contact.change_attachment')
56 def attachment(request, contact_id, tag):
57     attachment = get_object_or_404(Attachment, contact_id=contact_id, tag=tag)
58     return serve_file(attachment.file.url)