F4f Review changes
[unfold.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     template_name = 'contact.html'
18     def post (self, request):
19         form = ContactForm(request.POST) # A form bound to the POST data
20         if form.is_valid(): # All validation rules pass
21             # Process the data in form.cleaned_data
22             #first_name = form.cleaned_data['first_name']
23             #last_name = form.cleaned_data['last_name']
24             #authority = form.cleaned_data['authority']
25             subject = form.cleaned_data['subject']
26             description = form.cleaned_data['description']
27             email = form.cleaned_data['email'] # email of the sender
28             cc_myself = form.cleaned_data['cc_myself']
29
30             #try:
31                 # Send an email: the support recipients
32             #theme.template_name = 'email_support.txt'
33             #recipients = render_to_string(theme.template, form.cleaned_data)
34             #recipients = subject.replace('\n', '')
35             recipients = ['support@myslice.info','contact@fed4fire.eu']
36             if cc_myself:
37                 recipients.append(email)
38             #recipients = ['support@myslice.info']
39             theme.template_name = 'contact_support_email.html'
40             html_content = render_to_string(theme.template, form.cleaned_data)
41         
42             theme.template_name = 'contact_support_email.txt'
43             text_content = render_to_string(theme.template, form.cleaned_data)
44         
45             theme.template_name = 'contact_support_email_subject.txt'
46             subject = render_to_string(theme.template, form.cleaned_data)
47             subject = subject.replace('\n', '')
48         
49             #    if not email:
50             #        theme.template_name = 'email_default_sender.txt'
51             #        sender =  render_to_string(theme.template, form.cleaned_data)
52             #        sender = sender.replace('\n', '')
53             #    else:
54             sender = email
55         
56             msg = EmailMultiAlternatives(subject, text_content, sender, recipients)
57             msg.attach_alternative(html_content, "text/html")
58             msg.send()
59             #except Exception, e:
60                 #print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
61
62             if request.user.is_authenticated() :
63                 username = request.user.email
64             else :
65                 username = None
66             return render(request,'contact_sent.html', { 'theme' : self.theme,  'username': username}) # Redirect after POST
67         else:
68             return self._display (request, form)
69
70     def get (self, request):
71         return self._display (request, ContactForm()) # A fresh unbound form
72         
73     def _display (self, request, form):
74         if request.user.is_authenticated() :
75             username = request.user.email
76         else :
77             username = None
78         return render(request, self.template, {
79                 'form': form,
80                 'topmenu_items': topmenu_items('Contact', request),
81                 'theme' : self.theme,
82                 'username': username,
83                 'section': "Contact"
84                 })