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 import Hazelnut
19 from plugins.resources_selected import ResourcesSelected
20 from plugins.googlemaps import GoogleMaps
21 from plugins.senslabmap.senslabmap import SensLabMap
22 from plugins.querycode.querycode import QueryCode
23 from plugins.query_editor import QueryEditor
24 from plugins.active_filters import ActiveFilters
25 from plugins.quickfilter.quickfilter import QuickFilter
26 from plugins.messages.messages import Messages
27 #from plugins.updater import Updater
29 tmp_default_slice='ple.upmc.myslicedemo'
33 def slice_view (request, slicename=tmp_default_slice):
34 # xxx Thierry - ugly hack
35 # fetching metadata here might fail - e.g. with an expired session..
36 # let's catch this early on and log out our user if needed
37 # it should of course be handled in a more generic way
39 return _slice_view(request,slicename)
40 except ManifoldException, manifold_result:
41 # xxx needs a means to display this message to user...
42 from django.contrib.auth import logout
44 return HttpResponseRedirect ('/')
46 # xxx we need to sugarcoat this error message in some error template...
47 print "Unexpected exception",e
52 def _slice_view (request, slicename):
55 page.expose_js_metadata()
57 metadata = page.get_metadata()
58 resource_md = metadata.details_by_object('resource')
59 resource_fields = [column['name'] for column in resource_md['column']]
61 user_md = metadata.details_by_object('user')
62 user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
64 # TODO The query to run is embedded in the URL
65 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
68 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn',
71 #'application.measurement_point.counter'
74 query_resource_all = Query.get('resource').select(resource_fields)
75 query_user_all = Query.get('user').select(user_fields)
77 aq = AnalyzedQuery(main_query, metadata=metadata)
78 page.enqueue_query(main_query, analyzed_query=aq)
79 page.enqueue_query(query_resource_all)
80 page.enqueue_query(query_user_all)
82 # Prepare the display according to all metadata
83 # (some parts will be pending, others can be triggered by users).
85 # For example slice measurements will not be requested by default...
87 # Create the base layout (Stack)...
90 title="Slice !!view for %s"%slicename,
94 # ... responsible for the slice properties...
98 Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename)
102 Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
107 title="Slice view for %s"%slicename,
113 # ... and for the relations
114 # XXX Let's hardcode resources for now
115 sq_resource = aq.subquery('resource')
116 sq_user = aq.subquery('user')
117 sq_lease = aq.subquery('lease')
118 sq_measurement = aq.subquery('measurement')
121 ############################################################################
124 # A stack inserted in the subquery tab that will hold all operations
125 # related to resources
128 stack_resources = Stack(
134 resource_query_editor = QueryEditor(
138 stack_resources.insert(resource_query_editor)
140 resource_active_filters = ActiveFilters(
144 stack_resources.insert(resource_active_filters)
146 # --------------------------------------------------------------------------
147 # Different displays = DataTables + GoogleMaps
149 tab_resource_plugins = Tabs(
154 tab_resource_plugins.insert(Hazelnut(
157 domid = 'checkboxes',
158 # this is the query at the core of the slice list
160 query_all = query_resource_all,
162 datatables_options = {
163 # for now we turn off sorting on the checkboxes columns this way
164 # this of course should be automatic in hazelnut
165 'aoColumns' : [None, None, None, None, {'bSortable': False}],
166 'iDisplayLength' : 25,
167 'bLengthChange' : True,
171 tab_resource_plugins.insert(GoogleMaps(
173 title = 'Geographic view',
175 # tab's sons preferably turn this off
178 query_all = query_resource_all,
186 stack_resources.insert(tab_resource_plugins)
188 sq_plugin.insert(stack_resources)
190 ############################################################################
198 # activeid = 'checkboxes',
199 active_domid = 'checkboxes2',
201 sq_plugin.insert(tab_users)
203 tab_users.insert(Hazelnut(
206 domid = 'checkboxes2',
207 # tab's sons preferably turn this off
209 # this is the query at the core of the slice list
211 query_all = query_user_all,
213 datatables_options = {
214 # for now we turn off sorting on the checkboxes columns this way
215 # this of course should be automatic in hazelnut
216 'aoColumns' : [None, None, None, None, {'bSortable': False}],
217 'iDisplayLength' : 25,
218 'bLengthChange' : True,
222 tab_measurements = Tabs (
224 title = 'Measurements',
226 # activeid = 'checkboxes',
227 active_domid = 'checkboxes3',
229 sq_plugin.insert(tab_measurements)
231 tab_measurements.insert(Hazelnut(
234 domid = 'checkboxes3',
235 # tab's sons preferably turn this off
237 # this is the query at the core of the slice list
238 query = sq_measurement,
240 datatables_options = {
241 # for now we turn off sorting on the checkboxes columns this way
242 # this of course should be automatic in hazelnut
243 'aoColumns' : [None, None, None, None, {'bSortable': False}],
244 'iDisplayLength' : 25,
245 'bLengthChange' : True,
249 main_plugin.insert(sq_plugin)
251 # --------------------------------------------------------------------------
254 main_plugin.insert(ResourcesSelected(
256 title = 'Pending operations',
261 main_plugin.insert(Messages(
263 title = "Runtime messages for slice %s"%slicename,
267 # main_plugin.insert(Updater(
269 # title = "wont show up as non togglable by default",
270 # query = main_query,
271 # label = "Update slice",
276 # variables that will get passed to the view-unfold1.html template
279 # define 'unfold1_main' to the template engine - the main contents
280 template_env [ 'unfold1_main' ] = main_plugin.render(request)
282 # more general variables expected in the template
283 template_env [ 'title' ] = 'Test view that combines various plugins'
284 # the menu items on the top
285 template_env [ 'topmenu_items' ] = topmenu_items('Slice', request)
286 # so we can sho who is logged
287 template_env [ 'username' ] = the_user (request)
289 # don't forget to run the requests
290 page.expose_queries ()
292 # xxx create another plugin with the same query and a different layout (with_datatables)
293 # show that it worls as expected, one single api call to backend and 2 refreshed views
295 # the prelude object in page contains a summary of the requirements() for all plugins
296 # define {js,css}_{files,chunks}
297 prelude_env = page.prelude_env()
298 template_env.update(prelude_env)
299 result=render_to_response ('view-unfold1.html',template_env,
300 context_instance=RequestContext(request))