5 from django.shortcuts import render
6 from django.contrib.sites.models import Site
8 from manifold.core.query import Query
9 from manifoldapi.manifoldapi import execute_admin_query, execute_query
11 from unfold.loginrequired import LoginRequiredAutoLogoutView
13 from portal.actions import create_pending_project, create_pending_join, sfa_add_authority, authority_add_pis, is_pi
14 from portal.models import PendingProject, PendingJoin
16 from myslice.theme import ThemeView
17 from myslice.settings import logger
19 class ProjectRequestView(LoginRequiredAutoLogoutView, ThemeView):
20 template_name = 'projectrequest_view.html'
22 def getAuthorities(self, request):
23 if theme.theme == 'fed4fire':
24 authorities_query = Query.get('myslice:authority').select('name', 'authority_hrn')
26 authorities_query = Query.get('authority').select('name', 'authority_hrn')
27 authorities = execute_admin_query(request, authorities_query)
28 if authorities is not None:
29 # Remove the root authority from the list
30 matching = [s for s in authorities if "." in s['authority_hrn']]
31 authorities = sorted(matching, key=lambda k: k['authority_hrn'])
32 authorities = sorted(matching, key=lambda k: k['name'])
35 def getUserAuthority(self, request):
36 # Get user_email (XXX Would deserve to be simplified)
37 user_query = Query().get('local:user').select('email','config')
38 user_details = execute_query(request, user_query)
39 for user_detail in user_details:
40 user_config = json.loads(user_detail['config'])
41 user_authority = user_config.get('authority','N/A')
44 def getUserHrn(self, request):
47 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
48 account_details = execute_query(request, account_query)
50 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
51 platform_details = execute_query(request, platform_query)
53 # getting user_hrn from local:account
54 for account_detail in account_details:
55 for platform_detail in platform_details:
56 if platform_detail['platform_id'] == account_detail['platform_id']:
57 # taking user_hrn only from myslice account
58 # NOTE: we should later handle accounts filter_by auth_type= managed OR user
59 if 'myslice' in platform_detail['platform']:
60 account_config = json.loads(account_detail['config'])
61 user_hrn = account_config.get('user_hrn','N/A')
64 def getUserEmail(self, request):
65 # Get user_email (XXX Would deserve to be simplified)
66 user_query = Query().get('local:user').select('email','config')
67 user_details = execute_query(request, user_query)
68 user_email = user_details[0].get('email')
71 def post(self, request):
72 return self.handle_request(request, 'POST')
74 def get(self, request):
75 return self.handle_request(request, 'GET')
77 def handle_request(self, wsgi_request, method):
82 #errors.append(wsgi_request.POST)
84 user_hrn = self.getUserHrn(wsgi_request)
86 user_email = self.getUserEmail(wsgi_request)
88 authorities = self.getAuthorities(wsgi_request)
90 user_authority = self.getUserAuthority(wsgi_request)
92 # getting the org from authority
93 for authority in authorities:
94 if authority['authority_hrn'] == user_authority:
95 authority_name = authority['name']
99 project_name = wsgi_request.POST.get('project_name', '')
100 if not project_name or len(project_name) == 0 :
101 errors.append('Project name can\'t be empty')
103 # accept only lowercase names
104 project_name = project_name.lower()
106 if 'join' in wsgi_request.POST:
108 'user_hrn' : user_hrn,
109 'email' : user_email,
110 'project_name' : project_name,
111 'authority_hrn' : project_name,
116 'user_hrn' : user_hrn,
117 'email' : user_email,
118 'authority_hrn' : wsgi_request.POST.get('authority_name', ''),
119 'project_name' : project_name,
120 'purpose' : wsgi_request.POST.get('purpose', ''),
123 # for new projects max project_name length is 10
124 if (len(post['project_name']) >10):
125 errors.append('Project name can be maximum 10 characters long')
127 #if (post['authority_hrn'] is None or post['authority_hrn'] == ''):
128 # errors.append('Organization is mandatory')
130 if post['purpose'] is None or post['purpose'] == '':
131 errors.append('Project purpose is mandatory')
133 if re.search(r'^[A-Za-z0-9_]*$', post['project_name']) is None:
134 errors.append('Project name may contain only letters, numbers, and underscore.')
136 # What kind of project name is valid?
137 if post['project_name'] is None or post['project_name'] == '':
138 errors.append('Project name is mandatory')
141 logger.info("is_pi on auth_hrn = {}".format(user_authority))
142 if is_pi(wsgi_request, user_hrn, user_authority):
143 # PIs can directly create/join project in their own authority...
144 if 'join' in wsgi_request.POST:
145 # join existing project
146 authority_add_pis(wsgi_request, post['project_name'], user_hrn)
149 hrn = post['authority_hrn'] + '.' + post['project_name']
150 sfa_add_authority(wsgi_request, {'authority_hrn':hrn})
151 authority_add_pis(wsgi_request, hrn, user_hrn)
152 self.template_name = 'project-request-done-view.html'
154 # Otherwise a wsgi_request is sent to the PI
155 if 'join' in wsgi_request.POST:
156 create_pending_join(wsgi_request, post)
158 create_pending_project(wsgi_request, post)
159 self.template_name = 'project-request-ack-view.html'
161 # retrieves the pending projects creation list
162 pending_projects = PendingProject.objects.all().filter(user_hrn=user_hrn)
163 # retrieves the pending join a project list
164 pending_join_projects = PendingJoin.objects.all().filter(user_hrn=user_hrn)
166 root_authority = user_authority.split('.', 1)[0]
169 'username': wsgi_request.user,
171 'authorities': authorities,
172 'authority_hrn': user_authority,
173 'root_authority_hrn': root_authority,
174 'pending_projects': pending_projects,
175 'pending_join_projects': pending_join_projects,
177 return render(wsgi_request, self.template, env)