de6a31781b16e39aa7075bb43312d278866f3ca4
[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 submitted a query ", [first_name,last_name,affiliation,subject,message], email, recipients)
32             return render(request,'contact_sent.html') # Redirect after POST
33         else:
34             return self._display (request, form)
35
36     def get (self, request):
37         return self._display (request, ContactForm()) # A fresh unbound form
38         
39     def _display (self, request, form):
40         return render(request, 'contact.html', {
41                 'form': form,
42                 'topmenu_items': topmenu_items('Contact Us', request),
43                 'username': the_user (request)
44                 })