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 from django.core.mail           import EmailMultiAlternatives, send_mail
8 from portal.forms               import ContactForm
9
10 from myslice.theme import ThemeView
11 theme = 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             #try:
30                 # Send an email: the support recipients
31             #theme.template_name = 'email_support.txt'
32             #recipients = render_to_string(theme.template, form.cleaned_data)
33             #recipients = subject.replace('\n', '')
34             recipients = ['support@myslice.info']
35             if cc_myself:
36                 recipients.append(email)
37             #recipients = ['support@myslice.info']
38             theme.template_name = 'contact_support_email.html'
39             html_content = render_to_string(theme.template, form.cleaned_data)
40         
41             theme.template_name = 'contact_support_email.txt'
42             text_content = render_to_string(theme.template, form.cleaned_data)
43         
44             theme.template_name = 'contact_support_email_subject.txt'
45             subject = render_to_string(theme.template, form.cleaned_data)
46             subject = subject.replace('\n', '')
47         
48             #    if not email:
49             #        theme.template_name = 'email_default_sender.txt'
50             #        sender =  render_to_string(theme.template, form.cleaned_data)
51             #        sender = sender.replace('\n', '')
52             #    else:
53             sender = email
54         
55             msg = EmailMultiAlternatives(subject, text_content, sender, recipients)
56             msg.attach_alternative(html_content, "text/html")
57             msg.send()
58             #except Exception, e:
59                 #print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
60
61             if request.user.is_authenticated() :
62                 username = request.user.email
63             else :
64                 username = None
65             return render(request,'contact_sent.html', { 'theme' : self.theme,  'username': username}) # Redirect after POST
66         else:
67             return self._display (request, form)
68
69     def get (self, request):
70         return self._display (request, ContactForm()) # A fresh unbound form
71         
72     def _display (self, request, form):
73         if request.user.is_authenticated() :
74             username = request.user.email
75         else :
76             username = None
77         return render(request, 'contact.html', {
78                 'form': form,
79                 'topmenu_items': topmenu_items('Contact', request),
80                 'theme' : self.theme,
81                 'username': username,
82                 'section': "Contact"
83                 })