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