emails
[myslice.git] / portal / emailactivationview.py
index 8bb164f..02f56dd 100644 (file)
@@ -7,7 +7,6 @@ from django.http                        import HttpResponse, HttpResponseRedirec
 from django.contrib                     import messages
 from django.contrib.auth.decorators     import login_required
 from django.core.mail                   import EmailMultiAlternatives, send_mail
-from django.contrib.sites.models        import Site
 
 from manifold.core.query                import Query
 from manifoldapi.manifoldapi            import execute_query, execute_admin_query
@@ -17,15 +16,14 @@ from unfold.loginrequired               import FreeAccessView
 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)
+    make_request_user, create_user, send_email_to_pis)
 from portal.models                      import PendingUser, PendingAuthority
 
 from unfold.page                        import Page    
 from ui.topmenu                         import topmenu_items_live, the_user
 
 from myslice.theme                      import ThemeView
-from myslice.settings                   import logger
-
+from myslice.settings import logger
 
 def ValuesQuerySetToDict(vqs):
     return [item for item in vqs]
@@ -58,12 +56,21 @@ class ActivateEmailView(FreeAccessView, ThemeView):
         page = Page(self.request)
         #page.add_js_files  ( [ "js/jquery.validate.js", "js/my_account.register.js", "js/my_account.edit_profile.js" ] )
         #page.add_css_files ( [ "css/onelab.css", "css/account_view.css","css/plugin.css" ] )
+        if self.request.is_secure():
+            current_site = 'https://'
+        else:
+            current_site = 'http://'
+        current_site += self.request.META['HTTP_HOST']
 
         for key, value in kwargs.iteritems():
             if key == "hash_code":
                 hash_code=value
-        if PendingUser.objects.filter(email_hash__iexact = hash_code).filter(status__iexact = 'False'):           
+
+        #if PendingUser.objects.filter(email_hash__iexact = hash_code).filter(status__iexact = 'False'):
+        if PendingUser.objects.filter(email_hash__iexact = hash_code):
             activation = 'success'
+            pending_users = PendingUser.objects.filter(email_hash__iexact = hash_code)
+            pending_user = pending_users[0]
 
             # AUTO VALIDATION of PLE enabled users (only for OneLab Portal)
             if self.theme == "onelab":
@@ -72,14 +79,11 @@ class ActivateEmailView(FreeAccessView, ThemeView):
                 # as we currently need to do a Resolve on each user_hrn of the Registry in order to get its email
                 # TODO in SFA XXX We need a Resolve based on email
                 # TODO maybe we can use MyPLC API for PLE
-                pending_users = PendingUser.objects.filter(email_hash__iexact = hash_code)
 
                 # by default user is not in PLE
                 ple_user_enabled = False
 
-                if pending_users:
-                    pending_user = pending_users[0]
-                    
+                if pending_user:
                     # Auto Validation 
                     if self.is_ple_enabled(pending_user):
                         pending_user_request = make_request_user(pending_user)
@@ -90,37 +94,22 @@ class ActivateEmailView(FreeAccessView, ThemeView):
 
                         # template user auto validated
                         activation = 'validated'
-
-                        # sending email after activation success
-                        #try:
-                        #    # Send an email: the recipient is the user
-                        #    recipients = pending_user_eamil 
-                        #    theme.template_name = 'user_request_email.html'
-                        #    html_content = render_to_string(theme.template, request)
-                        #    theme.template_name = 'user_request_email.txt'
-                        #    text_content = render_to_string(theme.template, request)
-                        #    theme.template_name = 'user_request_email_subject.txt'
-                        #    subject = render_to_string(theme.template, request)
-                        #    subject = subject.replace('\n', '')
-                        #    theme.template_name = 'email_default_sender.txt'
-                        #    sender =  render_to_string(theme.template, request)
-                        #    sender = sender.replace('\n', '')
-                        #    msg = EmailMultiAlternatives(subject, text_content, sender, recipients)
-                        #    msg.attach_alternative(html_content, "text/html")
-                        #    msg.send()
-                        #except Exception as e:
-                        #    logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server")
-                        #    import traceback
-                        #    logger.error(traceback.format_exc())
             
             PendingUser.objects.filter(email_hash__iexact = hash_code).update(status='True')
+            u = {}
+            u['first_name']    =  pending_user.first_name   
+            u['last_name']     =  pending_user.last_name    
+            u['authority_hrn'] =  pending_user.authority_hrn
+            u['email']         =  pending_user.email        
+            u['user_hrn']      =  pending_user.user_hrn     
+            u['pi']            =  pending_user.pi           
+            u['public_key']    =  pending_user.public_key
+            u['current_site']  = current_site
+
+            send_email_to_pis(self.request, u, 'user')
         else:
             activation = 'failed'
         
-        # get the domain url
-        current_site = Site.objects.get_current()
-        current_site = current_site.domain
-
         
         context = super(ActivateEmailView, self).get_context_data(**kwargs)
         context['activation_status'] = activation
@@ -134,10 +123,8 @@ class ActivateEmailView(FreeAccessView, ThemeView):
         #context['first_name'] = first_name
         #context['last_name'] = last_name
         #context['authority_hrn'] = authority_hrn
-        #context['public_key'] = public_key
         #context['email'] = email
         #context['user_hrn'] = user_hrn
-        #context['current_site'] = current_site
         context['theme'] = self.theme
 #        context ['firstname'] = config['firstname']
         prelude_env = page.prelude_env()