typo
[unfold.git] / portal / contactview.py
1 from django.shortcuts           import render
2 from django.template.loader     import render_to_string
3 from django.views.generic       import View
4 from django.core.mail           import send_mail
5
6 from unfold.loginrequired       import FreeAccessView
7 from ui.topmenu                 import topmenu_items, the_user
8
9 from portal.forms               import ContactForm
10
11 # splitting the 2 functions done here
12 # GET is for displaying the empty form
13 # POST is to process it once filled - or show the form again if anything is missing
14 class ContactView (FreeAccessView):
15     def post (self, request):
16         form = ContactForm(request.POST) # A form bound to the POST data
17         if form.is_valid(): # All validation rules pass
18             # Process the data in form.cleaned_data
19             first_name = form.cleaned_data['first_name']
20             last_name = form.cleaned_data['last_name']
21             affiliation = form.cleaned_data['affiliation']
22             subject = form.cleaned_data['subject']
23             message = form.cleaned_data['message']
24             email = form.cleaned_data['email'] # email of the sender
25             cc_myself = form.cleaned_data['cc_myself']
26
27             #recipients = authority_get_pi_emails(authority_hrn)
28             recipients = ['yasin.upmc@gmail.com', 'thierry.parmentelat@inria.fr', ]
29             if cc_myself:
30                 recipients.append(email)
31
32             msg = render_to_string('slice-request-email.txt', form.cleaned_data)
33             send_mail("Onelab user %s submitted a query "%email, msg, email, recipients)
34             return render(request,'contact_sent.html') # Redirect after POST
35         else:
36             return self._display (request, form)
37
38     def get (self, request):
39         return self._display (request, ContactForm()) # A fresh unbound form
40         
41     def _display (self, request, form):
42         return render(request, 'contact.html', {
43                 'form': form,
44                 'topmenu_items': topmenu_items('Contact', request),
45                 'username': the_user (request)
46                 })