1 from django.template import RequestContext
2 from django.shortcuts import render_to_response
4 from portal.templateviews import LoginRequiredAutoLogoutView
6 from unfold.page import Page
7 from manifold.core.query import Query, AnalyzedQuery
9 from myslice.viewutils import topmenu_items, the_user
11 from plugins.raw.raw import Raw
12 from plugins.stack.stack import Stack
13 from plugins.tabs.tabs import Tabs
14 from plugins.lists.slicelist import SliceList
15 from plugins.hazelnut import Hazelnut
16 from plugins.resources_selected import ResourcesSelected
17 from plugins.googlemaps import GoogleMaps
18 from plugins.senslabmap.senslabmap import SensLabMap
19 from plugins.querycode.querycode import QueryCode
20 from plugins.query_editor import QueryEditor
21 from plugins.active_filters import ActiveFilters
22 from plugins.quickfilter.quickfilter import QuickFilter
23 from plugins.messages.messages import Messages
24 #from plugins.updater import Updater
26 tmp_default_slice='ple.upmc.myslicedemo'
28 class SliceView (LoginRequiredAutoLogoutView):
30 def get (self,request, slicename=tmp_default_slice):
33 page.add_css_files ('css/slice-view.css')
34 page.expose_js_metadata()
36 metadata = page.get_metadata()
37 resource_md = metadata.details_by_object('resource')
38 resource_fields = [column['name'] for column in resource_md['column']]
40 user_md = metadata.details_by_object('user')
41 user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
43 # TODO The query to run is embedded in the URL
44 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
47 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn',
50 #'application.measurement_point.counter'
53 query_resource_all = Query.get('resource').select(resource_fields)
54 query_user_all = Query.get('user').select(user_fields)
56 aq = AnalyzedQuery(main_query, metadata=metadata)
57 page.enqueue_query(main_query, analyzed_query=aq)
58 page.enqueue_query(query_resource_all)
59 page.enqueue_query(query_user_all)
61 # ... and for the relations
62 # XXX Let's hardcode resources for now
63 sq_resource = aq.subquery('resource')
64 sq_user = aq.subquery('user')
65 sq_lease = aq.subquery('lease')
66 sq_measurement = aq.subquery('measurement')
69 # Prepare the display according to all metadata
70 # (some parts will be pending, others can be triggered by users).
72 # For example slice measurements will not be requested by default...
74 # Create the base layout (Stack)...
77 title="Slice %s"%slicename,
81 # ... responsible for the slice properties...
88 html="<h2 class='well well-lg'> Slice %s</h2>"%slicename)
92 # Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
95 # the resources part is made of a Stack,
96 # with first a Tabs (List, Geographic),
97 # and second the QueryEditor to tweak the set of attributes to show
98 resources_as_list = Hazelnut(
100 title = 'Resources as a List',
101 domid = 'checkboxes',
102 # this is the query at the core of the slice list
104 query_all = query_resource_all,
106 datatables_options = {
107 # for now we turn off sorting on the checkboxes columns this way
108 # this of course should be automatic in hazelnut
109 'aoColumns' : [None, None, None, None, {'bSortable': False}],
110 'iDisplayLength' : 25,
111 'bLengthChange' : True,
115 resources_as_map = GoogleMaps(
117 title = 'Geographic view',
119 # tab's sons preferably turn this off
122 query_all = query_resource_all,
130 resources_query_editor = QueryEditor(
134 resources_active_filters = ActiveFilters(
139 resources_area = Stack (
144 outline_complete=True,
147 sons=[ resources_as_list, resources_as_map, ]
152 sons = [ resources_query_editor, resources_active_filters, ]
157 main_stack.insert (resources_area)
162 # title="Slice view for %s"%slicename,
168 ############################################################################
171 # A stack inserted in the subquery tab that will hold all operations
172 # related to resources
175 # stack_resources = Stack(
177 # title = 'Resources',
181 # stack_resources.insert(resource_active_filters)
184 # stack_resources.insert(tab_resource_plugins)
186 # sq_plugin.insert(stack_resources)
188 ############################################################################
195 outline_complete = True,
198 active_domid = 'checkboxes2',
200 main_stack.insert(tab_users)
202 tab_users.insert(Hazelnut(
204 title = 'Users List',
205 domid = 'checkboxes2',
206 # tab's sons preferably turn this off
208 # this is the query at the core of the slice list
210 query_all = query_user_all,
212 datatables_options = {
213 # for now we turn off sorting on the checkboxes columns this way
214 # this of course should be automatic in hazelnut
215 'aoColumns' : [None, None, None, None, {'bSortable': False}],
216 'iDisplayLength' : 25,
217 'bLengthChange' : True,
221 tab_measurements = Tabs (
223 active_domid = 'checkboxes3',
224 outline_complete = True,
226 title = 'Measurements',
227 domid = 'measurements',
229 main_stack.insert(tab_measurements)
231 tab_measurements.insert(Hazelnut(
233 title = 'Measurements',
234 domid = 'checkboxes3',
235 # tab's sons preferably turn this off
237 # this is the query at the core of the slice list
238 query = sq_measurement,
240 datatables_options = {
241 # for now we turn off sorting on the checkboxes columns this way
242 # this of course should be automatic in hazelnut
243 'aoColumns' : [None, None, None, None, {'bSortable': False}],
244 'iDisplayLength' : 25,
245 'bLengthChange' : True,
249 # main_stack.insert(sq_plugin)
251 # --------------------------------------------------------------------------
254 main_stack.insert(ResourcesSelected(
256 title = 'Pending operations',
260 outline_complete = True,
263 main_stack.insert(Messages(
265 title = "Runtime messages for slice %s"%slicename,
269 # main_stack.insert(Updater(
271 # title = "wont show up as non togglable by default",
272 # query = main_query,
273 # label = "Update slice",
278 # variables that will get passed to the view-unfold1.html template
281 # define 'unfold1_main' to the template engine - the main contents
282 template_env [ 'unfold1_main' ] = main_stack.render(request)
284 # more general variables expected in the template
285 template_env [ 'title' ] = '%(slicename)s'%locals()
286 # the menu items on the top
287 template_env [ 'topmenu_items' ] = topmenu_items('Slice', request)
288 # so we can sho who is logged
289 template_env [ 'username' ] = the_user (request)
291 # don't forget to run the requests
292 page.expose_queries ()
294 # xxx create another plugin with the same query and a different layout (with_datatables)
295 # show that it worls as expected, one single api call to backend and 2 refreshed views
297 # the prelude object in page contains a summary of the requirements() for all plugins
298 # define {js,css}_{files,chunks}
299 prelude_env = page.prelude_env()
300 template_env.update(prelude_env)
301 result=render_to_response ('view-unfold1.html',template_env,
302 context_instance=RequestContext(request))