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