+ def getAuthorities(self, request):
+ authorities_query = Query.get('authority').select('name', 'authority_hrn')
+ authorities = execute_admin_query(request, authorities_query)
+ if authorities is not None:
+ # Remove the root authority from the list
+ matching = [s for s in authorities if "." in s['authority_hrn']]
+ authorities = sorted(matching, key=lambda k: k['authority_hrn'])
+ authorities = sorted(matching, key=lambda k: k['name'])
+ return authorities
+
+ def getUserAuthority(self, request):
+ # Get user_email (XXX Would deserve to be simplified)
+ user_query = Query().get('local:user').select('email','config')
+ user_details = execute_query(request, user_query)
+ for user_detail in user_details:
+ user_config = json.loads(user_detail['config'])
+ user_authority = user_config.get('authority','N/A')
+ return user_authority
+
+ def getUserHrn(self, request):
+ user_hrn = None
+
+ account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
+ account_details = execute_query(request, account_query)
+
+ platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
+ platform_details = execute_query(request, platform_query)
+
+ # 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')
+ return user_hrn
+
+ def getUserEmail(self, request):
+ # Get user_email (XXX Would deserve to be simplified)
+ user_query = Query().get('local:user').select('email','config')
+ user_details = execute_query(request, user_query)
+ user_email = user_details[0].get('email')
+ return user_email
+
+ def post(self, request):
+ return self.handle_request(request, 'POST')
+
+ def get(self, request):
+ return self.handle_request(request, 'GET')
+
+ def handle_request(self, wsgi_request, method):
+ errors = []
+ authority_hrn = None
+ authority_name = None
+
+ #errors.append(wsgi_request.POST)
+
+ user_hrn = self.getUserHrn(wsgi_request)
+
+ user_email = self.getUserEmail(wsgi_request)
+
+ authorities = self.getAuthorities(wsgi_request)
+
+ user_authority = self.getUserAuthority(wsgi_request)
+
+ # getting the org from authority
+ for authority in authorities:
+ if authority['authority_hrn'] == user_authority:
+ authority_name = authority['name']
+
+ if method == 'POST' :
+
+ if 'join' in wsgi_request.POST:
+ post = {
+ 'user_hrn' : user_hrn,
+ 'email' : user_email,
+ 'project_name' : wsgi_request.POST.get('project_name', ''),
+ 'authority_hrn' : wsgi_request.POST.get('project_name', ''),
+ }
+
+ else:
+ post = {
+ 'user_hrn' : user_hrn,
+ 'email' : user_email,
+ 'authority_hrn' : wsgi_request.POST.get('authority_name', ''),
+ 'project_name' : wsgi_request.POST.get('project_name', ''),
+ 'purpose' : wsgi_request.POST.get('purpose', ''),
+ }
+
+ # for new projects max project_name length is 10
+ if (len(post['project_name']) >10):
+ errors.append('Project name can be maximum 10 characters long')
+
+ #if (post['authority_hrn'] is None or post['authority_hrn'] == ''):
+ # errors.append('Organization is mandatory')
+
+ if (post['purpose'] is None or post['purpose'] == ''):
+ errors.append('Project purpose is mandatory')