home and ticket style
[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             if request.user.is_authenticated() :
37                 username = request.user.email
38             else :
39                 username = None
40             return render(request,'contact_sent.html', { 'theme' : self.theme,  'username': username}) # Redirect after POST
41         else:
42             return self._display (request, form)
43
44     def get (self, request):
45         return self._display (request, ContactForm()) # A fresh unbound form
46         
47     def _display (self, request, form):
48         if request.user.is_authenticated() :
49             username = request.user.email
50         else :
51             username = None
52         return render(request, 'contact.html', {
53                 'form': form,
54                 'topmenu_items': topmenu_items('Contact', request),
55                 'theme' : self.theme,
56                 'username': username,
57                 })