From: Yasin Date: Wed, 21 Jan 2015 17:31:54 +0000 (+0100) Subject: Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab X-Git-Tag: myslice-1.3~113 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=5826537e8d4519f35cdb55769794c95edef7e3dc;hp=67dd0c2e73f832813a1b51390264191a49a643f0;p=myslice.git Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab --- diff --git a/auth/static/js/logout.js b/auth/static/js/logout.js index 593eb588..3419b9d2 100644 --- a/auth/static/js/logout.js +++ b/auth/static/js/logout.js @@ -5,7 +5,7 @@ function logout () { var msg="Are you sure you want to logout as " + username + " ?"; /* redirect to /logout, see urls.py */ if (confirm(msg)){ - localStorage.removeItem('user'); + localStorage.clear(); window.location="/logout/"; } } diff --git a/portal/institution.py b/portal/institution.py index ccdb6251..cf0055ce 100644 --- a/portal/institution.py +++ b/portal/institution.py @@ -30,29 +30,32 @@ class InstitutionView (LoginRequiredAutoLogoutView, ThemeView): env['theme'] = self.theme return render_to_response(self.template, env, context_instance=RequestContext(request)) - def get (self, request, state=None): + def get (self, request, authority_hrn=None, state=None): + print " ----- institution",authority_hrn env = self.default_env() - if request.user.is_authenticated(): env['person'] = self.request.user - user_query = Query().get('myslice:user').select('user_hrn','parent_authority').filter_by('user_hrn','==','$user_hrn') - user_details = execute_query(self.request, user_query) - try: - env['user_details'] = user_details[0] - except Exception,e: - env['error'] = "Please check your Credentials" - - try: - user_local_query = Query().get('local:user').select('config').filter_by('email','==',str(env['person'])) - user_local_details = execute_query(self.request, user_local_query) - user_local = user_local_details[0] - user_local_config = user_local['config'] - user_local_config = json.loads(user_local_config) - user_local_authority = user_local_config.get('authority') - if 'user_details' not in env or 'parent_authority' not in env['user_details'] or env['user_details']['parent_authority'] is None: - env['user_details'] = {'parent_authority': user_local_authority} - except Exception,e: - env['error'] = "Please check your Manifold user config" + if authority_hrn is None: + user_query = Query().get('myslice:user').select('user_hrn','parent_authority').filter_by('user_hrn','==','$user_hrn') + user_details = execute_query(self.request, user_query) + try: + env['user_details'] = user_details[0] + except Exception,e: + # If the Query fails, check in local DB + try: + user_local_query = Query().get('local:user').select('config').filter_by('email','==',str(env['person'])) + user_local_details = execute_query(self.request, user_local_query) + user_local = user_local_details[0] + user_local_config = user_local['config'] + user_local_config = json.loads(user_local_config) + user_local_authority = user_local_config.get('authority') + if 'user_details' not in env or 'parent_authority' not in env['user_details'] or env['user_details']['parent_authority'] is None: + env['user_details'] = {'parent_authority': user_local_authority} + except Exception,e: + env['error'] = "Please check your Credentials" + else: + env['project'] = True + env['user_details'] = {'parent_authority': authority_hrn} ## check user is pi or not platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled') account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config') diff --git a/portal/projectrequestview.py b/portal/projectrequestview.py new file mode 100644 index 00000000..d2f11a6a --- /dev/null +++ b/portal/projectrequestview.py @@ -0,0 +1,193 @@ +from django.shortcuts import render +from django.contrib.sites.models import Site + + +from unfold.page import Page + +from manifold.core.query import Query +from manifoldapi.manifoldapi import execute_admin_query, execute_query + +from portal.actions import is_pi, create_slice, create_pending_slice, clear_user_creds +#from portal.forms import SliceRequestForm +from unfold.loginrequired import LoginRequiredAutoLogoutView +from ui.topmenu import topmenu_items_live, the_user + +from myslice.theme import ThemeView + +import json, time, re + +import activity.user + +class ProjectRequestView (LoginRequiredAutoLogoutView, ThemeView): + template_name = 'projectrequest_view.html' + + # because we inherit LoginRequiredAutoLogoutView that is implemented by redefining 'dispatch' + # we cannot redefine dispatch here, or we'd lose LoginRequired and AutoLogout behaviours + def post (self, request): + return self.get_or_post (request, 'POST') + + def get (self, request): + return self.get_or_post (request, 'GET') + + def get_or_post (self, wsgi_request, method): + """ + """ + errors = [] + slice_name ='' + purpose='' + url='' + authority_hrn = None + authority_name = None + # Retrieve the list of authorities + authorities_query = Query.get('authority').select('name', 'authority_hrn') + authorities = execute_admin_query(wsgi_request, authorities_query) + if authorities is not None: + authorities = sorted(authorities, key=lambda k: k['authority_hrn']) + authorities = sorted(authorities, key=lambda k: k['name']) + + # Get user_email (XXX Would deserve to be simplified) + user_query = Query().get('local:user').select('email','config') + user_details = execute_query(wsgi_request, user_query) + user_email = user_details[0].get('email') + # getting user_hrn + for user_detail in user_details: + user_config = json.loads(user_detail['config']) + user_authority = user_config.get('authority','N/A') + # getting the org from authority + for authority in authorities: + if authority['authority_hrn'] == user_authority: + authority_name = authority['name'] + + # Handle the case when we use only hrn and not name + if authority_name is None: + authority_name = user_authority + # + account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config') + account_details = execute_query(wsgi_request, account_query) + # + platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled') + platform_details = execute_query(wsgi_request, platform_query) + user_hrn = None + # getting user_hrn from local:account + for account_detail in account_details: + for platform_detail in platform_details: + if platform_detail['platform_id'] == account_detail['platform_id']: + # taking user_hrn only from myslice account + # NOTE: we should later handle accounts filter_by auth_type= managed OR user + if 'myslice' in platform_detail['platform']: + account_config = json.loads(account_detail['config']) + user_hrn = account_config.get('user_hrn','N/A') + acc_auth_cred = account_config.get('delegated_authority_credentials','N/A') + + + # checking if pi or not + if acc_auth_cred == {} or acc_auth_cred == 'N/A': + pi = "is_not_pi" + else: + pi = "is_pi" + + + # Page rendering + page = Page(wsgi_request) + page.add_js_files ( [ "js/jquery.validate.js", "js/jquery-ui.js" ] ) + page.add_css_files ( [ "https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" ] ) + page.expose_js_metadata() + + if method == 'POST': + # The form has been submitted + + # get the domain url + current_site = Site.objects.get_current() + current_site = current_site.domain + + # getting the authority_hrn from the selected organization + for authority in authorities: + if authority['name'] == wsgi_request.POST.get('org_name', ''): + authority_hrn = authority['authority_hrn'] + + # Handle the case when we use only hrn and not name + if authority_hrn is None: + authority_hrn = wsgi_request.POST.get('org_name', '') + + slice_request = { + 'type' : 'slice', + 'id' : None, + 'user_hrn' : user_hrn, + 'email' : user_email, + 'timestamp' : time.time(), + 'authority_hrn' : authority_hrn, + 'organization' : wsgi_request.POST.get('org_name', ''), + 'slice_name' : wsgi_request.POST.get('slice_name', ''), + 'url' : wsgi_request.POST.get('url', ''), + 'purpose' : wsgi_request.POST.get('purpose', ''), + 'current_site' : current_site + } + + # create slice_hrn based on authority_hrn and slice_name + slice_name = slice_request['slice_name'] + req_slice_hrn = authority_hrn + '.' + slice_name + # comparing requested slice_hrn with the existing slice_hrn + slice_query = Query().get('myslice:slice').select('slice_hrn','parent_authority').filter_by('parent_authority','==',authority_hrn) + slice_details_sfa = execute_admin_query(wsgi_request, slice_query) + for _slice in slice_details_sfa: + if _slice['slice_hrn'] == req_slice_hrn: + errors.append('Slice already exists. Please use a different slice name.') + + + # What kind of slice name is valid? + if (slice_name is None or slice_name == ''): + errors.append('Slice name is mandatory') + + if (re.search(r'^[A-Za-z0-9_]*$', slice_name) == None): + errors.append('Slice name may contain only letters, numbers, and underscore.') + + organization = slice_request['organization'] + if (organization is None or organization == ''): + errors.append('Organization is mandatory') + + + + purpose = slice_request['purpose'] + if (purpose is None or purpose == ''): + errors.append('Experiment purpose is mandatory') + + url = slice_request['url'] + + if not errors: + if is_pi(wsgi_request, user_hrn, authority_hrn): + # PIs can directly create slices in their own authority... + create_slice(wsgi_request, slice_request) + clear_user_creds(wsgi_request, user_email) + self.template_name = 'slice-request-done-view.html' + else: + # Otherwise a wsgi_request is sent to the PI + create_pending_slice(wsgi_request, slice_request, user_email) + self.template_name = 'slice-request-ack-view.html' + + # log user activity + activity.user.slice(wsgi_request) + + return render(wsgi_request, self.template, {'theme': self.theme}) # Redirect after POST + else: + slice_request = {} + + template_env = { + 'username': wsgi_request.user.email, + 'topmenu_items': topmenu_items_live('Request a slice', page), + 'errors': errors, + 'slice_name': slice_name, + 'purpose': purpose, + 'email': user_email, + 'user_hrn': user_hrn, + 'url': url, + 'pi': pi, + 'authority_name': authority_name, + 'authority_hrn': user_authority, + 'cc_myself': True, + 'authorities': authorities, + 'theme': self.theme, + 'section': "Slice request" + } + template_env.update(slice_request) + template_env.update(page.prelude_env()) + return render(wsgi_request, self.template, template_env) diff --git a/portal/registrationview.py b/portal/registrationview.py index d5d3b972..47ae6c9b 100644 --- a/portal/registrationview.py +++ b/portal/registrationview.py @@ -45,6 +45,7 @@ class RegistrationView (FreeAccessView, ThemeView): authorities_query = Query.get('authority').select('name', 'authority_hrn') authorities = execute_admin_query(wsgi_request, authorities_query) if authorities is not None: + authorities = sorted(authorities, key=lambda k: k['authority_hrn']) authorities = sorted(authorities, key=lambda k: k['name']) print "############ BREAKPOINT 1 #################" diff --git a/portal/static/css/fed4fire.css b/portal/static/css/fed4fire.css index 49771c2f..cba7715c 100644 --- a/portal/static/css/fed4fire.css +++ b/portal/static/css/fed4fire.css @@ -129,7 +129,7 @@ div#navigation li a { } div#navigation li a:hover { text-decoration:none; - color:#B8B2FF; + color:black; } div#navigation li:last-child { margin-right:0; diff --git a/portal/static/js/myslice.js b/portal/static/js/myslice.js index 1ccc1c1a..aa5df439 100644 --- a/portal/static/js/myslice.js +++ b/portal/static/js/myslice.js @@ -1,6 +1,10 @@ /* * MySlice Class */ +function isFunction(functionToCheck) { + var getType = {}; + return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'; +} function list() { this.elements = []; @@ -114,6 +118,20 @@ var myslice = { } return this.user; }, + projects: {}, + + projects: function() { + if ($.isEmptyObject(this.projects)) { + //this.login(function() { return this.user; }); + if(localStorage.getItem('projects')!='undefined'){ + this.projects = JSON.parse(localStorage.getItem('projects')); + }else{ + return false; + } + } + return this.projects; + }, + loadSlices: function(slices) { if (typeof(slices) == "undefined"){ @@ -154,12 +172,43 @@ var myslice = { if($.isEmptyObject(user)){ // REGISTRY ONLY TO BE REMOVED WITH MANIFOLD-V2 $.post("/rest/myslice:user/",{'filters':{'user_hrn':'$user_hrn'}}, function( data ) { - //myslice.user = new user(data[0]); localStorage.setItem('user', JSON.stringify(data[0])); myslice.loadSlices(data[0].slices); + if(isFunction(fn)){ + fn(); + } }); + }else{ + if(isFunction(fn)){ + fn(); + } + } + + }, + loadProjects: function(fn) { + user = JSON.parse(localStorage.getItem('user')); + projects = localStorage.getItem('projects'); + if($.isEmptyObject(projects)){ + if($.isEmptyObject(user) || $.isEmptyObject(user.parent_authority)){ + $.post("/rest/myslice:user/",{'filters':{'user_hrn':'$user_hrn'},'fields':['parent_authority']}, function( data ) { + parent_authority = data[0].parent_authority; + + }); + }else{ + parent_authority = user.parent_authority; + } + // REGISTRY ONLY TO BE REMOVED WITH MANIFOLD-V2 + $.post("/rest/myslice:authority/",{'fields':['authority_hrn'],'filters':{'authority_hrn':'CONTAINS'+parent_authority}}, function( data ) { + localStorage.setItem('projects', JSON.stringify(data)); + }); + }else{ + if(isFunction(fn)){ + fn(); + } } + }, + getSlices: function(name) { }, diff --git a/portal/templates/base.html b/portal/templates/base.html index 0bf9ab4e..decc1b38 100644 --- a/portal/templates/base.html +++ b/portal/templates/base.html @@ -48,48 +48,50 @@ XXX TODO: session This list of slices should go into SESSION ! */ $(document).ready(function() { - {% if username %} - myslice.login(); +{% if username %} + myslice.login(function(){ + user = myslice.user(); - user = myslice.user(); + var slices = []; + if($.isEmptyObject(user)){ + $("div#home-slice-list").html( + "
You do not yet have a slice
"); + $("ul#dropdown-slice-list").append("
  • no slice
  • "); + slices.push("no slice"); + }else{ + slices = user.slices; + drawSlices(slices); + } + {% if theme == "fed4fire" %} + myslice.loadProjects(function(){ + p = myslice.projects(); + drawProjects(p); + }); + {% endif %} + }); - var slices = []; - if($.isEmptyObject(user)){ - $.post("/rest/myslice:user/",{'filters':{'user_hrn':'$user_hrn'}}, function( data ) { - if(data.length > 0){ - drawSlices(data[0].slices); - }else{ - $("div#home-slice-list").html( - "
    You do not yet have a slice
    "); - $("ul#dropdown-slice-list").append("
  • no slice
  • "); - slices.push("no slice"); - } - }); - }else{ - slices = user.slices; - drawSlices(slices); - } - - /* - Launch queries to get the resources and leases in Manifold Cache - */ - function drawSlices(slices){ var items = []; $.each( slices, function(i, val) { items.push( "
  • " + val + "
  • " ); - /* - // Launch a Query for each slice to get resources and leases in Manifold Cache - // Now only done in Homeview to speed up the other pages - $.post("/rest/slice/", { 'filters': { 'slice_hrn' : val } }, function(data) { - }); - */ }); $("div#home-slice-list").html($( "