various cleanups and tweaks
[myslice.git] / portal / contactview.py
1 from django.shortcuts           import render
2
3 from django.views.generic       import View
4
5 from myslice.viewutils          import topmenu_items, the_user
6
7 from portal.forms               import ContactForm
8
9 # splitting the 2 functions done here
10 # GET is for displaying the empty form
11 # POST is to process it once filled - or show the form again if anything is missing
12 class ContactView (View):
13     def post (self, request):
14         form = ContactForm(request.POST) # A form bound to the POST data
15         if form.is_valid(): # All validation rules pass
16             # Process the data in form.cleaned_data
17             first_name = form.cleaned_data['first_name']
18             last_name = form.cleaned_data['last_name']
19             affiliation = form.cleaned_data['affiliation']
20             subject = form.cleaned_data['subject']
21             message = form.cleaned_data['message']
22             email = form.cleaned_data['email'] # email of the sender
23             cc_myself = form.cleaned_data['cc_myself']
24
25             #recipients = authority_get_pi_emails(authority_hrn)
26             recipients = ['yasin.upmc@gmail.com', 'thierry.parmentelat@inria.fr', ]
27             if cc_myself:
28                 recipients.append(email)
29
30             from django.core.mail import send_mail
31             send_mail("Onelab user %s submitted a query "%email, 
32                       [first_name,last_name,affiliation,subject,message], 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                 })