1 from unfold.loginrequired import FreeAccessView
3 from manifold.core.query import Query
4 from manifoldapi.manifoldapi import execute_query, execute_admin_query
5 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
7 from unfold.page import Page
8 from ui.topmenu import topmenu_items_live, the_user
10 from django.http import HttpResponse, HttpResponseRedirect
11 from django.contrib import messages
12 from django.contrib.auth.decorators import login_required
13 from myslice.theme import ThemeView
14 from portal.models import PendingUser, PendingAuthority
15 from django.core.mail import EmailMultiAlternatives, send_mail
16 from django.contrib.sites.models import Site
19 import json, os, re, itertools
21 def ValuesQuerySetToDict(vqs):
22 return [item for item in vqs]
25 class ActivateEmailView(FreeAccessView, ThemeView):
26 template_name = "email_activation.html"
27 def is_ple_enabled(self, pending_user):
28 pending_authorities = PendingAuthority.objects.filter(site_authority__iexact = pending_user.authority_hrn)
29 if pending_authorities:
31 pending_user_email = pending_user.email
32 query = Query.get('myplcuser').filter_by('email', '==', pending_user_email).select('enabled')
33 results = execute_admin_query(self.request, query)
34 for result in results:
35 # User is enabled in PLE
36 if 'enabled' in result and result['enabled']==True:
40 def dispatch(self, *args, **kwargs):
41 return super(ActivateEmailView, self).dispatch(*args, **kwargs)
43 def get_context_data(self, **kwargs):
45 page = Page(self.request)
46 #page.add_js_files ( [ "js/jquery.validate.js", "js/my_account.register.js", "js/my_account.edit_profile.js" ] )
47 #page.add_css_files ( [ "css/onelab.css", "css/account_view.css","css/plugin.css" ] )
49 for key, value in kwargs.iteritems():
50 #print "%s = %s" % (key, value)
51 if key == "hash_code":
53 if PendingUser.objects.filter(email_hash__iexact = hash_code).filter(status__iexact = 'False'):
54 activation = 'success'
56 # AUTO VALIDATION of PLE enabled users (only for OneLab Portal)
57 if self.theme == "onelab":
58 # Auto-Validation of pending user, which is enabled in a trusted SFA Registry (example: PLE)
59 # We could check in the Registry based on email, but it takes too long
60 # as we currently need to do a Resolve on each user_hrn of the Registry in order to get its email
61 # TODO in SFA XXX We need a Resolve based on email
62 # TODO maybe we can use MyPLC API for PLE
63 pending_users = PendingUser.objects.filter(email_hash__iexact = hash_code)
65 # by default user is not in PLE
66 ple_user_enabled = False
69 pending_user = pending_users[0]
72 if self.is_ple_enabled(pending_user):
73 pending_user_request = make_request_user(pending_user)
74 # Create user in SFA and Update in Manifold
75 create_user(self.request, pending_user_request, namespace = 'myslice', as_admin = True)
77 PendingUser.objects.filter(email_hash__iexact = hash_code).delete()
79 # template user auto validated
80 activation = 'validated'
82 # sending email after activation success
84 # # Send an email: the recipient is the user
85 # recipients = pending_user_eamil
86 # theme.template_name = 'user_request_email.html'
87 # html_content = render_to_string(theme.template, request)
88 # theme.template_name = 'user_request_email.txt'
89 # text_content = render_to_string(theme.template, request)
90 # theme.template_name = 'user_request_email_subject.txt'
91 # subject = render_to_string(theme.template, request)
92 # subject = subject.replace('\n', '')
93 # theme.template_name = 'email_default_sender.txt'
94 # sender = render_to_string(theme.template, request)
95 # sender = sender.replace('\n', '')
96 # msg = EmailMultiAlternatives(subject, text_content, sender, recipients)
97 # msg.attach_alternative(html_content, "text/html")
100 # print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
102 # traceback.print_exc()
104 PendingUser.objects.filter(email_hash__iexact = hash_code).update(status='True')
106 activation = 'failed'
109 current_site = Site.objects.get_current()
110 current_site = current_site.domain
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['public_key'] = public_key
126 #context['email'] = email
127 #context['user_hrn'] = user_hrn
128 #context['current_site'] = current_site
129 context['theme'] = self.theme
130 # context ['firstname'] = config['firstname']
131 prelude_env = page.prelude_env()
132 context.update(prelude_env)