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 # TODO The query to run is embedded in the URL
57 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
58 query_resource_all = Query.get('resource').select('resource_hrn', 'hostname', 'type', 'network_hrn', 'latitude', 'longitude')
60 # Get default fields from metadata unless specified
61 if not main_query.fields:
62 metadata = page.get_metadata()
63 md_fields = metadata.details_by_object('slice')
65 print "METADATA", md_fields
66 # TODO Get default fields
69 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn',
72 # 'application.measurement_point.counter'
75 aq = AnalyzedQuery(main_query, metadata=metadata)
76 page.enqueue_query(main_query, analyzed_query=aq)
77 page.enqueue_query(query_resource_all)
79 # Prepare the display according to all metadata
80 # (some parts will be pending, others can be triggered by users).
82 # For example slice measurements will not be requested by default...
84 # Create the base layout (Stack)...
87 title="Slice !!view for %s"%slicename,
91 # ... responsible for the slice properties...
95 Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename)
99 Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
104 title="Slice view for %s"%slicename,
110 # ... and for the relations
111 # XXX Let's hardcode resources for now
112 sq_resource = aq.subquery('resource')
113 sq_user = aq.subquery('user')
114 sq_lease = aq.subquery('lease')
115 sq_measurement = aq.subquery('measurement')
118 ############################################################################
121 # A stack inserted in the subquery tab that will hold all operations
122 # related to resources
125 stack_resources = Stack(
131 resource_query_editor = QueryEditor(
135 stack_resources.insert(resource_query_editor)
137 # --------------------------------------------------------------------------
138 # Different displays = DataTables + GoogleMaps
140 tab_resource_plugins = Tabs(
145 tab_resource_plugins.insert(Hazelnut(
148 domid = 'checkboxes',
149 # this is the query at the core of the slice list
151 query_all_uuid = query_resource_all.query_uuid,
153 datatables_options = {
154 # for now we turn off sorting on the checkboxes columns this way
155 # this of course should be automatic in hazelnut
156 'aoColumns' : [None, None, None, None, {'bSortable': False}],
157 'iDisplayLength' : 25,
158 'bLengthChange' : True,
162 tab_resource_plugins.insert(GoogleMap(
164 title = 'Geographic view',
166 # tab's sons preferably turn this off
169 query_all_uuid = query_resource_all.query_uuid,
177 stack_resources.insert(tab_resource_plugins)
179 # --------------------------------------------------------------------------
182 stack_resources.insert(ResourcesSelected(
184 title = 'Pending operations',
185 resource_query_uuid = sq_resource,
186 lease_query_uuid = sq_lease,
190 sq_plugin.insert(stack_resources)
192 ############################################################################
200 # activeid = 'checkboxes',
201 active_domid = 'checkboxes2',
203 sq_plugin.insert(tab_users)
205 tab_users.insert(Hazelnut(
208 domid = 'checkboxes2',
209 # tab's sons preferably turn this off
211 # this is the query at the core of the slice list
214 datatables_options = {
215 # for now we turn off sorting on the checkboxes columns this way
216 # this of course should be automatic in hazelnut
217 'aoColumns' : [None, None, None, None, {'bSortable': False}],
218 'iDisplayLength' : 25,
219 'bLengthChange' : True,
223 tab_measurements = Tabs (
225 title = 'Measurements',
227 # activeid = 'checkboxes',
228 active_domid = 'checkboxes3',
230 sq_plugin.insert(tab_measurements)
232 tab_measurements.insert(Hazelnut(
235 domid = 'checkboxes3',
236 # tab's sons preferably turn this off
238 # this is the query at the core of the slice list
239 query = sq_measurement,
241 datatables_options = {
242 # for now we turn off sorting on the checkboxes columns this way
243 # this of course should be automatic in hazelnut
244 'aoColumns' : [None, None, None, None, {'bSortable': False}],
245 'iDisplayLength' : 25,
246 'bLengthChange' : True,
250 main_plugin.insert(sq_plugin)
252 main_plugin.insert(Messages(
254 title = "Runtime messages for slice %s"%slicename,
258 main_plugin.insert(Updater(
260 title = "wont show up as non togglable by default",
262 label = "Update slice",
267 # variables that will get passed to the view-unfold1.html template
270 # define 'unfold1_main' to the template engine - the main contents
271 template_env [ 'unfold1_main' ] = main_plugin.render(request)
273 # more general variables expected in the template
274 template_env [ 'title' ] = 'Test view that combines various plugins'
275 # the menu items on the top
276 template_env [ 'topmenu_items' ] = topmenu_items('Slice', request)
277 # so we can sho who is logged
278 template_env [ 'username' ] = the_user (request)
280 # don't forget to run the requests
281 page.expose_queries ()
283 # xxx create another plugin with the same query and a different layout (with_datatables)
284 # show that it worls as expected, one single api call to backend and 2 refreshed views
286 # the prelude object in page contains a summary of the requirements() for all plugins
287 # define {js,css}_{files,chunks}
288 prelude_env = page.prelude_env()
289 template_env.update(prelude_env)
290 result=render_to_response ('view-unfold1.html',template_env,
291 context_instance=RequestContext(request))