1 # Create your views here.
3 from django.template import RequestContext
4 from django.shortcuts import render_to_response
5 from django.contrib.auth.decorators import login_required
6 from django.http import HttpResponseRedirect
8 from unfold.page import Page
9 from manifold.core.query import Query, AnalyzedQuery
10 from manifold.manifoldresult import ManifoldException
11 from manifold.metadata import MetaData as Metadata
12 from myslice.viewutils import quickfilter_criterias, topmenu_items, the_user
14 from plugins.raw.raw import Raw
15 from plugins.stack.stack import Stack
16 from plugins.tabs.tabs import Tabs
17 from plugins.lists.slicelist import SliceList
18 from plugins.hazelnut.hazelnut import Hazelnut
19 from plugins.resources_selected import ResourcesSelected
20 from plugins.googlemap.googlemap import GoogleMap
21 from plugins.senslabmap.senslabmap import SensLabMap
22 from plugins.querycode.querycode import QueryCode
23 from plugins.quickfilter.quickfilter import QuickFilter
24 from plugins.messages.messages import Messages
25 from plugins.updater.updater import Updater
27 tmp_default_slice='ple.inria.myslicedemo'
31 def slice_view (request, slicename=tmp_default_slice):
32 # xxx Thierry - ugly hack
33 # fetching metadata here might fail - e.g. with an expired session..
34 # let's catch this early on and log out our user if needed
35 # it should of course be handled in a more generic way
37 return _slice_view(request,slicename)
38 except ManifoldException, manifold_result:
39 # xxx needs a means to display this message to user...
40 from django.contrib.auth import logout
42 return HttpResponseRedirect ('/')
44 # xxx we need to sugarcoat this error message in some error template...
45 print "Unexpected exception",e
50 def _slice_view (request, slicename):
53 page.expose_js_metadata()
55 # TODO The query to run is embedded in the URL
56 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
57 query_resource_all = Query.get('resource').select('resource_hrn', 'hostname', 'type', 'network_hrn', 'latitude', 'longitude')
59 # Get default fields from metadata unless specified
60 if not main_query.fields:
61 metadata = page.get_metadata()
62 md_fields = metadata.details_by_object('slice')
64 print "METADATA", md_fields
65 # TODO Get default fields
68 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn',
71 # 'application.measurement_point.counter'
74 aq = AnalyzedQuery(main_query, metadata=metadata)
75 page.enqueue_query(main_query, analyzed_query=aq)
76 page.enqueue_query(query_resource_all)
78 # Prepare the display according to all metadata
79 # (some parts will be pending, others can be triggered by users).
81 # For example slice measurements will not be requested by default...
83 # Create the base layout (Stack)...
86 title="Slice !!view for %s"%slicename,
90 # ... responsible for the slice properties...
94 Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename)
98 Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
103 title="Slice view for %s"%slicename,
109 # ... and for the relations
110 # XXX Let's hardcode resources for now
111 sq_resource = aq.subquery('resource')
112 sq_user = aq.subquery('user')
113 sq_lease = aq.subquery('lease')
114 sq_measurement = aq.subquery('measurement')
117 ############################################################################
120 # A stack inserted in the subquery tab that will hold all operations
121 # related to resources
124 stack_resources = Stack(
130 # --------------------------------------------------------------------------
131 # Different displays = DataTables + GoogleMaps
133 tab_resource_plugins = Tabs(
138 tab_resource_plugins.insert(Hazelnut(
141 domid = 'checkboxes',
142 # this is the query at the core of the slice list
144 query_all_uuid = query_resource_all.query_uuid,
146 datatables_options = {
147 # for now we turn off sorting on the checkboxes columns this way
148 # this of course should be automatic in hazelnut
149 'aoColumns' : [None, None, None, None, {'bSortable': False}],
150 'iDisplayLength' : 25,
151 'bLengthChange' : True,
155 tab_resource_plugins.insert(GoogleMap(
157 title = 'Geographic view',
159 # tab's sons preferably turn this off
162 query_all_uuid = query_resource_all.query_uuid,
170 stack_resources.insert(tab_resource_plugins)
172 # --------------------------------------------------------------------------
175 stack_resources.insert(ResourcesSelected(
177 title = 'Pending operations',
178 resource_query_uuid = sq_resource,
179 lease_query_uuid = sq_lease,
183 sq_plugin.insert(stack_resources)
185 ############################################################################
193 # activeid = 'checkboxes',
194 active_domid = 'checkboxes2',
196 sq_plugin.insert(tab_users)
198 tab_users.insert(Hazelnut(
201 domid = 'checkboxes2',
202 # tab's sons preferably turn this off
204 # this is the query at the core of the slice list
207 datatables_options = {
208 # for now we turn off sorting on the checkboxes columns this way
209 # this of course should be automatic in hazelnut
210 'aoColumns' : [None, None, None, None, {'bSortable': False}],
211 'iDisplayLength' : 25,
212 'bLengthChange' : True,
216 tab_measurements = Tabs (
218 title = 'Measurements',
220 # activeid = 'checkboxes',
221 active_domid = 'checkboxes3',
223 sq_plugin.insert(tab_measurements)
225 tab_measurements.insert(Hazelnut(
228 domid = 'checkboxes3',
229 # tab's sons preferably turn this off
231 # this is the query at the core of the slice list
232 query = sq_measurement,
234 datatables_options = {
235 # for now we turn off sorting on the checkboxes columns this way
236 # this of course should be automatic in hazelnut
237 'aoColumns' : [None, None, None, None, {'bSortable': False}],
238 'iDisplayLength' : 25,
239 'bLengthChange' : True,
243 main_plugin.insert(sq_plugin)
245 main_plugin.insert(Messages(
247 title = "Runtime messages for slice %s"%slicename,
251 main_plugin.insert(Updater(
253 title = "wont show up as non togglable by default",
255 label = "Update slice",
259 # END OF JORDAN's CODE
261 #old# main_plugin = Stack (
263 #old# title="Slice view for %s"%slicename,
264 #old# domid='thestack',
265 #old# togglable=False,
267 #old# Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename),
270 #old# title="Runtime messages for slice %s"%slicename,
271 #old# domid="msgs-pre",
276 #old# title="2 tabs : w/ and w/o checkboxes",
277 #old# domid='thetabs',
278 #old# # active_domid='checkboxes',
279 #old# active_domid='gmap',
283 #old# title='a sample and simple hazelnut',
284 #old# domid='simple',
285 #old# # tab's sons preferably turn this off
286 #old# togglable=False,
287 #old# # this is the query at the core of the slice list
288 #old# query=main_query,
292 #old# title='with checkboxes',
293 #old# domid='checkboxes',
294 #old# # tab's sons preferably turn this off
295 #old# togglable=False,
296 #old# # this is the query at the core of the slice list
297 #old# query=main_query,
298 #old# checkboxes=True,
299 #old# datatables_options = {
300 #old# # for now we turn off sorting on the checkboxes columns this way
301 #old# # this of course should be automatic in hazelnut
302 #old# 'aoColumns' : [ None, None, None, None, {'bSortable': False} ],
303 #old# 'iDisplayLength' : 25,
304 #old# 'bLengthChange' : True,
309 #old# title='geographic view',
311 #old# # tab's sons preferably turn this off
312 #old# togglable=False,
313 #old# query=main_query,
314 #old# # center on Paris
322 #old# title='3D view (disabled)',
324 #old## # tab's sons preferably turn this off
325 #old# togglable=False,
326 #old## query=main_query,
327 #old# html="""<p class='well'>
328 #old#Thierry: I am commeting off the use of <button class="btn btn-danger">SensLabMap</button> which,
329 #old# although rudimentarily ported to the django framework,
330 #old#causes a weird behaviour especially wrt scrolling.
331 #old#On my Mac <button class="btn btn-warning"> I cannot use the mouse to scroll</button> any longer
332 #old#if I keep this active, so for now it's disabled
338 #old# title='a hazelnut not in tabs',
339 #old# domid='standalone',
340 #old# # this is the query at the core of the slice list
341 #old# query=main_query,
342 #old# columns=['hrn','hostname'],
344 #old# # you don't *have to* set a domid, but if you plan on using toggled=persistent then it's required
345 #old# # because domid is the key for storing toggle status in the browser
348 #old# title='xmlrpc code (toggled=False)',
349 #old# query=main_query,
350 #old## domid='xmlrpc',
355 #old# title="QuickFilter - requires metadata (toggled=False)",
356 #old# criterias=quickfilter_criterias,
357 #old# domid='filters',
362 #old# title="Runtime messages (again)",
363 #old# domid="msgs-post",
367 # variables that will get passed to the view-unfold1.html template
370 # define 'unfold1_main' to the template engine - the main contents
371 template_env [ 'unfold1_main' ] = main_plugin.render(request)
373 # more general variables expected in the template
374 template_env [ 'title' ] = 'Test view that combines various plugins'
375 # the menu items on the top
376 template_env [ 'topmenu_items' ] = topmenu_items('Slice', request)
377 # so we can sho who is logged
378 template_env [ 'username' ] = the_user (request)
380 # don't forget to run the requests
381 page.expose_queries ()
383 # xxx create another plugin with the same query and a different layout (with_datatables)
384 # show that it worls as expected, one single api call to backend and 2 refreshed views
386 # the prelude object in page contains a summary of the requirements() for all plugins
387 # define {js,css}_{files,chunks}
388 prelude_env = page.prelude_env()
389 template_env.update(prelude_env)
390 result=render_to_response ('view-unfold1.html',template_env,
391 context_instance=RequestContext(request))