HttpResponse expects content_type, mimetype was an old thing and was removed in 1.7
[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 from manifold.core.query                import Query
10 from manifoldapi.manifoldapi            import execute_query
11 import json
12
13 from myslice.theme import ThemeView
14
15 import activity.user
16
17 theme = ThemeView()
18
19 # splitting the 2 functions done here
20 # GET is for displaying the empty form
21 # POST is to process it once filled - or show the form again if anything is missing
22 class ContactView (FreeAccessView, ThemeView):
23     template_name = 'contact.html'
24     def post (self, request):
25         form = ContactForm(request.POST) # A form bound to the POST data
26         if form.is_valid(): # All validation rules pass
27             # Process the data in form.cleaned_data
28             #first_name = form.cleaned_data['first_name']
29             #last_name = form.cleaned_data['last_name']
30             #authority = form.cleaned_data['authority']
31             subject = form.cleaned_data['subject']
32             description = form.cleaned_data['description']
33             email = form.cleaned_data['email'] # email of the sender
34             #cc_myself = form.cleaned_data['cc_myself']
35
36             #try:
37                 # Send an email: the support recipients
38             theme.template_name = 'email_default_recipients.txt'
39             recipients = render_to_string(theme.template, form.cleaned_data)
40             recipients = recipients.replace('\n', '')
41             #recipients = ['support@onelab.eu']
42             ## removed it cz recipients is not a list so append doesn't work ###
43             ## we don't need it cz the new ticketing systems sends a confirmation email ###
44             #if cc_myself:
45             #    recipients.append(email)
46             theme.template_name = 'contact_support_email.html'
47             html_content = render_to_string(theme.template, form.cleaned_data)
48         
49             theme.template_name = 'contact_support_email.txt'
50             text_content = render_to_string(theme.template, form.cleaned_data)
51         
52             theme.template_name = 'contact_support_email_subject.txt'
53             subject = render_to_string(theme.template, form.cleaned_data)
54             subject = subject.replace('\n', '')
55         
56             #    if not email:
57             #        theme.template_name = 'email_default_sender.txt'
58             #        sender =  render_to_string(theme.template, form.cleaned_data)
59             #        sender = sender.replace('\n', '')
60             #    else:
61             sender = email
62         
63             msg = EmailMultiAlternatives(subject, text_content, sender, [recipients])
64             msg.attach_alternative(html_content, "text/html")
65             msg.send()
66             #except Exception, e:
67                 #print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
68
69             if request.user.is_authenticated() :
70                 username = request.user.email
71             else :
72                 username = None
73             # log user activity
74             activity.user.contact(self.request)
75             return render(request,'contact_sent.html', { 'theme' : self.theme,  'username': username}) # Redirect after POST
76         else:
77             return self._display (request, form)
78
79     def get (self, request):
80         return self._display (request, ContactForm()) # A fresh unbound form
81         
82     def _display (self, request, form):
83         if request.user.is_authenticated():
84             username = request.user.email
85             ## check user is pi or not
86             platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
87             account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
88             platform_details = execute_query(self.request, platform_query)
89             account_details = execute_query(self.request, account_query)
90             for platform_detail in platform_details:
91                 for account_detail in account_details:
92                     if platform_detail['platform_id'] == account_detail['platform_id']:
93                         if 'config' in account_detail and account_detail['config'] is not '':
94                             account_config = json.loads(account_detail['config'])
95                             if 'myslice' in platform_detail['platform']:
96                                 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
97             # assigning values
98             if acc_auth_cred == {} or acc_auth_cred == 'N/A':
99                 pi = "is_not_pi"
100             else:
101                 pi = "is_pi"
102         else :
103             username = None
104             pi = "is_not_pi"
105         return render(request, self.template, {
106                 'form': form,
107                 'topmenu_items': topmenu_items('Contact', request),
108                 'theme' : self.theme,
109                 'username': username,
110                 'pi': pi,
111                 'section': "Contact"
112                 })