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, authority_get_pis
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
17 from django.contrib.auth.models import User
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'
55 # sending email after activation success
57 request = PendingUser.objects.filter(email_hash= hash_code)
58 split_authority_hrn = request[0].authority_hrn.split('.')[0]
59 pis = authority_get_pis(request, split_authority_hrn)
62 for e in x['pi_users']:
64 y = User.Objects.get(username = u)
65 if y.username.count("@") != 0:
66 if y.username.split("@")[1] == request[0].user_hrn.split("@")[1]:
67 pi_emails += [y.email]
68 subject = 'User email activated'
69 msg = 'The user %s has validated his/her email. Now you can validate his/her account' % (request[0].login)
70 send_mail(subject, msg, 'support@fibre.org.br', pi_emails, fail_silently = False)
72 print "error sending the email!"
74 # Send an email: the recipients are the PI of the authority
75 # If No PI is defined for this Authority, send to a default email (different for each theme)
76 # recipients = authority_get_pi_emails(wsgi_request, authority_hrn)
77 # theme.template_name = 'user_request_email.html'
78 # html_content = render_to_string(theme.template, request)
79 # theme.template_name = 'user_request_email.txt'
80 # text_content = render_to_string(theme.template, request)
81 # theme.template_name = 'user_request_email_subject.txt'
82 # subject = render_to_string(theme.template, request)
83 # subject = subject.replace('\n', '')
84 # theme.template_name = 'email_default_sender.txt'
85 # sender = render_to_string(theme.template, request)
86 # sender = sender.replace('\n', '')
87 # msg = EmailMultiAlternatives(subject, text_content, sender, recipients)
88 # msg.attach_alternative(html_content, "text/html")
90 # except Exception, e:
91 # print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
93 # traceback.print_exc()
95 # AUTO VALIDATION of PLE enabled users (only for OneLab Portal)
96 if self.theme == "onelab":
97 # Auto-Validation of pending user, which is enabled in a trusted SFA Registry (example: PLE)
98 # We could check in the Registry based on email, but it takes too long
99 # as we currently need to do a Resolve on each user_hrn of the Registry in order to get its email
100 # TODO in SFA XXX We need a Resolve based on email
101 # TODO maybe we can use MyPLC API for PLE
102 pending_users = PendingUser.objects.filter(email_hash__iexact = hash_code)
104 # by default user is not in PLE
105 ple_user_enabled = False
108 pending_user = pending_users[0]
111 if self.is_ple_enabled(pending_user):
112 pending_user_request = make_request_user(pending_user)
113 # Create user in SFA and Update in Manifold
114 create_user(self.request, pending_user_request, namespace = 'myslice', as_admin = True)
115 # Delete pending user
116 PendingUser.objects.filter(email_hash__iexact = hash_code).delete()
118 # template user auto validated
119 activation = 'validated'
121 # sending email after activation success
123 # # Send an email: the recipient is the user
124 # recipients = pending_user_eamil
125 # theme.template_name = 'user_request_email.html'
126 # html_content = render_to_string(theme.template, request)
127 # theme.template_name = 'user_request_email.txt'
128 # text_content = render_to_string(theme.template, request)
129 # theme.template_name = 'user_request_email_subject.txt'
130 # subject = render_to_string(theme.template, request)
131 # subject = subject.replace('\n', '')
132 # theme.template_name = 'email_default_sender.txt'
133 # sender = render_to_string(theme.template, request)
134 # sender = sender.replace('\n', '')
135 # msg = EmailMultiAlternatives(subject, text_content, sender, recipients)
136 # msg.attach_alternative(html_content, "text/html")
138 #except Exception, e:
139 # print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
141 # traceback.print_exc()
143 PendingUser.objects.filter(email_hash__iexact = hash_code).update(status='True')
145 activation = 'failed'
148 current_site = Site.objects.get_current()
149 current_site = current_site.domain
152 context = super(ActivateEmailView, self).get_context_data(**kwargs)
153 context['activation_status'] = activation
154 # XXX This is repeated in all pages
155 # more general variables expected in the template
156 context['title'] = 'Platforms connected to MySlice'
157 # the menu items on the top
158 context['topmenu_items'] = topmenu_items_live('My Account', page)
159 # so we can sho who is logged
160 context['username'] = the_user(self.request)
161 #context['first_name'] = first_name
162 #context['last_name'] = last_name
163 #context['authority_hrn'] = authority_hrn
164 #context['public_key'] = public_key
165 #context['email'] = email
166 #context['user_hrn'] = user_hrn
167 #context['current_site'] = current_site
168 context['theme'] = self.theme
169 # context ['firstname'] = config['firstname']
170 prelude_env = page.prelude_env()
171 context.update(prelude_env)