1 from django.shortcuts import render
2 from django.contrib.sites.models import Site
4 from manifold.core.query import Query
5 from manifoldapi.manifoldapi import execute_admin_query, execute_query
7 from unfold.loginrequired import LoginRequiredAutoLogoutView
9 from portal.actions import create_pending_project, create_pending_join, sfa_add_authority, authority_add_pis, is_pi
10 from portal.models import PendingProject, PendingJoin
12 from myslice.theme import ThemeView
16 class ProjectRequestView(LoginRequiredAutoLogoutView, ThemeView):
17 template_name = 'projectrequest_view.html'
19 def getAuthorities(self, request):
20 authorities_query = Query.get('authority').select('name', 'authority_hrn')
21 authorities = execute_admin_query(request, authorities_query)
22 if authorities is not None:
23 # Remove the root authority from the list
24 matching = [s for s in authorities if "." in s['authority_hrn']]
25 authorities = sorted(matching, key=lambda k: k['authority_hrn'])
26 authorities = sorted(matching, 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 # for new projects max project_name length is 10
111 if (len(post['project_name']) >10):
112 errors.append('Project name can be maximum 10 characters long')
114 #if (post['authority_hrn'] is None or post['authority_hrn'] == ''):
115 # errors.append('Organization is mandatory')
117 if (post['purpose'] is None or post['purpose'] == ''):
118 errors.append('Project purpose is mandatory')
120 if (re.search(r'^[A-Za-z0-9_]*$', post['project_name']) == None):
121 errors.append('Project name may contain only letters, numbers, and underscore.')
123 # What kind of project name is valid?
124 if (post['project_name'] is None or post['project_name'] == ''):
125 errors.append('Project name is mandatory')
128 print "is_pi on auth_hrn = ", user_authority
129 if is_pi(wsgi_request, user_hrn, user_authority):
130 # PIs can directly create/join project in their own authority...
131 if 'join' in wsgi_request.POST:
132 authority_add_pis(wsgi_request, post['project_name'], user_hrn)
134 hrn = post['authority_hrn'] + '.' + post['project_name']
135 sfa_add_authority(wsgi_request, {'authority_hrn':hrn})
136 authority_add_pis(wsgi_request, hrn, user_hrn)
137 self.template_name = 'project-request-done-view.html'
139 # Otherwise a wsgi_request is sent to the PI
140 if 'join' in wsgi_request.POST:
141 create_pending_join(wsgi_request, post)
143 create_pending_project(wsgi_request, post)
144 self.template_name = 'project-request-ack-view.html'
146 # retrieves the pending projects creation list
147 pending_projects = PendingProject.objects.all().filter(user_hrn=user_hrn)
148 # retrieves the pending join a project list
149 pending_join_projects = PendingJoin.objects.all().filter(user_hrn=user_hrn)
151 root_authority = user_authority.split('.', 1)[0]
154 'username': wsgi_request.user,
156 'authorities': authorities,
157 'authority_hrn': user_authority,
158 'root_authority_hrn': root_authority,
159 'pending_projects': pending_projects,
160 'pending_join_projects': pending_join_projects,
162 return render(wsgi_request, self.template, env)
173 authority_name = None
174 # Retrieve the list of authorities
175 authorities_query = Query.get('authority').select('name', 'authority_hrn')
176 authorities = execute_admin_query(wsgi_request, authorities_query)
177 if authorities is not None:
178 authorities = sorted(authorities, key=lambda k: k['authority_hrn'])
179 authorities = sorted(authorities, key=lambda k: k['name'])
181 # Get user_email (XXX Would deserve to be simplified)
182 user_query = Query().get('local:user').select('email','config')
183 user_details = execute_query(wsgi_request, user_query)
184 user_email = user_details[0].get('email')
186 for user_detail in user_details:
187 user_config = json.loads(user_detail['config'])
188 user_authority = user_config.get('authority','N/A')
189 # getting the org from authority
190 for authority in authorities:
191 if authority['authority_hrn'] == user_authority:
192 authority_name = authority['name']
194 # Handle the case when we use only hrn and not name
195 if authority_name is None:
196 authority_name = user_authority
198 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
199 account_details = execute_query(wsgi_request, account_query)
201 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
202 platform_details = execute_query(wsgi_request, platform_query)
205 # getting user_hrn from local:account
206 for account_detail in account_details:
207 for platform_detail in platform_details:
208 if platform_detail['platform_id'] == account_detail['platform_id']:
209 # taking user_hrn only from myslice account
210 # NOTE: we should later handle accounts filter_by auth_type= managed OR user
211 if 'myslice' in platform_detail['platform']:
212 account_config = json.loads(account_detail['config'])
213 user_hrn = account_config.get('user_hrn','N/A')
214 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
217 # checking if pi or not
218 if acc_auth_cred == {} or acc_auth_cred == 'N/A':
225 # page = Page(wsgi_request)
226 # page.add_js_files ( [ "js/jquery.validate.js", "js/jquery-ui.js" ] )
227 # page.add_css_files ( [ "https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" ] )
228 # page.expose_js_metadata()
231 # The form has been submitted
234 # current_site = Site.objects.get_current()
235 # current_site = current_site.domain
237 # getting the authority_hrn from the selected organization
238 for authority in authorities:
239 if authority['name'] == wsgi_request.POST.get('org_name', ''):
240 authority_hrn = authority['authority_hrn']
242 # Handle the case when we use only hrn and not name
243 if authority_hrn is None:
244 authority_hrn = wsgi_request.POST.get('org_name', '')
249 'user_hrn' : user_hrn,
250 'email' : user_email,
251 'timestamp' : time.time(),
252 'authority_hrn' : authority_hrn,
253 'organization' : wsgi_request.POST.get('org_name', ''),
254 'slice_name' : wsgi_request.POST.get('slice_name', ''),
255 'url' : wsgi_request.POST.get('url', ''),
256 'purpose' : wsgi_request.POST.get('purpose', ''),
257 'current_site' : current_site
260 # create slice_hrn based on authority_hrn and slice_name
261 # slice_name = slice_request['slice_name']
262 req_slice_hrn = authority_hrn + '.' + slice_name
263 # comparing requested slice_hrn with the existing slice_hrn
264 slice_query = Query().get('myslice:slice').select('slice_hrn','parent_authority').filter_by('parent_authority','==',authority_hrn)
265 slice_details_sfa = execute_admin_query(wsgi_request, slice_query)
266 for _slice in slice_details_sfa:
267 if _slice['slice_hrn'] == req_slice_hrn:
268 errors.append('Slice already exists. Please use a different slice name.')
271 # What kind of slice name is valid?
272 if (slice_name is None or slice_name == ''):
273 errors.append('Slice name is mandatory')
275 if (re.search(r'^[A-Za-z0-9_]*$', slice_name) == None):
276 errors.append('Slice name may contain only letters, numbers, and underscore.')
278 organization = slice_request['organization']
279 if (organization is None or organization == ''):
280 errors.append('Organization is mandatory')
284 purpose = slice_request['purpose']
285 if (purpose is None or purpose == ''):
286 errors.append('Experiment purpose is mandatory')
288 url = slice_request['url']
291 if is_pi(wsgi_request, user_hrn, authority_hrn):
292 # PIs can directly create slices in their own authority...
293 create_slice(wsgi_request, slice_request)
294 clear_user_creds(wsgi_request, user_email)
295 self.template_name = 'slice-request-done-view.html'
297 # Otherwise a wsgi_request is sent to the PI
298 create_pending_slice(wsgi_request, slice_request, user_email)
299 self.template_name = 'slice-request-ack-view.html'
302 activity.user.slice(wsgi_request)
304 return render(wsgi_request, self.template, {'theme': self.theme}) # Redirect after POST
309 'username': wsgi_request.user.email,
311 'slice_name': slice_name,
314 'user_hrn': user_hrn,
317 'authority_name': authority_name,
318 'authority_hrn': user_authority,
320 'authorities': authorities,
322 'section': "Slice request"
324 template_env.update(slice_request)
325 template_env.update(page.prelude_env())
326 return render(wsgi_request, self.template, template_env)