1 from __future__ import print_function
3 from django.shortcuts import render
4 from django.contrib.sites.models import Site
6 from manifold.core.query import Query
7 from manifoldapi.manifoldapi import execute_admin_query, execute_query
9 from unfold.loginrequired import LoginRequiredAutoLogoutView
11 from portal.actions import create_pending_project, create_pending_join, sfa_add_authority, authority_add_pis, is_pi
12 from portal.models import PendingProject, PendingJoin
14 from myslice.theme import ThemeView
18 class ProjectRequestView(LoginRequiredAutoLogoutView, ThemeView):
19 template_name = 'projectrequest_view.html'
21 def getAuthorities(self, request):
22 authorities_query = Query.get('authority').select('name', 'authority_hrn')
23 authorities = execute_admin_query(request, authorities_query)
24 if authorities is not None:
25 authorities = sorted(authorities, key=lambda k: k['authority_hrn'])
26 authorities = sorted(authorities, key=lambda k: k['name'])
29 def getUserAuthority(self, request):
30 # Get user_email (XXX Would deserve to be simplified)
31 user_query = Query().get('local:user').select('email','config')
32 user_details = execute_query(request, user_query)
33 for user_detail in user_details:
34 user_config = json.loads(user_detail['config'])
35 user_authority = user_config.get('authority','N/A')
38 def getUserHrn(self, request):
41 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
42 account_details = execute_query(request, account_query)
44 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
45 platform_details = execute_query(request, platform_query)
47 # getting user_hrn from local:account
48 for account_detail in account_details:
49 for platform_detail in platform_details:
50 if platform_detail['platform_id'] == account_detail['platform_id']:
51 # taking user_hrn only from myslice account
52 # NOTE: we should later handle accounts filter_by auth_type= managed OR user
53 if 'myslice' in platform_detail['platform']:
54 account_config = json.loads(account_detail['config'])
55 user_hrn = account_config.get('user_hrn','N/A')
58 def getUserEmail(self, request):
59 # Get user_email (XXX Would deserve to be simplified)
60 user_query = Query().get('local:user').select('email','config')
61 user_details = execute_query(request, user_query)
62 user_email = user_details[0].get('email')
65 def post(self, request):
66 return self.handle_request(request, 'POST')
68 def get(self, request):
69 return self.handle_request(request, 'GET')
71 def handle_request(self, wsgi_request, method):
76 #errors.append(wsgi_request.POST)
78 user_hrn = self.getUserHrn(wsgi_request)
80 user_email = self.getUserEmail(wsgi_request)
82 authorities = self.getAuthorities(wsgi_request)
84 user_authority = self.getUserAuthority(wsgi_request)
86 # getting the org from authority
87 for authority in authorities:
88 if authority['authority_hrn'] == user_authority:
89 authority_name = authority['name']
93 if 'join' in wsgi_request.POST:
95 'user_hrn' : user_hrn,
97 'project_name' : wsgi_request.POST.get('project_name', ''),
98 'authority_hrn' : wsgi_request.POST.get('project_name', ''),
103 'user_hrn' : user_hrn,
104 'email' : user_email,
105 'authority_hrn' : wsgi_request.POST.get('authority_name', ''),
106 'project_name' : wsgi_request.POST.get('project_name', ''),
107 'purpose' : wsgi_request.POST.get('purpose', ''),
110 if (post['authority_hrn'] is None or post['authority_hrn'] == ''):
111 errors.append('Organization is mandatory')
113 if (post['purpose'] is None or post['purpose'] == ''):
114 errors.append('Project purpose is mandatory')
116 if (re.search(r'^[A-Za-z0-9_]*$', post['project_name']) == None):
117 errors.append('Project name may contain only letters, numbers, and underscore.')
119 # What kind of project name is valid?
120 if (post['project_name'] is None or post['project_name'] == ''):
121 errors.append('Project name is mandatory')
124 print("is_pi on auth_hrn = ", user_authority)
125 if is_pi(wsgi_request, user_hrn, user_authority):
126 # PIs can directly create/join project in their own authority...
127 if 'join' in wsgi_request.POST:
128 authority_add_pis(wsgi_request, post['project_name'], user_hrn)
130 hrn = post['authority_hrn'] + '.' + post['project_name']
131 sfa_add_authority(wsgi_request, {'authority_hrn':hrn})
132 authority_add_pis(wsgi_request, hrn, user_hrn)
133 self.template_name = 'project-request-done-view.html'
135 # Otherwise a wsgi_request is sent to the PI
136 if 'join' in wsgi_request.POST:
137 create_pending_join(wsgi_request, post)
139 create_pending_project(wsgi_request, post)
140 self.template_name = 'project-request-ack-view.html'
142 # retrieves the pending projects creation list
143 pending_projects = PendingProject.objects.all().filter(user_hrn=user_hrn)
144 # retrieves the pending join a project list
145 pending_join_projects = PendingJoin.objects.all().filter(user_hrn=user_hrn)
147 root_authority = user_authority.split('.', 1)[0]
150 'username': wsgi_request.user,
152 'authorities': authorities,
153 'authority_hrn': user_authority,
154 'root_authority_hrn': root_authority,
155 'pending_projects': pending_projects,
156 'pending_join_projects': pending_join_projects,
158 return render(wsgi_request, self.template, env)
169 authority_name = None
170 # Retrieve the list of authorities
171 authorities_query = Query.get('authority').select('name', 'authority_hrn')
172 authorities = execute_admin_query(wsgi_request, authorities_query)
173 if authorities is not None:
174 authorities = sorted(authorities, key=lambda k: k['authority_hrn'])
175 authorities = sorted(authorities, key=lambda k: k['name'])
177 # Get user_email (XXX Would deserve to be simplified)
178 user_query = Query().get('local:user').select('email','config')
179 user_details = execute_query(wsgi_request, user_query)
180 user_email = user_details[0].get('email')
182 for user_detail in user_details:
183 user_config = json.loads(user_detail['config'])
184 user_authority = user_config.get('authority','N/A')
185 # getting the org from authority
186 for authority in authorities:
187 if authority['authority_hrn'] == user_authority:
188 authority_name = authority['name']
190 # Handle the case when we use only hrn and not name
191 if authority_name is None:
192 authority_name = user_authority
194 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
195 account_details = execute_query(wsgi_request, account_query)
197 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
198 platform_details = execute_query(wsgi_request, platform_query)
201 # getting user_hrn from local:account
202 for account_detail in account_details:
203 for platform_detail in platform_details:
204 if platform_detail['platform_id'] == account_detail['platform_id']:
205 # taking user_hrn only from myslice account
206 # NOTE: we should later handle accounts filter_by auth_type= managed OR user
207 if 'myslice' in platform_detail['platform']:
208 account_config = json.loads(account_detail['config'])
209 user_hrn = account_config.get('user_hrn','N/A')
210 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
213 # checking if pi or not
214 if acc_auth_cred == {} or acc_auth_cred == 'N/A':
221 # page = Page(wsgi_request)
222 # page.add_js_files ( [ "js/jquery.validate.js", "js/jquery-ui.js" ] )
223 # page.add_css_files ( [ "https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" ] )
224 # page.expose_js_metadata()
227 # The form has been submitted
230 # current_site = Site.objects.get_current()
231 # current_site = current_site.domain
233 # getting the authority_hrn from the selected organization
234 for authority in authorities:
235 if authority['name'] == wsgi_request.POST.get('org_name', ''):
236 authority_hrn = authority['authority_hrn']
238 # Handle the case when we use only hrn and not name
239 if authority_hrn is None:
240 authority_hrn = wsgi_request.POST.get('org_name', '')
245 'user_hrn' : user_hrn,
246 'email' : user_email,
247 'timestamp' : time.time(),
248 'authority_hrn' : authority_hrn,
249 'organization' : wsgi_request.POST.get('org_name', ''),
250 'slice_name' : wsgi_request.POST.get('slice_name', ''),
251 'url' : wsgi_request.POST.get('url', ''),
252 'purpose' : wsgi_request.POST.get('purpose', ''),
253 'current_site' : current_site
256 # create slice_hrn based on authority_hrn and slice_name
257 # slice_name = slice_request['slice_name']
258 req_slice_hrn = authority_hrn + '.' + slice_name
259 # comparing requested slice_hrn with the existing slice_hrn
260 slice_query = Query().get('myslice:slice').select('slice_hrn','parent_authority').filter_by('parent_authority','==',authority_hrn)
261 slice_details_sfa = execute_admin_query(wsgi_request, slice_query)
262 for _slice in slice_details_sfa:
263 if _slice['slice_hrn'] == req_slice_hrn:
264 errors.append('Slice already exists. Please use a different slice name.')
267 # What kind of slice name is valid?
268 if (slice_name is None or slice_name == ''):
269 errors.append('Slice name is mandatory')
271 if (re.search(r'^[A-Za-z0-9_]*$', slice_name) == None):
272 errors.append('Slice name may contain only letters, numbers, and underscore.')
274 organization = slice_request['organization']
275 if (organization is None or organization == ''):
276 errors.append('Organization is mandatory')
280 purpose = slice_request['purpose']
281 if (purpose is None or purpose == ''):
282 errors.append('Experiment purpose is mandatory')
284 url = slice_request['url']
287 if is_pi(wsgi_request, user_hrn, authority_hrn):
288 # PIs can directly create slices in their own authority...
289 create_slice(wsgi_request, slice_request)
290 clear_user_creds(wsgi_request, user_email)
291 self.template_name = 'slice-request-done-view.html'
293 # Otherwise a wsgi_request is sent to the PI
294 create_pending_slice(wsgi_request, slice_request, user_email)
295 self.template_name = 'slice-request-ack-view.html'
298 activity.user.slice(wsgi_request)
300 return render(wsgi_request, self.template, {'theme': self.theme}) # Redirect after POST
305 'username': wsgi_request.user.email,
307 'slice_name': slice_name,
310 'user_hrn': user_hrn,
313 'authority_name': authority_name,
314 'authority_hrn': user_authority,
316 'authorities': authorities,
318 'section': "Slice request"
320 template_env.update(slice_request)
321 template_env.update(page.prelude_env())
322 return render(wsgi_request, self.template, template_env)