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.heartbeat'
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)
58 # Get default fields from metadata unless specified
59 if not main_query.fields:
60 metadata = page.get_metadata()
61 md_fields = metadata.details_by_object('slice')
63 print "METADATA", md_fields
64 # TODO Get default fields
67 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.authority',
70 # 'application.measurement_point.counter'
73 aq = AnalyzedQuery(main_query)
74 page.enqueue_query(main_query, analyzed_query=aq)
76 # Prepare the display according to all metadata
77 # (some parts will be pending, others can be triggered by users).
79 # For example slice measurements will not be requested by default...
81 # Create the base layout (Stack)...
84 title="Slice !!view for %s"%slicename,
88 # ... responsible for the slice properties...
92 Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename)
96 Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
101 title="Slice view for %s"%slicename,
107 # ... and for the relations
108 # XXX Let's hardcode resources for now
109 sq_resource = aq.subquery('resource')
110 sq_user = aq.subquery('user')
111 sq_lease = aq.subquery('lease')
112 sq_measurement = aq.subquery('measurement')
115 ############################################################################
118 # A stack inserted in the subquery tab that will hold all operations
119 # related to resources
122 stack_resources = Stack(
128 # --------------------------------------------------------------------------
129 # Different displays = DataTables + GoogleMaps
131 tab_resource_plugins = Tabs(
136 tab_resource_plugins.insert(Hazelnut(
139 domid = 'checkboxes',
140 # this is the query at the core of the slice list
143 datatables_options = {
144 # for now we turn off sorting on the checkboxes columns this way
145 # this of course should be automatic in hazelnut
146 'aoColumns' : [None, None, None, None, {'bSortable': False}],
147 'iDisplayLength' : 25,
148 'bLengthChange' : True,
152 tab_resource_plugins.insert(GoogleMap(
154 title = 'Geographic view',
156 # tab's sons preferably turn this off
165 stack_resources.insert(tab_resource_plugins)
167 # --------------------------------------------------------------------------
170 stack_resources.insert(ResourcesSelected(
172 title = 'Pending operations',
173 resource_query_uuid = sq_resource,
174 lease_query_uuid = sq_lease,
178 sq_plugin.insert(stack_resources)
180 ############################################################################
188 # activeid = 'checkboxes',
189 active_domid = 'checkboxes2',
191 sq_plugin.insert(tab_users)
193 tab_users.insert(Hazelnut(
196 domid = 'checkboxes2',
197 # tab's sons preferably turn this off
199 # this is the query at the core of the slice list
202 datatables_options = {
203 # for now we turn off sorting on the checkboxes columns this way
204 # this of course should be automatic in hazelnut
205 'aoColumns' : [None, None, None, None, {'bSortable': False}],
206 'iDisplayLength' : 25,
207 'bLengthChange' : True,
211 tab_measurements = Tabs (
213 title = 'Measurements',
215 # activeid = 'checkboxes',
216 active_domid = 'checkboxes3',
218 sq_plugin.insert(tab_measurements)
220 tab_measurements.insert(Hazelnut(
223 domid = 'checkboxes3',
224 # tab's sons preferably turn this off
226 # this is the query at the core of the slice list
227 query = sq_measurement,
229 datatables_options = {
230 # for now we turn off sorting on the checkboxes columns this way
231 # this of course should be automatic in hazelnut
232 'aoColumns' : [None, None, None, None, {'bSortable': False}],
233 'iDisplayLength' : 25,
234 'bLengthChange' : True,
238 main_plugin.insert(sq_plugin)
240 main_plugin.insert(Messages(
242 title = "Runtime messages for slice %s"%slicename,
246 main_plugin.insert(Updater(
248 title = "wont show up as non togglable by default",
250 label = "Update slice",
254 # END OF JORDAN's CODE
256 #old# main_plugin = Stack (
258 #old# title="Slice view for %s"%slicename,
259 #old# domid='thestack',
260 #old# togglable=False,
262 #old# Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename),
265 #old# title="Runtime messages for slice %s"%slicename,
266 #old# domid="msgs-pre",
271 #old# title="2 tabs : w/ and w/o checkboxes",
272 #old# domid='thetabs',
273 #old# # active_domid='checkboxes',
274 #old# active_domid='gmap',
278 #old# title='a sample and simple hazelnut',
279 #old# domid='simple',
280 #old# # tab's sons preferably turn this off
281 #old# togglable=False,
282 #old# # this is the query at the core of the slice list
283 #old# query=main_query,
287 #old# title='with checkboxes',
288 #old# domid='checkboxes',
289 #old# # tab's sons preferably turn this off
290 #old# togglable=False,
291 #old# # this is the query at the core of the slice list
292 #old# query=main_query,
293 #old# checkboxes=True,
294 #old# datatables_options = {
295 #old# # for now we turn off sorting on the checkboxes columns this way
296 #old# # this of course should be automatic in hazelnut
297 #old# 'aoColumns' : [ None, None, None, None, {'bSortable': False} ],
298 #old# 'iDisplayLength' : 25,
299 #old# 'bLengthChange' : True,
304 #old# title='geographic view',
306 #old# # tab's sons preferably turn this off
307 #old# togglable=False,
308 #old# query=main_query,
309 #old# # center on Paris
317 #old# title='3D view (disabled)',
319 #old## # tab's sons preferably turn this off
320 #old# togglable=False,
321 #old## query=main_query,
322 #old# html="""<p class='well'>
323 #old#Thierry: I am commeting off the use of <button class="btn btn-danger">SensLabMap</button> which,
324 #old# although rudimentarily ported to the django framework,
325 #old#causes a weird behaviour especially wrt scrolling.
326 #old#On my Mac <button class="btn btn-warning"> I cannot use the mouse to scroll</button> any longer
327 #old#if I keep this active, so for now it's disabled
333 #old# title='a hazelnut not in tabs',
334 #old# domid='standalone',
335 #old# # this is the query at the core of the slice list
336 #old# query=main_query,
337 #old# columns=['hrn','hostname'],
339 #old# # you don't *have to* set a domid, but if you plan on using toggled=persistent then it's required
340 #old# # because domid is the key for storing toggle status in the browser
343 #old# title='xmlrpc code (toggled=False)',
344 #old# query=main_query,
345 #old## domid='xmlrpc',
350 #old# title="QuickFilter - requires metadata (toggled=False)",
351 #old# criterias=quickfilter_criterias,
352 #old# domid='filters',
357 #old# title="Runtime messages (again)",
358 #old# domid="msgs-post",
362 # variables that will get passed to the view-unfold1.html template
365 # define 'unfold1_main' to the template engine - the main contents
366 template_env [ 'unfold1_main' ] = main_plugin.render(request)
368 # more general variables expected in the template
369 template_env [ 'title' ] = 'Test view that combines various plugins'
370 # the menu items on the top
371 template_env [ 'topmenu_items' ] = topmenu_items('slice', request)
372 # so we can sho who is logged
373 template_env [ 'username' ] = the_user (request)
375 # don't forget to run the requests
376 page.expose_queries ()
378 # xxx create another plugin with the same query and a different layout (with_datatables)
379 # show that it worls as expected, one single api call to backend and 2 refreshed views
381 # the prelude object in page contains a summary of the requirements() for all plugins
382 # define {js,css}_{files,chunks}
383 prelude_env = page.prelude_env()
384 template_env.update(prelude_env)
385 result=render_to_response ('view-unfold1.html',template_env,
386 context_instance=RequestContext(request))