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
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
19 class SliceRequestView (LoginRequiredAutoLogoutView, ThemeView):
20 template_name = 'slicerequest_view.html'
22 # because we inherit LoginRequiredAutoLogoutView that is implemented by redefining 'dispatch'
23 # we cannot redefine dispatch here, or we'd lose LoginRequired and AutoLogout behaviours
24 def post (self, request):
25 return self.get_or_post (request, 'POST')
27 def get (self, request):
28 return self.get_or_post (request, 'GET')
30 def get_or_post (self, wsgi_request, method):
35 # Retrieve the list of authorities
36 authorities_query = Query.get('authority').select('name', 'authority_hrn')
37 authorities = execute_admin_query(wsgi_request, authorities_query)
38 if authorities is not None:
39 authorities = sorted(authorities)
41 # Get user_hrn (XXX Would deserve to be simplified)
42 user_query = Query().get('local:user').select('email')
43 user_emails = execute_query(wsgi_request, user_query)
44 user_email = user_emails[0].get('email')
46 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
47 account_details = execute_query(wsgi_request, account_query)
49 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
50 platform_details = execute_query(wsgi_request, platform_query)
52 # getting user_hrn from local:account
53 for account_detail in account_details:
54 for platform_detail in platform_details:
55 if platform_detail['platform_id'] == account_detail['platform_id']:
56 # taking user_hrn only from myslice account
57 # NOTE: we should later handle accounts filter_by auth_type= managed OR user
58 if 'myslice' in platform_detail['platform']:
59 account_config = json.loads(account_detail['config'])
60 user_hrn = account_config.get('user_hrn','N/A')
61 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
64 # checking if pi or not
65 if acc_auth_cred == {}:
72 page = Page(wsgi_request)
73 page.add_css_files ( [ "http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" ] )
76 # The form has been submitted
79 current_site = Site.objects.get_current()
80 current_site = current_site.domain
85 'user_hrn' : user_hrn,
87 'timestamp' : time.time(),
88 'authority_hrn' : wsgi_request.POST.get('authority_hrn', ''),
89 'slice_name' : wsgi_request.POST.get('slice_name', ''),
90 'number_of_nodes' : wsgi_request.POST.get('number_of_nodes', ''),
91 'purpose' : wsgi_request.POST.get('purpose', ''),
92 'current_site' : current_site
95 authority_hrn = slice_request['authority_hrn']
96 if (authority_hrn is None or authority_hrn == ''):
97 errors.append('Please, select an authority')
99 # What kind of slice name is valid?
100 slice_name = slice_request['slice_name']
101 if (slice_name is None or slice_name == ''):
102 errors.append('Slice Name is mandatory')
104 purpose = slice_request['purpose']
105 if (purpose is None or purpose == ''):
106 errors.append('Purpose is mandatory')
109 if is_pi(wsgi_request, user_hrn, authority_hrn):
110 # PIs can directly create slices in their own authority...
111 create_slice(wsgi_request, slice_request)
112 self.template_name = 'slice-request-done-view.html'
114 # Otherwise a wsgi_request is sent to the PI
115 create_pending_slice(wsgi_request, slice_request, user_email)
116 self.template_name = 'slice-request-ack-view.html'
118 return render(wsgi_request, self.template, {'theme': self.theme}) # Redirect after POST
123 'username': wsgi_request.user.email,
124 'topmenu_items': topmenu_items_live('Request a slice', page),
127 'user_hrn': user_hrn,
130 'authorities': authorities,
132 'section': "Slice request"
134 template_env.update(slice_request)
135 template_env.update(page.prelude_env())
136 return render(wsgi_request, self.template, template_env)