Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab
[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
5 from unfold.loginrequired       import FreeAccessView
6 from ui.topmenu                 import topmenu_items, the_user
7
8 from portal.forms               import ContactForm
9
10 from myslice.theme import ThemeView
11
12 # splitting the 2 functions done here
13 # GET is for displaying the empty form
14 # POST is to process it once filled - or show the form again if anything is missing
15 class ContactView (FreeAccessView, ThemeView):
16     def post (self, request):
17         form = ContactForm(request.POST) # A form bound to the POST data
18         if form.is_valid(): # All validation rules pass
19             # Process the data in form.cleaned_data
20             first_name = form.cleaned_data['first_name']
21             last_name = form.cleaned_data['last_name']
22             authority = form.cleaned_data['authority']
23             subject = form.cleaned_data['subject']
24             description = form.cleaned_data['description']
25             email = form.cleaned_data['email'] # email of the sender
26             cc_myself = form.cleaned_data['cc_myself']
27
28             try:
29                 # Send an email: the support recipients
30                 theme.template_name = 'email_support.txt'
31                 recipients = render_to_string(theme.template, form.cleaned_data)
32                 recipients = subject.replace('\n', '')
33                 if cc_myself:
34                     recipients.append(email)
35         
36                 theme.template_name = 'contact_support_email.html'
37                 html_content = render_to_string(theme.template, form.cleaned_data)
38         
39                 theme.template_name = 'contact_support_email.txt'
40                 text_content = render_to_string(theme.template, form.cleaned_data)
41         
42                 theme.template_name = 'contact_support_email_subject.txt'
43                 subject = render_to_string(theme.template, form.cleaned_data)
44                 subject = subject.replace('\n', '')
45         
46                 if not email:
47                     theme.template_name = 'email_default_sender.txt'
48                     sender =  render_to_string(theme.template, form.cleaned_data)
49                     sender = sender.replace('\n', '')
50                 else:
51                     sender = email
52         
53                 msg = EmailMultiAlternatives(subject, text_content, sender, recipients)
54                 msg.attach_alternative(html_content, "text/html")
55                 msg.send()
56             except Exception, e:
57                 print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
58
59             if request.user.is_authenticated() :
60                 username = request.user.email
61             else :
62                 username = None
63             return render(request,'contact_sent.html', { 'theme' : self.theme,  'username': username}) # Redirect after POST
64         else:
65             return self._display (request, form)
66
67     def get (self, request):
68         return self._display (request, ContactForm()) # A fresh unbound form
69         
70     def _display (self, request, form):
71         if request.user.is_authenticated() :
72             username = request.user.email
73         else :
74             username = None
75         return render(request, 'contact.html', {
76                 'form': form,
77                 'topmenu_items': topmenu_items('Contact', request),
78                 'theme' : self.theme,
79                 'username': username,
80                 'section': "Contact"
81                 })