1 from django.shortcuts import render
2 from django.template.loader import render_to_string
3 from django.views.generic import View
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
10 from myslice.theme import ThemeView
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']
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']
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)
42 theme.template_name = 'contact_support_email.txt'
43 text_content = render_to_string(theme.template, form.cleaned_data)
45 theme.template_name = 'contact_support_email_subject.txt'
46 subject = render_to_string(theme.template, form.cleaned_data)
47 subject = subject.replace('\n', '')
50 # theme.template_name = 'email_default_sender.txt'
51 # sender = render_to_string(theme.template, form.cleaned_data)
52 # sender = sender.replace('\n', '')
56 msg = EmailMultiAlternatives(subject, text_content, sender, recipients)
57 msg.attach_alternative(html_content, "text/html")
60 #print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
62 if request.user.is_authenticated() :
63 username = request.user.email
66 return render(request,'contact_sent.html', { 'theme' : self.theme, 'username': username}) # Redirect after POST
68 return self._display (request, form)
70 def get (self, request):
71 return self._display (request, ContactForm()) # A fresh unbound form
73 def _display (self, request, form):
74 if request.user.is_authenticated() :
75 username = request.user.email
78 return render(request, self.template, {
80 'topmenu_items': topmenu_items('Contact', request),