1 from django.shortcuts import render
2 from django.contrib.sites.models import Site
5 from unfold.page import Page
7 from manifold.core.query import Query
8 from manifoldapi.manifoldapi import execute_admin_query, execute_query
10 from portal.actions import is_pi, create_slice, create_pending_slice, clear_user_creds
11 #from portal.forms import SliceRequestForm
12 from unfold.loginrequired import LoginRequiredAutoLogoutView
13 from ui.topmenu import topmenu_items_live, the_user
15 from myslice.theme import ThemeView
21 class SliceRequestView (LoginRequiredAutoLogoutView, ThemeView):
22 template_name = 'slicerequest_view.html'
24 # because we inherit LoginRequiredAutoLogoutView that is implemented by redefining 'dispatch'
25 # we cannot redefine dispatch here, or we'd lose LoginRequired and AutoLogout behaviours
26 def post (self, request):
27 return self.get_or_post (request, 'POST')
29 def get (self, request):
30 return self.get_or_post (request, 'GET')
32 def get_or_post (self, wsgi_request, method):
41 # Retrieve the list of authorities
42 authorities_query = Query.get('authority').select('name', 'authority_hrn')
43 authorities = execute_admin_query(wsgi_request, authorities_query)
44 if authorities is not None:
45 authorities = sorted(authorities)
47 # Get user_email (XXX Would deserve to be simplified)
48 user_query = Query().get('local:user').select('email','config')
49 user_details = execute_query(wsgi_request, user_query)
50 user_email = user_details[0].get('email')
52 for user_detail in user_details:
53 user_config = json.loads(user_detail['config'])
54 user_authority = user_config.get('authority','N/A')
55 # getting the org from authority
56 for authority in authorities:
57 if authority['authority_hrn'] == user_authority:
58 authority_name = authority['name']
60 # Handle the case when we use only hrn and not name
61 if authority_name is None:
62 authority_name = user_authority
64 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
65 account_details = execute_query(wsgi_request, account_query)
67 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
68 platform_details = execute_query(wsgi_request, platform_query)
70 # getting user_hrn from local:account
71 for account_detail in account_details:
72 for platform_detail in platform_details:
73 if platform_detail['platform_id'] == account_detail['platform_id']:
74 # taking user_hrn only from myslice account
75 # NOTE: we should later handle accounts filter_by auth_type= managed OR user
76 if 'myslice' in platform_detail['platform']:
77 account_config = json.loads(account_detail['config'])
78 user_hrn = account_config.get('user_hrn','N/A')
79 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
82 # checking if pi or not
83 if acc_auth_cred == {} or acc_auth_cred == 'N/A':
90 page = Page(wsgi_request)
91 page.add_js_files ( [ "js/jquery.validate.js", "js/jquery-ui.js" ] )
92 page.add_css_files ( [ "https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" ] )
93 page.expose_js_metadata()
96 # The form has been submitted
99 current_site = Site.objects.get_current()
100 current_site = current_site.domain
102 # getting the authority_hrn from the selected organization
103 for authority in authorities:
104 if authority['name'] == wsgi_request.POST.get('org_name', ''):
105 authority_hrn = authority['authority_hrn']
107 # Handle the case when we use only hrn and not name
108 if authority_hrn is None:
109 authority_hrn = wsgi_request.POST.get('org_name', '')
114 'user_hrn' : user_hrn,
115 'email' : user_email,
116 'timestamp' : time.time(),
117 'authority_hrn' : authority_hrn,
118 'organization' : wsgi_request.POST.get('org_name', ''),
119 'slice_name' : wsgi_request.POST.get('slice_name', ''),
120 'url' : wsgi_request.POST.get('url', ''),
121 'purpose' : wsgi_request.POST.get('purpose', ''),
122 'current_site' : current_site
125 # create slice_hrn based on authority_hrn and slice_name
126 slice_name = slice_request['slice_name']
127 req_slice_hrn = authority_hrn + '.' + slice_name
128 # comparing requested slice_hrn with the existing slice_hrn
129 slice_query = Query().get('slice').select('slice_hrn','parent_authority').filter_by('parent_authority','==',authority_hrn)
130 slice_details_sfa = execute_admin_query(wsgi_request, slice_query)
131 for _slice in slice_details_sfa:
132 if _slice['slice_hrn'] == req_slice_hrn:
133 errors.append('Slice already exists. Please use a different slice name.')
136 # What kind of slice name is valid?
137 if (slice_name is None or slice_name == ''):
138 errors.append('Slice name is mandatory')
140 if (re.search(r'^[A-Za-z0-9_]*$', slice_name) == None):
141 errors.append('Slice name may contain only letters, numbers, and underscore.')
143 organization = slice_request['organization']
144 if (organization is None or organization == ''):
145 errors.append('Organization is mandatory')
149 purpose = slice_request['purpose']
150 if (purpose is None or purpose == ''):
151 errors.append('Experiment purpose is mandatory')
153 url = slice_request['url']
156 if is_pi(wsgi_request, user_hrn, authority_hrn):
157 # PIs can directly create slices in their own authority...
158 create_slice(wsgi_request, slice_request)
159 clear_user_creds(wsgi_request, user_email)
160 self.template_name = 'slice-request-done-view.html'
162 # Otherwise a wsgi_request is sent to the PI
163 create_pending_slice(wsgi_request, slice_request, user_email)
164 self.template_name = 'slice-request-ack-view.html'
167 activity.user.slice(wsgi_request)
169 return render(wsgi_request, self.template, {'theme': self.theme}) # Redirect after POST
174 'username': wsgi_request.user.email,
175 'topmenu_items': topmenu_items_live('Request a slice', page),
177 'slice_name': slice_name,
180 'user_hrn': user_hrn,
183 'authority_name': authority_name,
184 'authority_hrn': user_authority,
186 'authorities': authorities,
188 'section': "Slice request"
190 template_env.update(slice_request)
191 template_env.update(page.prelude_env())
192 return render(wsgi_request, self.template, template_env)