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
11 from portal.models import PendingProject
13 from myslice.theme import ThemeView
17 class ProjectRequestView(LoginRequiredAutoLogoutView, ThemeView):
18 template_name = 'projectrequest_view.html'
20 def getAuthorities(self, request):
21 authorities_query = Query.get('authority').select('name', 'authority_hrn')
22 authorities = execute_admin_query(request, authorities_query)
23 if authorities is not None:
24 authorities = sorted(authorities, key=lambda k: k['authority_hrn'])
25 authorities = sorted(authorities, key=lambda k: k['name'])
28 def getUserAuthority(self, request):
29 # Get user_email (XXX Would deserve to be simplified)
30 user_query = Query().get('local:user').select('email','config')
31 user_details = execute_query(request, user_query)
32 for user_detail in user_details:
33 user_config = json.loads(user_detail['config'])
34 user_authority = user_config.get('authority','N/A')
37 def getUserHrn(self, request):
40 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
41 account_details = execute_query(request, account_query)
43 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
44 platform_details = execute_query(request, platform_query)
46 # getting user_hrn from local:account
47 for account_detail in account_details:
48 for platform_detail in platform_details:
49 if platform_detail['platform_id'] == account_detail['platform_id']:
50 # taking user_hrn only from myslice account
51 # NOTE: we should later handle accounts filter_by auth_type= managed OR user
52 if 'myslice' in platform_detail['platform']:
53 account_config = json.loads(account_detail['config'])
54 user_hrn = account_config.get('user_hrn','N/A')
57 def post(self, request):
58 return self.handle_request(request, 'POST')
60 def get(self, request):
61 return self.handle_request(request, 'GET')
63 def handle_request(self, wsgi_request, method):
68 user_hrn = self.getUserHrn(wsgi_request)
70 authorities = self.getAuthorities(wsgi_request)
72 user_authority = self.getUserAuthority(wsgi_request)
74 # getting the org from authority
75 for authority in authorities:
76 if authority['authority_hrn'] == user_authority:
77 authority_name = authority['name']
82 'user_hrn' : user_hrn,
83 'authority_hrn' : wsgi_request.POST.get('authority_name', ''),
84 'project_name' : wsgi_request.POST.get('project_name', ''),
85 'purpose' : wsgi_request.POST.get('purpose', ''),
88 # # create slice_hrn based on authority_hrn and slice_name
89 # # slice_name = slice_request['slice_name']
90 # req_slice_hrn = authority_hrn + '.' + slice_name
91 # # comparing requested slice_hrn with the existing slice_hrn
92 # slice_query = Query().get('myslice:slice').select('slice_hrn','parent_authority').filter_by('parent_authority','==',authority_hrn)
93 # slice_details_sfa = execute_admin_query(wsgi_request, slice_query)
94 # for _slice in slice_details_sfa:
95 # if _slice['slice_hrn'] == req_slice_hrn:
96 # errors.append('Slice already exists. Please use a different slice name.')
99 # What kind of slice name is valid?
100 if (post['project_name'] is None or post['project_name'] == ''):
101 errors.append('Project name is mandatory')
103 if (re.search(r'^[A-Za-z0-9_]*$', post['project_name']) == None):
104 errors.append('Project name may contain only letters, numbers, and underscore.')
106 if (post['authority_hrn'] is None or post['authority_hrn'] == ''):
107 errors.append('Organization is mandatory')
109 if (post['purpose'] is None or post['purpose'] == ''):
110 errors.append('Experiment purpose is mandatory')
113 create_pending_project(wsgi_request, post)
115 # retrieves the pending projects list
116 pending_projects = PendingProject.objects.all().filter(user_hrn=user_hrn)
120 'username': wsgi_request.user,
122 'authorities': authorities,
123 'authority_hrn': user_authority,
124 'pending_projects': pending_projects,
126 return render(wsgi_request, self.template, env)
137 authority_name = None
138 # Retrieve the list of authorities
139 authorities_query = Query.get('authority').select('name', 'authority_hrn')
140 authorities = execute_admin_query(wsgi_request, authorities_query)
141 if authorities is not None:
142 authorities = sorted(authorities, key=lambda k: k['authority_hrn'])
143 authorities = sorted(authorities, key=lambda k: k['name'])
145 # Get user_email (XXX Would deserve to be simplified)
146 user_query = Query().get('local:user').select('email','config')
147 user_details = execute_query(wsgi_request, user_query)
148 user_email = user_details[0].get('email')
150 for user_detail in user_details:
151 user_config = json.loads(user_detail['config'])
152 user_authority = user_config.get('authority','N/A')
153 # getting the org from authority
154 for authority in authorities:
155 if authority['authority_hrn'] == user_authority:
156 authority_name = authority['name']
158 # Handle the case when we use only hrn and not name
159 if authority_name is None:
160 authority_name = user_authority
162 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
163 account_details = execute_query(wsgi_request, account_query)
165 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
166 platform_details = execute_query(wsgi_request, platform_query)
169 # getting user_hrn from local:account
170 for account_detail in account_details:
171 for platform_detail in platform_details:
172 if platform_detail['platform_id'] == account_detail['platform_id']:
173 # taking user_hrn only from myslice account
174 # NOTE: we should later handle accounts filter_by auth_type= managed OR user
175 if 'myslice' in platform_detail['platform']:
176 account_config = json.loads(account_detail['config'])
177 user_hrn = account_config.get('user_hrn','N/A')
178 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
181 # checking if pi or not
182 if acc_auth_cred == {} or acc_auth_cred == 'N/A':
189 # page = Page(wsgi_request)
190 # page.add_js_files ( [ "js/jquery.validate.js", "js/jquery-ui.js" ] )
191 # page.add_css_files ( [ "https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" ] )
192 # page.expose_js_metadata()
195 # The form has been submitted
198 # current_site = Site.objects.get_current()
199 # current_site = current_site.domain
201 # getting the authority_hrn from the selected organization
202 for authority in authorities:
203 if authority['name'] == wsgi_request.POST.get('org_name', ''):
204 authority_hrn = authority['authority_hrn']
206 # Handle the case when we use only hrn and not name
207 if authority_hrn is None:
208 authority_hrn = wsgi_request.POST.get('org_name', '')
213 'user_hrn' : user_hrn,
214 'email' : user_email,
215 'timestamp' : time.time(),
216 'authority_hrn' : authority_hrn,
217 'organization' : wsgi_request.POST.get('org_name', ''),
218 'slice_name' : wsgi_request.POST.get('slice_name', ''),
219 'url' : wsgi_request.POST.get('url', ''),
220 'purpose' : wsgi_request.POST.get('purpose', ''),
221 'current_site' : current_site
224 # create slice_hrn based on authority_hrn and slice_name
225 # slice_name = slice_request['slice_name']
226 req_slice_hrn = authority_hrn + '.' + slice_name
227 # comparing requested slice_hrn with the existing slice_hrn
228 slice_query = Query().get('myslice:slice').select('slice_hrn','parent_authority').filter_by('parent_authority','==',authority_hrn)
229 slice_details_sfa = execute_admin_query(wsgi_request, slice_query)
230 for _slice in slice_details_sfa:
231 if _slice['slice_hrn'] == req_slice_hrn:
232 errors.append('Slice already exists. Please use a different slice name.')
235 # What kind of slice name is valid?
236 if (slice_name is None or slice_name == ''):
237 errors.append('Slice name is mandatory')
239 if (re.search(r'^[A-Za-z0-9_]*$', slice_name) == None):
240 errors.append('Slice name may contain only letters, numbers, and underscore.')
242 organization = slice_request['organization']
243 if (organization is None or organization == ''):
244 errors.append('Organization is mandatory')
248 purpose = slice_request['purpose']
249 if (purpose is None or purpose == ''):
250 errors.append('Experiment purpose is mandatory')
252 url = slice_request['url']
255 if is_pi(wsgi_request, user_hrn, authority_hrn):
256 # PIs can directly create slices in their own authority...
257 create_slice(wsgi_request, slice_request)
258 clear_user_creds(wsgi_request, user_email)
259 self.template_name = 'slice-request-done-view.html'
261 # Otherwise a wsgi_request is sent to the PI
262 create_pending_slice(wsgi_request, slice_request, user_email)
263 self.template_name = 'slice-request-ack-view.html'
266 activity.user.slice(wsgi_request)
268 return render(wsgi_request, self.template, {'theme': self.theme}) # Redirect after POST
273 'username': wsgi_request.user.email,
275 'slice_name': slice_name,
278 'user_hrn': user_hrn,
281 'authority_name': authority_name,
282 'authority_hrn': user_authority,
284 'authorities': authorities,
286 'section': "Slice request"
288 template_env.update(slice_request)
289 template_env.update(page.prelude_env())
290 return render(wsgi_request, self.template, template_env)