Scheduler2 plugin: Queries handled by the plugin = query (resource in slice), query_l...
[myslice.git] / portal / slicerequestview.py
1 from django.shortcuts           import render
2 from django.contrib.sites.models import Site
3
4
5 from unfold.page                import Page
6
7 from manifold.core.query        import Query
8 from manifoldapi.manifoldapi    import execute_admin_query, execute_query
9
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
14
15 from myslice.theme import ThemeView
16
17 import json, time
18
19 class SliceRequestView (LoginRequiredAutoLogoutView, ThemeView):
20     template_name = 'slicerequest_view.html'
21     
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')
26
27     def get (self, request):
28         return self.get_or_post (request, 'GET')
29
30     def get_or_post  (self, wsgi_request, method):
31         """
32         """
33         errors = []
34
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)
40
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')
45         #
46         account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
47         account_details = execute_query(wsgi_request, account_query)
48         #
49         platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
50         platform_details = execute_query(wsgi_request, platform_query)
51         user_hrn = None
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')
62
63
64         # checking if pi or not
65         if acc_auth_cred == {}:
66             pi = "is_not_pi"
67         else:
68             pi = "is_pi"
69
70
71         # Page rendering
72         page = Page(wsgi_request)
73         page.add_css_files ( [ "http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" ] )
74
75         if method == 'POST':
76             # The form has been submitted
77
78             # get the domain url
79             current_site = Site.objects.get_current()
80             current_site = current_site.domain
81
82             slice_request = {
83                 'type'              : 'slice',
84                 'id'                : None,
85                 'user_hrn'          : user_hrn,
86                 'email'             : user_email,
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
93             }
94             
95             authority_hrn = slice_request['authority_hrn']
96             if (authority_hrn is None or authority_hrn == ''):
97                 errors.append('Please, select an authority')
98
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')
103     
104             purpose = slice_request['purpose']
105             if (purpose is None or purpose == ''):
106                 errors.append('Purpose is mandatory')
107
108             if not errors:
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'
113                 else:
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'
117                 
118                 return render(wsgi_request, self.template, {'theme': self.theme}) # Redirect after POST
119         else:
120             slice_request = {}
121
122         template_env = {
123             'username': wsgi_request.user.email,
124             'topmenu_items': topmenu_items_live('Request a slice', page),
125             'errors': errors,
126             'email': user_email,
127             'user_hrn': user_hrn,
128             'pi': pi,        
129             'cc_myself': True,
130             'authorities': authorities,
131             'theme': self.theme,
132             'section': "Slice request"
133         }
134         template_env.update(slice_request)
135         template_env.update(page.prelude_env())
136         return render(wsgi_request, self.template, template_env)