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 # need to remove this dep.
13 from trash.trashutils import quickfilter_criterias
14 from myslice.viewutils import topmenu_items, the_user
16 from plugins.raw.raw import Raw
17 from plugins.stack.stack import Stack
18 from plugins.tabs.tabs import Tabs
19 from plugins.lists.slicelist import SliceList
20 from plugins.hazelnut import Hazelnut
21 from plugins.resources_selected import ResourcesSelected
22 from plugins.googlemaps import GoogleMaps
23 from plugins.senslabmap.senslabmap import SensLabMap
24 from plugins.querycode.querycode import QueryCode
25 from plugins.query_editor import QueryEditor
26 from plugins.active_filters import ActiveFilters
27 from plugins.quickfilter.quickfilter import QuickFilter
28 from plugins.messages.messages import Messages
29 #from plugins.updater import Updater
31 tmp_default_slice='ple.upmc.myslicedemo'
35 def slice_view (request, slicename=tmp_default_slice):
36 # xxx Thierry - ugly hack
37 # fetching metadata here might fail - e.g. with an expired session..
38 # let's catch this early on and log out our user if needed
39 # it should of course be handled in a more generic way
41 return _slice_view(request,slicename)
42 except ManifoldException, manifold_result:
43 # xxx needs a means to display this message to user...
44 from django.contrib.auth import logout
46 return HttpResponseRedirect ('/')
48 # xxx we need to sugarcoat this error message in some error template...
49 print "Unexpected exception",e
54 def _slice_view (request, slicename):
57 page.expose_js_metadata()
59 metadata = page.get_metadata()
60 resource_md = metadata.details_by_object('resource')
61 resource_fields = [column['name'] for column in resource_md['column']]
63 user_md = metadata.details_by_object('user')
64 user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
66 # TODO The query to run is embedded in the URL
67 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
70 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn',
73 #'application.measurement_point.counter'
76 query_resource_all = Query.get('resource').select(resource_fields)
77 query_user_all = Query.get('user').select(user_fields)
79 aq = AnalyzedQuery(main_query, metadata=metadata)
80 page.enqueue_query(main_query, analyzed_query=aq)
81 page.enqueue_query(query_resource_all)
82 page.enqueue_query(query_user_all)
84 # Prepare the display according to all metadata
85 # (some parts will be pending, others can be triggered by users).
87 # For example slice measurements will not be requested by default...
89 # Create the base layout (Stack)...
92 title="Slice !!view for %s"%slicename,
96 # ... responsible for the slice properties...
100 Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename)
104 Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
109 title="Slice view for %s"%slicename,
115 # ... and for the relations
116 # XXX Let's hardcode resources for now
117 sq_resource = aq.subquery('resource')
118 sq_user = aq.subquery('user')
119 sq_lease = aq.subquery('lease')
120 sq_measurement = aq.subquery('measurement')
123 ############################################################################
126 # A stack inserted in the subquery tab that will hold all operations
127 # related to resources
130 stack_resources = Stack(
136 resource_query_editor = QueryEditor(
140 stack_resources.insert(resource_query_editor)
142 resource_active_filters = ActiveFilters(
146 stack_resources.insert(resource_active_filters)
148 # --------------------------------------------------------------------------
149 # Different displays = DataTables + GoogleMaps
151 tab_resource_plugins = Tabs(
156 tab_resource_plugins.insert(Hazelnut(
159 domid = 'checkboxes',
160 # this is the query at the core of the slice list
162 query_all = query_resource_all,
164 datatables_options = {
165 # for now we turn off sorting on the checkboxes columns this way
166 # this of course should be automatic in hazelnut
167 'aoColumns' : [None, None, None, None, {'bSortable': False}],
168 'iDisplayLength' : 25,
169 'bLengthChange' : True,
173 tab_resource_plugins.insert(GoogleMaps(
175 title = 'Geographic view',
177 # tab's sons preferably turn this off
180 query_all = query_resource_all,
188 stack_resources.insert(tab_resource_plugins)
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
213 query_all = query_user_all,
215 datatables_options = {
216 # for now we turn off sorting on the checkboxes columns this way
217 # this of course should be automatic in hazelnut
218 'aoColumns' : [None, None, None, None, {'bSortable': False}],
219 'iDisplayLength' : 25,
220 'bLengthChange' : True,
224 tab_measurements = Tabs (
226 title = 'Measurements',
228 # activeid = 'checkboxes',
229 active_domid = 'checkboxes3',
231 sq_plugin.insert(tab_measurements)
233 tab_measurements.insert(Hazelnut(
236 domid = 'checkboxes3',
237 # tab's sons preferably turn this off
239 # this is the query at the core of the slice list
240 query = sq_measurement,
242 datatables_options = {
243 # for now we turn off sorting on the checkboxes columns this way
244 # this of course should be automatic in hazelnut
245 'aoColumns' : [None, None, None, None, {'bSortable': False}],
246 'iDisplayLength' : 25,
247 'bLengthChange' : True,
251 main_plugin.insert(sq_plugin)
253 # --------------------------------------------------------------------------
256 main_plugin.insert(ResourcesSelected(
258 title = 'Pending operations',
263 main_plugin.insert(Messages(
265 title = "Runtime messages for slice %s"%slicename,
269 # main_plugin.insert(Updater(
271 # title = "wont show up as non togglable by default",
272 # query = main_query,
273 # label = "Update slice",
278 # variables that will get passed to the view-unfold1.html template
281 # define 'unfold1_main' to the template engine - the main contents
282 template_env [ 'unfold1_main' ] = main_plugin.render(request)
284 # more general variables expected in the template
285 template_env [ 'title' ] = '%(slicename)s (test view that combines various plugins)'%locals()
286 # the menu items on the top
287 template_env [ 'topmenu_items' ] = topmenu_items('Slice', request)
288 # so we can sho who is logged
289 template_env [ 'username' ] = the_user (request)
291 # don't forget to run the requests
292 page.expose_queries ()
294 # xxx create another plugin with the same query and a different layout (with_datatables)
295 # show that it worls as expected, one single api call to backend and 2 refreshed views
297 # the prelude object in page contains a summary of the requirements() for all plugins
298 # define {js,css}_{files,chunks}
299 prelude_env = page.prelude_env()
300 template_env.update(prelude_env)
301 result=render_to_response ('view-unfold1.html',template_env,
302 context_instance=RequestContext(request))