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
6 from unfold.loginrequired import FreeAccessView
7 from ui.topmenu import topmenu_items, the_user
9 from portal.forms import ContactForm
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']
27 #recipients = authority_get_pi_emails(authority_hrn)
28 recipients = ['yasin.upmc@gmail.com', 'thierry.parmentelat@inria.fr', ]
30 recipients.append(email)
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
36 return self._display (request, form)
38 def get (self, request):
39 return self._display (request, ContactForm()) # A fresh unbound form
41 def _display (self, request, form):
42 return render(request, 'contact.html', {
44 'topmenu_items': topmenu_items('Contact', request),
45 'username': the_user (request)