a485bc3bae78a9290b1b7e2f70ee426f7230fcaf
[unfold.git] / portal / emailactivationview.py
1 from unfold.loginrequired               import FreeAccessView
2 #
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
6 #
7 from unfold.page                        import Page    
8 from ui.topmenu                         import topmenu_items_live, the_user
9 #
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
15 from django.core.mail                   import EmailMultiAlternatives, send_mail
16 from django.contrib.sites.models        import Site
17
18 #
19 import json, os, re, itertools
20
21 def ValuesQuerySetToDict(vqs):
22     return [item for item in vqs]
23
24
25 # requires login
26 class ActivateEmailView(FreeAccessView, ThemeView):
27     template_name = "email_activation.html"
28     def dispatch(self, *args, **kwargs):
29         return super(ActivateEmailView, self).dispatch(*args, **kwargs)
30
31     def get_context_data(self, **kwargs):
32
33         page = Page(self.request)
34         #page.add_js_files  ( [ "js/jquery.validate.js", "js/my_account.register.js", "js/my_account.edit_profile.js" ] )
35         #page.add_css_files ( [ "css/onelab.css", "css/account_view.css","css/plugin.css" ] )
36
37         for key, value in kwargs.iteritems():
38             #print "%s = %s" % (key, value)
39             if key == "hash_code":
40                 hash_code=value
41         if PendingUser.objects.filter(email_hash__iexact = hash_code).filter(status__iexact = 'False'):           
42             activation = 'success'
43             PendingUser.objects.filter(email_hash__iexact = hash_code).update(status='True')
44
45             # AUTO VALIDATION of PLE enabled users (only for OneLab Portal)
46             if self.theme == "onelab":
47                 # Auto-Validation of pending user, which is enabled in a trusted SFA Registry (example: PLE)
48                 # We could check in the Registry based on email, but it takes too long 
49                 # as we currently need to do a Resolve on each user_hrn of the Registry in order to get its email
50                 # TODO in SFA XXX We need a Resolve based on email
51                 # TODO maybe we can use MyPLC API for PLE
52                 pending_users = PendingUser.objects.filter(email_hash__iexact = hash_code)
53                 if pending_users:
54                     pending_user = pending_users[0]
55                     pending_user_request = make_request_user(pending_user)
56                     pending_user_email = pending_users[0].email
57                     query = Query.get('myplcuser').filter_by('email', '==', pending_user_email).select('enabled')
58                     results = execute_admin_query(self.request, query)
59                     for result in results:
60                         # User is enabled in PLE
61                         if 'enabled' in result and result['enabled']==True:
62                             ple_user_enabled = True
63                             break
64                         else:
65                             ple_user_enabled = False
66
67                     # Auto Validation 
68                     if ple_user_enabled:
69                         # Create user in SFA and Update in Manifold
70                         create_user(self.request, pending_user_request, namespace = 'myslice', as_admin = True)
71                         # Delete pending user
72                         #PendingUser.objects.filter(email_hash__iexact = hash_code).delete()
73
74                         # template user auto validated
75                         activation = 'validated'
76
77                         # sending email after activation success
78                         #try:
79                         #    # Send an email: the recipient is the user
80                         #    recipients = pending_user_eamil 
81                         #    theme.template_name = 'user_request_email.html'
82                         #    html_content = render_to_string(theme.template, request)
83                         #    theme.template_name = 'user_request_email.txt'
84                         #    text_content = render_to_string(theme.template, request)
85                         #    theme.template_name = 'user_request_email_subject.txt'
86                         #    subject = render_to_string(theme.template, request)
87                         #    subject = subject.replace('\n', '')
88                         #    theme.template_name = 'email_default_sender.txt'
89                         #    sender =  render_to_string(theme.template, request)
90                         #    sender = sender.replace('\n', '')
91                         #    msg = EmailMultiAlternatives(subject, text_content, sender, recipients)
92                         #    msg.attach_alternative(html_content, "text/html")
93                         #    msg.send()
94                         #except Exception, e:
95                         #    print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
96                         #    import traceback
97                         #    traceback.print_exc()
98             
99         else:
100             activation = 'failed'
101         
102         # get the domain url
103         current_site = Site.objects.get_current()
104         current_site = current_site.domain
105
106         
107         context = super(ActivateEmailView, self).get_context_data(**kwargs)
108         context['activation_status'] = activation
109         # XXX This is repeated in all pages
110         # more general variables expected in the template
111         context['title'] = 'Platforms connected to MySlice'
112         # the menu items on the top
113         context['topmenu_items'] = topmenu_items_live('My Account', page)
114         # so we can sho who is logged
115         context['username'] = the_user(self.request)
116         #context['first_name'] = first_name
117         #context['last_name'] = last_name
118         #context['authority_hrn'] = authority_hrn
119         #context['public_key'] = public_key
120         #context['email'] = email
121         #context['user_hrn'] = user_hrn
122         #context['current_site'] = current_site
123         context['theme'] = self.theme
124 #        context ['firstname'] = config['firstname']
125         prelude_env = page.prelude_env()
126         context.update(prelude_env)
127         return context