X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=portal%2Factions.py;h=b9a07378b1ed607af4ba4695656273b4e2d9f04d;hb=d04f3fd85686bee03a961494bdb4a68d6fb07ad4;hp=64f4802751e488f16bd6c2abd00e1d70f03648e6;hpb=21694dbfd43b8a4d9649ca84d4530e3778f5db39;p=unfold.git diff --git a/portal/actions.py b/portal/actions.py index 64f48027..b9a07378 100644 --- a/portal/actions.py +++ b/portal/actions.py @@ -1,12 +1,16 @@ -from django.http import HttpResponse -from manifold.core.query import Query -from manifoldapi.manifoldapi import execute_query,execute_admin_query -from portal.models import PendingUser, PendingSlice, PendingAuthority +from django.http import HttpResponse +from manifold.core.query import Query +from manifoldapi.manifoldapi import execute_query,execute_admin_query +from portal.models import PendingUser, PendingSlice, PendingAuthority import json from django.contrib.auth.models import User -from django.template.loader import render_to_string -from django.core.mail import send_mail +from django.template.loader import render_to_string +from django.core.mail import EmailMultiAlternatives, send_mail + +from myslice.theme import ThemeView + +theme = ThemeView() # Thierry: moving this right into the code so # most people can use myslice without having to install sfa @@ -19,6 +23,7 @@ from django.core.mail import send_mail def authority_get_pis(request, authority_hrn): query = Query.get('authority').filter_by('authority_hrn', '==', authority_hrn).select('pi_users') results = execute_admin_query(request, query) + print "authority_get_pis = %s" % results # NOTE: temporarily commented. Because results is giving empty list. # Needs more debugging #if not results: @@ -29,7 +34,14 @@ def authority_get_pis(request, authority_hrn): def authority_get_pi_emails(request, authority_hrn): pi_users = authority_get_pis(request,authority_hrn) + print "pi_users = %s" % pi_users + if any(d['pi_users'] == None for d in pi_users): + #theme.template_name = 'email_default_recipients.txt' + #default_email = render_to_string(theme.template, request) + #default_email = default_email.replace('\n', '') + #return default_email + # the above doesn't work return ['support@myslice.info'] else: pi_user_hrns = [ hrn for x in pi_users for hrn in x['pi_users'] ] @@ -156,7 +168,7 @@ def manifold_add_platform(request, platform_params): query = Query.create('local:platform').set(platform_params).select(['user', 'platform']) results = execute_admin_query(request,query) if not results: - raise Exception, "Failed creating manifold platform %s for user: %s" % (platform_params['platform'], platform_params['user']) + raise Exception, "Failed creating manifold platform %s for user: %s" % (platform_params['platform'], platform_params['user']) result, = results return result['platform_id'] @@ -272,7 +284,7 @@ def portal_validate_request(wsgi_request, request_ids): try: create_user(wsgi_request, request) request_status['SFA user'] = {'status': True } - + PendingUser.objects.get(id=request['id']).delete() except Exception, e: request_status['SFA user'] = {'status': False, 'description': str(e)} @@ -296,6 +308,7 @@ def portal_validate_request(wsgi_request, request_ids): try: create_slice(wsgi_request, request) request_status['SFA slice'] = {'status': True } + PendingSlice.objects.get(id=request['id']).delete() except Exception, e: request_status['SFA slice'] = {'status': False, 'description': str(e)} @@ -318,6 +331,7 @@ def portal_validate_request(wsgi_request, request_ids): print "ADD Authority" sfa_add_authority(wsgi_request, sfa_authority_params) request_status['SFA authority'] = {'status': True } + PendingAuthority.objects.get(id=request['id']).delete() except Exception, e: request_status['SFA authority'] = {'status': False, 'description': str(e)} @@ -392,10 +406,26 @@ def create_pending_slice(wsgi_request, request, email): ) s.save() - # Send an email: the recipients are the PI of the authority - recipients = authority_get_pi_emails(wsgi_request, request['authority_hrn']) - msg = render_to_string('slice-request-email.txt', request) - send_mail("Onelab user %(email)s requested a slice" % locals(), msg, email, recipients) + try: + # Send an email: the recipients are the PI of the authority + recipients = authority_get_pi_emails(wsgi_request, request['authority_hrn']) + + theme.template_name = 'slice_request_email.txt' + text_content = render_to_string(theme.template, request) + + theme.template_name = 'slice_request_email.html' + html_content = render_to_string(theme.template, request) + + theme.template_name = 'slice_request_email_subject.txt' + subject = render_to_string(theme.template, request) + subject = subject.replace('\n', '') + + sender = email + msg = EmailMultiAlternatives(subject, text_content, sender, recipients) + msg.attach_alternative(html_content, "text/html") + msg.send() + except Exception, e: + print "Failed to send email, please check the mail templates and the SMTP configuration of your server" #------------------------------------------------------------------------------- # REQUESTS - Users @@ -508,9 +538,26 @@ def create_pending_user(wsgi_request, request, user_detail): private_key = request['private_key'], user_hrn = request['user_hrn'], pi = '', # XXX Why not None ? + email_hash = request['email_hash'], + status = 'False', ) b.save() - + # sends email to user to activate the email + theme.template_name = 'activate_user.html' + html_content = render_to_string(theme.template, request) + theme.template_name = 'activate_user.txt' + text_content = render_to_string(theme.template, request) + theme.template_name = 'activate_user_email_subject.txt' + subject = render_to_string(theme.template, request) + subject = subject.replace('\n', '') + sender = 'support@myslice.info' + recipient = [request['email']] + #recipient = recipient.append(request['email']) + + msg = EmailMultiAlternatives(subject, text_content, sender, recipient) + msg.attach_alternative(html_content, "text/html") + msg.send() + # saves the user to django auth_user table [needed for password reset] user = User.objects.create_user(request['email'], request['email'], request['password']) @@ -527,15 +574,52 @@ def create_pending_user(wsgi_request, request, user_detail): account_config['user_private_key'] = request['private_key'] user_id = user_detail['user_id'] + 1 # the user_id for the newly created user in local:user - account_params = { - 'platform_id' : 5, # XXX ALERT !! - 'user_id' : user_id, - 'auth_type' : request['auth_type'], - 'config' : json.dumps(account_config), - } - manifold_add_account(wsgi_request, account_params) - # Send an email: the recipients are the PI of the authority - recipients = authority_get_pi_emails(wsgi_request, request['authority_hrn']) - msg = render_to_string('user_request_email.txt', request) - send_mail("Onelab New User request for %(email)s submitted" % request, msg, 'support@myslice.info', recipients) + # XXX TODO: Require a myslice platform + # ALERT: this will disapear with ROUTERV2 of Manifold + # We have to consider the case where several registries can be used + # Removed hardcoded platform = 5 + # This platform == 'myslice' is a TMP FIX !! + try: + reg_platform_query = Query().get('local:platform') \ + .filter_by('platform', '==', 'myslice') \ + .select('platform_id') + reg_platform = execute_admin_query(wsgi_request, reg_platform_query) + + reg_platform_id = reg_platform[0]['platform_id'] + account_params = { + 'platform_id' : reg_platform_id, # XXX ALERT !! + 'user_id' : user_id, + 'auth_type' : request['auth_type'], + 'config' : json.dumps(account_config), + } + manifold_add_account(wsgi_request, account_params) + except Exception, e: + print "Failed creating manifold account on platform %s for user: %s" % ('myslice', request['email']) + + try: + # Send an email: the recipients are the PI of the authority + # If No PI is defined for this Authority, send to a default email (different for each theme) + recipients = authority_get_pi_emails(wsgi_request, request['authority_hrn']) + + 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, e: + print "Failed to send email, please check the mail templates and the SMTP configuration of your server" + import traceback + traceback.print_exc()