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