1 from __future__ import print_function
3 from unfold.loginrequired import FreeAccessView
5 from manifold.core.query import Query
6 from manifoldapi.manifoldapi import execute_query, execute_admin_query
7 from portal.actions import manifold_update_user, manifold_update_account, manifold_add_account, manifold_delete_account, sfa_update_user, authority_get_pi_emails, make_request_user, create_user
9 from unfold.page import Page
10 from ui.topmenu import topmenu_items_live, the_user
12 from django.http import HttpResponse, HttpResponseRedirect
13 from django.contrib import messages
14 from django.contrib.auth.decorators import login_required
15 from myslice.theme import ThemeView
16 from portal.models import PendingUser, PendingAuthority
17 from django.core.mail import EmailMultiAlternatives, send_mail
18 from django.contrib.sites.models import Site
21 import json, os, re, itertools
23 def ValuesQuerySetToDict(vqs):
24 return [item for item in vqs]
27 class ActivateEmailView(FreeAccessView, ThemeView):
28 template_name = "email_activation.html"
29 def is_ple_enabled(self, pending_user):
30 pending_authorities = PendingAuthority.objects.filter(site_authority__iexact = pending_user.authority_hrn)
31 if pending_authorities:
33 pending_user_email = pending_user.email
35 query = Query.get('myplcuser').filter_by('email', '==', pending_user_email).select('enabled')
36 results = execute_admin_query(self.request, query)
37 for result in results:
38 # User is enabled in PLE
39 if 'enabled' in result and result['enabled']==True:
42 print("Exception in myplc query = ",e)
46 def dispatch(self, *args, **kwargs):
47 return super(ActivateEmailView, self).dispatch(*args, **kwargs)
49 def get_context_data(self, **kwargs):
51 page = Page(self.request)
52 #page.add_js_files ( [ "js/jquery.validate.js", "js/my_account.register.js", "js/my_account.edit_profile.js" ] )
53 #page.add_css_files ( [ "css/onelab.css", "css/account_view.css","css/plugin.css" ] )
55 for key, value in kwargs.iteritems():
56 #print "%s = %s" % (key, value)
57 if key == "hash_code":
59 if PendingUser.objects.filter(email_hash__iexact = hash_code).filter(status__iexact = 'False'):
60 activation = 'success'
62 # AUTO VALIDATION of PLE enabled users (only for OneLab Portal)
63 if self.theme == "onelab":
64 # Auto-Validation of pending user, which is enabled in a trusted SFA Registry (example: PLE)
65 # We could check in the Registry based on email, but it takes too long
66 # as we currently need to do a Resolve on each user_hrn of the Registry in order to get its email
67 # TODO in SFA XXX We need a Resolve based on email
68 # TODO maybe we can use MyPLC API for PLE
69 pending_users = PendingUser.objects.filter(email_hash__iexact = hash_code)
71 # by default user is not in PLE
72 ple_user_enabled = False
75 pending_user = pending_users[0]
78 if self.is_ple_enabled(pending_user):
79 pending_user_request = make_request_user(pending_user)
80 # Create user in SFA and Update in Manifold
81 create_user(self.request, pending_user_request, namespace = 'myslice', as_admin = True)
83 PendingUser.objects.filter(email_hash__iexact = hash_code).delete()
85 # template user auto validated
86 activation = 'validated'
88 # sending email after activation success
90 # # Send an email: the recipient is the user
91 # recipients = pending_user_eamil
92 # theme.template_name = 'user_request_email.html'
93 # html_content = render_to_string(theme.template, request)
94 # theme.template_name = 'user_request_email.txt'
95 # text_content = render_to_string(theme.template, request)
96 # theme.template_name = 'user_request_email_subject.txt'
97 # subject = render_to_string(theme.template, request)
98 # subject = subject.replace('\n', '')
99 # theme.template_name = 'email_default_sender.txt'
100 # sender = render_to_string(theme.template, request)
101 # sender = sender.replace('\n', '')
102 # msg = EmailMultiAlternatives(subject, text_content, sender, recipients)
103 # msg.attach_alternative(html_content, "text/html")
105 #except Exception, e:
106 # print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
108 # traceback.print_exc()
110 PendingUser.objects.filter(email_hash__iexact = hash_code).update(status='True')
112 activation = 'failed'
115 current_site = Site.objects.get_current()
116 current_site = current_site.domain
119 context = super(ActivateEmailView, self).get_context_data(**kwargs)
120 context['activation_status'] = activation
121 # XXX This is repeated in all pages
122 # more general variables expected in the template
123 context['title'] = 'Platforms connected to MySlice'
124 # the menu items on the top
125 context['topmenu_items'] = topmenu_items_live('My Account', page)
126 # so we can sho who is logged
127 context['username'] = the_user(self.request)
128 #context['first_name'] = first_name
129 #context['last_name'] = last_name
130 #context['authority_hrn'] = authority_hrn
131 #context['public_key'] = public_key
132 #context['email'] = email
133 #context['user_hrn'] = user_hrn
134 #context['current_site'] = current_site
135 context['theme'] = self.theme
136 # context ['firstname'] = config['firstname']
137 prelude_env = page.prelude_env()
138 context.update(prelude_env)