6 from django.http import HttpResponse, HttpResponseRedirect
7 from django.contrib import messages
8 from django.contrib.auth.decorators import login_required
9 from django.core.mail import EmailMultiAlternatives, send_mail
11 from manifold.core.query import Query
12 from manifoldapi.manifoldapi import execute_query, execute_admin_query
14 from unfold.loginrequired import FreeAccessView
16 from portal.actions import (
17 manifold_update_user, manifold_update_account, manifold_add_account,
18 manifold_delete_account, sfa_update_user, authority_get_pi_emails,
19 make_request_user, create_user, send_email_to_pis)
20 from portal.models import PendingUser, PendingAuthority
22 from unfold.page import Page
23 from ui.topmenu import topmenu_items_live, the_user
25 from myslice.theme import ThemeView
26 from myslice.settings import logger
28 def ValuesQuerySetToDict(vqs):
29 return [item for item in vqs]
32 class ActivateEmailView(FreeAccessView, ThemeView):
33 template_name = "email_activation.html"
34 def is_ple_enabled(self, pending_user):
35 pending_authorities = PendingAuthority.objects.filter(site_authority__iexact = pending_user.authority_hrn)
36 if pending_authorities:
38 pending_user_email = pending_user.email
40 query = Query.get('myplcuser').filter_by('email', '==', pending_user_email).select('enabled')
41 results = execute_admin_query(self.request, query)
42 for result in results:
43 # User is enabled in PLE
44 if 'enabled' in result and result['enabled']==True:
46 except Exception as e:
47 logger.error("Exception in myplc query = {}".format(e))
51 def dispatch(self, *args, **kwargs):
52 return super(ActivateEmailView, self).dispatch(*args, **kwargs)
54 def get_context_data(self, **kwargs):
56 page = Page(self.request)
57 #page.add_js_files ( [ "js/jquery.validate.js", "js/my_account.register.js", "js/my_account.edit_profile.js" ] )
58 #page.add_css_files ( [ "css/onelab.css", "css/account_view.css","css/plugin.css" ] )
59 if self.request.is_secure():
60 current_site = 'https://'
62 current_site = 'http://'
63 current_site += self.request.META['HTTP_HOST']
65 for key, value in kwargs.iteritems():
66 if key == "hash_code":
69 if PendingUser.objects.filter(email_hash__iexact = hash_code).filter(status__iexact = 'False'):
70 activation = 'success'
71 pending_users = PendingUser.objects.filter(email_hash__iexact = hash_code)
72 pending_user = pending_users[0]
74 # AUTO VALIDATION of PLE enabled users (only for OneLab Portal)
75 if self.theme == "onelab":
76 # Auto-Validation of pending user, which is enabled in a trusted SFA Registry (example: PLE)
77 # We could check in the Registry based on email, but it takes too long
78 # as we currently need to do a Resolve on each user_hrn of the Registry in order to get its email
79 # TODO in SFA XXX We need a Resolve based on email
80 # TODO maybe we can use MyPLC API for PLE
82 # by default user is not in PLE
83 ple_user_enabled = False
87 if self.is_ple_enabled(pending_user):
88 pending_user_request = make_request_user(pending_user)
89 # Create user in SFA and Update in Manifold
90 create_user(self.request, pending_user_request, namespace = 'myslice', as_admin = True)
92 PendingUser.objects.filter(email_hash__iexact = hash_code).delete()
94 # template user auto validated
95 activation = 'validated'
97 PendingUser.objects.filter(email_hash__iexact = hash_code).update(status='True')
99 u['first_name'] = pending_user.first_name
100 u['last_name'] = pending_user.last_name
101 u['authority_hrn'] = pending_user.authority_hrn
102 u['email'] = pending_user.email
103 u['user_hrn'] = pending_user.user_hrn
104 u['pi'] = pending_user.pi
105 u['public_key'] = pending_user.public_key
106 u['current_site'] = current_site
108 send_email_to_pis(self.request, u, 'user')
110 activation = 'failed'
113 context = super(ActivateEmailView, self).get_context_data(**kwargs)
114 context['activation_status'] = activation
115 # XXX This is repeated in all pages
116 # more general variables expected in the template
117 context['title'] = 'Platforms connected to MySlice'
118 # the menu items on the top
119 context['topmenu_items'] = topmenu_items_live('My Account', page)
120 # so we can sho who is logged
121 context['username'] = the_user(self.request)
122 #context['first_name'] = first_name
123 #context['last_name'] = last_name
124 #context['authority_hrn'] = authority_hrn
125 #context['email'] = email
126 #context['user_hrn'] = user_hrn
127 context['theme'] = self.theme
128 # context ['firstname'] = config['firstname']
129 prelude_env = page.prelude_env()
130 context.update(prelude_env)