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.query_editor import QueryEditor
24 from plugins.quickfilter.quickfilter import QuickFilter
25 from plugins.messages.messages import Messages
26 from plugins.updater.updater import Updater
28 tmp_default_slice='ple.inria.myslicedemo'
32 def slice_view (request, slicename=tmp_default_slice):
33 # xxx Thierry - ugly hack
34 # fetching metadata here might fail - e.g. with an expired session..
35 # let's catch this early on and log out our user if needed
36 # it should of course be handled in a more generic way
38 return _slice_view(request,slicename)
39 except ManifoldException, manifold_result:
40 # xxx needs a means to display this message to user...
41 from django.contrib.auth import logout
43 return HttpResponseRedirect ('/')
45 # xxx we need to sugarcoat this error message in some error template...
46 print "Unexpected exception",e
51 def _slice_view (request, slicename):
54 page.expose_js_metadata()
56 metadata = page.get_metadata()
57 resource_md = metadata.details_by_object('resource')
58 resource_fields = [column['name'] for column in resource_md['column']]
60 # TODO The query to run is embedded in the URL
61 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
64 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn',
67 #'application.measurement_point.counter'
70 query_resource_all = Query.get('resource').select(resource_fields)
72 aq = AnalyzedQuery(main_query, metadata=metadata)
73 page.enqueue_query(main_query, analyzed_query=aq)
74 page.enqueue_query(query_resource_all)
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 resource_query_editor = QueryEditor(
132 stack_resources.insert(resource_query_editor)
134 # --------------------------------------------------------------------------
135 # Different displays = DataTables + GoogleMaps
137 tab_resource_plugins = Tabs(
142 tab_resource_plugins.insert(Hazelnut(
145 domid = 'checkboxes',
146 # this is the query at the core of the slice list
148 query_all = query_resource_all,
150 datatables_options = {
151 # for now we turn off sorting on the checkboxes columns this way
152 # this of course should be automatic in hazelnut
153 'aoColumns' : [None, None, None, None, {'bSortable': False}],
154 'iDisplayLength' : 25,
155 'bLengthChange' : True,
159 tab_resource_plugins.insert(GoogleMap(
161 title = 'Geographic view',
163 # tab's sons preferably turn this off
166 query_all = query_resource_all,
174 stack_resources.insert(tab_resource_plugins)
176 # --------------------------------------------------------------------------
179 stack_resources.insert(ResourcesSelected(
181 title = 'Pending operations',
182 resource_query_uuid = sq_resource,
183 lease_query_uuid = sq_lease,
187 sq_plugin.insert(stack_resources)
189 ############################################################################
197 # activeid = 'checkboxes',
198 active_domid = 'checkboxes2',
200 sq_plugin.insert(tab_users)
202 tab_users.insert(Hazelnut(
205 domid = 'checkboxes2',
206 # tab's sons preferably turn this off
208 # this is the query at the core of the slice list
211 datatables_options = {
212 # for now we turn off sorting on the checkboxes columns this way
213 # this of course should be automatic in hazelnut
214 'aoColumns' : [None, None, None, None, {'bSortable': False}],
215 'iDisplayLength' : 25,
216 'bLengthChange' : True,
220 tab_measurements = Tabs (
222 title = 'Measurements',
224 # activeid = 'checkboxes',
225 active_domid = 'checkboxes3',
227 sq_plugin.insert(tab_measurements)
229 tab_measurements.insert(Hazelnut(
232 domid = 'checkboxes3',
233 # tab's sons preferably turn this off
235 # this is the query at the core of the slice list
236 query = sq_measurement,
238 datatables_options = {
239 # for now we turn off sorting on the checkboxes columns this way
240 # this of course should be automatic in hazelnut
241 'aoColumns' : [None, None, None, None, {'bSortable': False}],
242 'iDisplayLength' : 25,
243 'bLengthChange' : True,
247 main_plugin.insert(sq_plugin)
249 main_plugin.insert(Messages(
251 title = "Runtime messages for slice %s"%slicename,
255 main_plugin.insert(Updater(
257 title = "wont show up as non togglable by default",
259 label = "Update slice",
264 # variables that will get passed to the view-unfold1.html template
267 # define 'unfold1_main' to the template engine - the main contents
268 template_env [ 'unfold1_main' ] = main_plugin.render(request)
270 # more general variables expected in the template
271 template_env [ 'title' ] = 'Test view that combines various plugins'
272 # the menu items on the top
273 template_env [ 'topmenu_items' ] = topmenu_items('Slice', request)
274 # so we can sho who is logged
275 template_env [ 'username' ] = the_user (request)
277 # don't forget to run the requests
278 page.expose_queries ()
280 # xxx create another plugin with the same query and a different layout (with_datatables)
281 # show that it worls as expected, one single api call to backend and 2 refreshed views
283 # the prelude object in page contains a summary of the requirements() for all plugins
284 # define {js,css}_{files,chunks}
285 prelude_env = page.prelude_env()
286 template_env.update(prelude_env)
287 result=render_to_response ('view-unfold1.html',template_env,
288 context_instance=RequestContext(request))