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
26 tmp_default_slice='ple.inria.heartbeat'
30 def slice_view (request, slicename=tmp_default_slice):
31 # xxx Thierry - ugly hack
32 # fetching metadata here might fail - e.g. with an expired session..
33 # let's catch this early on and log out our user if needed
34 # it should of course be handled in a more generic way
36 return _slice_view(request,slicename)
37 except ManifoldException, manifold_result:
38 # xxx needs a means to display this message to user...
39 from django.contrib.auth import logout
41 return HttpResponseRedirect ('/')
43 # xxx we need to sugarcoat this error message in some error template...
44 print "Unexpected exception",e
49 def _slice_view (request, slicename):
52 page.expose_js_metadata()
54 # TODO The query to run is embedded in the URL
55 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
57 # Get default fields from metadata unless specified
58 if not main_query.fields:
59 metadata = page.get_metadata()
60 md_fields = metadata.details_by_object('slice')
62 print "METADATA", md_fields
63 # TODO Get default fields
66 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.authority',
69 # 'application.measurement_point.counter'
72 aq = AnalyzedQuery(main_query)
73 page.enqueue_query(main_query, analyzed_query=aq)
75 # Prepare the display according to all metadata
76 # (some parts will be pending, others can be triggered by users).
78 # For example slice measurements will not be requested by default...
80 # Create the base layout (Stack)...
83 title="Slice !!view for %s"%slicename,
87 # ... responsible for the slice properties...
91 Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename)
95 Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
100 title="Slice view for %s"%slicename,
106 # ... and for the relations
107 # XXX Let's hardcode resources for now
108 sq_resource = aq.subquery('resource')
109 sq_user = aq.subquery('user')
110 sq_lease = aq.subquery('lease')
111 sq_measurement = aq.subquery('measurement')
114 ############################################################################
117 # A stack inserted in the subquery tab that will hold all operations
118 # related to resources
121 stack_resources = Stack(
127 # --------------------------------------------------------------------------
128 # Different displays = DataTables + GoogleMaps
130 tab_resource_plugins = Tabs(
135 tab_resource_plugins.insert(Hazelnut(
138 domid = 'checkboxes',
139 # this is the query at the core of the slice list
142 datatables_options = {
143 # for now we turn off sorting on the checkboxes columns this way
144 # this of course should be automatic in hazelnut
145 'aoColumns' : [None, None, None, None, {'bSortable': False}],
146 'iDisplayLength' : 25,
147 'bLengthChange' : True,
151 tab_resource_plugins.insert(GoogleMap(
153 title = 'Geographic view',
155 # tab's sons preferably turn this off
164 stack_resources.insert(tab_resource_plugins)
166 # --------------------------------------------------------------------------
169 stack_resources.insert(ResourcesSelected(
171 title = 'Pending operations',
172 resource_query_uuid = sq_resource,
173 lease_query_uuid = sq_lease,
177 sq_plugin.insert(stack_resources)
179 ############################################################################
187 # activeid = 'checkboxes',
188 active_domid = 'checkboxes2',
190 sq_plugin.insert(tab_users)
192 tab_users.insert(Hazelnut(
195 domid = 'checkboxes2',
196 # tab's sons preferably turn this off
198 # this is the query at the core of the slice list
201 datatables_options = {
202 # for now we turn off sorting on the checkboxes columns this way
203 # this of course should be automatic in hazelnut
204 'aoColumns' : [None, None, None, None, {'bSortable': False}],
205 'iDisplayLength' : 25,
206 'bLengthChange' : True,
210 tab_measurements = Tabs (
212 title = 'Measurements',
214 # activeid = 'checkboxes',
215 active_domid = 'checkboxes3',
217 sq_plugin.insert(tab_measurements)
219 tab_measurements.insert(Hazelnut(
222 domid = 'checkboxes3',
223 # tab's sons preferably turn this off
225 # this is the query at the core of the slice list
226 query = sq_measurement,
228 datatables_options = {
229 # for now we turn off sorting on the checkboxes columns this way
230 # this of course should be automatic in hazelnut
231 'aoColumns' : [None, None, None, None, {'bSortable': False}],
232 'iDisplayLength' : 25,
233 'bLengthChange' : True,
237 main_plugin.insert(sq_plugin)
242 title="Runtime messages for slice %s"%slicename,
248 # END OF JORDAN's CODE
250 #old# main_plugin = Stack (
252 #old# title="Slice view for %s"%slicename,
253 #old# domid='thestack',
254 #old# togglable=False,
256 #old# Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename),
259 #old# title="Runtime messages for slice %s"%slicename,
260 #old# domid="msgs-pre",
265 #old# title="2 tabs : w/ and w/o checkboxes",
266 #old# domid='thetabs',
267 #old# # active_domid='checkboxes',
268 #old# active_domid='gmap',
272 #old# title='a sample and simple hazelnut',
273 #old# domid='simple',
274 #old# # tab's sons preferably turn this off
275 #old# togglable=False,
276 #old# # this is the query at the core of the slice list
277 #old# query=main_query,
281 #old# title='with checkboxes',
282 #old# domid='checkboxes',
283 #old# # tab's sons preferably turn this off
284 #old# togglable=False,
285 #old# # this is the query at the core of the slice list
286 #old# query=main_query,
287 #old# checkboxes=True,
288 #old# datatables_options = {
289 #old# # for now we turn off sorting on the checkboxes columns this way
290 #old# # this of course should be automatic in hazelnut
291 #old# 'aoColumns' : [ None, None, None, None, {'bSortable': False} ],
292 #old# 'iDisplayLength' : 25,
293 #old# 'bLengthChange' : True,
298 #old# title='geographic view',
300 #old# # tab's sons preferably turn this off
301 #old# togglable=False,
302 #old# query=main_query,
303 #old# # center on Paris
311 #old# title='3D view (disabled)',
313 #old## # tab's sons preferably turn this off
314 #old# togglable=False,
315 #old## query=main_query,
316 #old# html="""<p class='well'>
317 #old#Thierry: I am commeting off the use of <button class="btn btn-danger">SensLabMap</button> which,
318 #old# although rudimentarily ported to the django framework,
319 #old#causes a weird behaviour especially wrt scrolling.
320 #old#On my Mac <button class="btn btn-warning"> I cannot use the mouse to scroll</button> any longer
321 #old#if I keep this active, so for now it's disabled
327 #old# title='a hazelnut not in tabs',
328 #old# domid='standalone',
329 #old# # this is the query at the core of the slice list
330 #old# query=main_query,
331 #old# columns=['hrn','hostname'],
333 #old# # you don't *have to* set a domid, but if you plan on using toggled=persistent then it's required
334 #old# # because domid is the key for storing toggle status in the browser
337 #old# title='xmlrpc code (toggled=False)',
338 #old# query=main_query,
339 #old## domid='xmlrpc',
344 #old# title="QuickFilter - requires metadata (toggled=False)",
345 #old# criterias=quickfilter_criterias,
346 #old# domid='filters',
351 #old# title="Runtime messages (again)",
352 #old# domid="msgs-post",
356 # variables that will get passed to the view-unfold1.html template
359 # define 'unfold1_main' to the template engine - the main contents
360 template_env [ 'unfold1_main' ] = main_plugin.render(request)
362 # more general variables expected in the template
363 template_env [ 'title' ] = 'Test view that combines various plugins'
364 # the menu items on the top
365 template_env [ 'topmenu_items' ] = topmenu_items('slice', request)
366 # so we can sho who is logged
367 template_env [ 'username' ] = the_user (request)
369 # don't forget to run the requests
370 page.expose_queries ()
372 # xxx create another plugin with the same query and a different layout (with_datatables)
373 # show that it worls as expected, one single api call to backend and 2 refreshed views
375 # the prelude object in page contains a summary of the requirements() for all plugins
376 # define {js,css}_{files,chunks}
377 prelude_env = page.prelude_env()
378 template_env.update(prelude_env)
379 result=render_to_response ('view-unfold1.html',template_env,
380 context_instance=RequestContext(request))