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 # TODO The query to run is embedded in the URL
62 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
65 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn',
68 #'application.measurement_point.counter'
71 query_resource_all = Query.get('resource').select(resource_fields)
73 aq = AnalyzedQuery(main_query, metadata=metadata)
74 page.enqueue_query(main_query, analyzed_query=aq)
75 page.enqueue_query(query_resource_all)
77 # Prepare the display according to all metadata
78 # (some parts will be pending, others can be triggered by users).
80 # For example slice measurements will not be requested by default...
82 # Create the base layout (Stack)...
85 title="Slice !!view for %s"%slicename,
89 # ... responsible for the slice properties...
93 Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename)
97 Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
102 title="Slice view for %s"%slicename,
108 # ... and for the relations
109 # XXX Let's hardcode resources for now
110 sq_resource = aq.subquery('resource')
111 sq_user = aq.subquery('user')
112 sq_lease = aq.subquery('lease')
113 sq_measurement = aq.subquery('measurement')
116 ############################################################################
119 # A stack inserted in the subquery tab that will hold all operations
120 # related to resources
123 stack_resources = Stack(
129 resource_query_editor = QueryEditor(
133 stack_resources.insert(resource_query_editor)
135 resource_active_filters = ActiveFilters(
139 stack_resources.insert(resource_active_filters)
141 # --------------------------------------------------------------------------
142 # Different displays = DataTables + GoogleMaps
144 tab_resource_plugins = Tabs(
149 tab_resource_plugins.insert(Hazelnut(
152 domid = 'checkboxes',
153 # this is the query at the core of the slice list
155 query_all = query_resource_all,
157 datatables_options = {
158 # for now we turn off sorting on the checkboxes columns this way
159 # this of course should be automatic in hazelnut
160 'aoColumns' : [None, None, None, None, {'bSortable': False}],
161 'iDisplayLength' : 25,
162 'bLengthChange' : True,
166 tab_resource_plugins.insert(GoogleMaps(
168 title = 'Geographic view',
170 # tab's sons preferably turn this off
173 query_all = query_resource_all,
181 stack_resources.insert(tab_resource_plugins)
183 sq_plugin.insert(stack_resources)
185 ############################################################################
193 # activeid = 'checkboxes',
194 active_domid = 'checkboxes2',
196 sq_plugin.insert(tab_users)
198 tab_users.insert(Hazelnut(
201 domid = 'checkboxes2',
202 # tab's sons preferably turn this off
204 # this is the query at the core of the slice list
207 datatables_options = {
208 # for now we turn off sorting on the checkboxes columns this way
209 # this of course should be automatic in hazelnut
210 'aoColumns' : [None, None, None, None, {'bSortable': False}],
211 'iDisplayLength' : 25,
212 'bLengthChange' : True,
216 tab_measurements = Tabs (
218 title = 'Measurements',
220 # activeid = 'checkboxes',
221 active_domid = 'checkboxes3',
223 sq_plugin.insert(tab_measurements)
225 tab_measurements.insert(Hazelnut(
228 domid = 'checkboxes3',
229 # tab's sons preferably turn this off
231 # this is the query at the core of the slice list
232 query = sq_measurement,
234 datatables_options = {
235 # for now we turn off sorting on the checkboxes columns this way
236 # this of course should be automatic in hazelnut
237 'aoColumns' : [None, None, None, None, {'bSortable': False}],
238 'iDisplayLength' : 25,
239 'bLengthChange' : True,
243 main_plugin.insert(sq_plugin)
245 # --------------------------------------------------------------------------
248 main_plugin.insert(ResourcesSelected(
250 title = 'Pending operations',
255 main_plugin.insert(Messages(
257 title = "Runtime messages for slice %s"%slicename,
261 # main_plugin.insert(Updater(
263 # title = "wont show up as non togglable by default",
264 # query = main_query,
265 # label = "Update slice",
270 # variables that will get passed to the view-unfold1.html template
273 # define 'unfold1_main' to the template engine - the main contents
274 template_env [ 'unfold1_main' ] = main_plugin.render(request)
276 # more general variables expected in the template
277 template_env [ 'title' ] = 'Test view that combines various plugins'
278 # the menu items on the top
279 template_env [ 'topmenu_items' ] = topmenu_items('Slice', request)
280 # so we can sho who is logged
281 template_env [ 'username' ] = the_user (request)
283 # don't forget to run the requests
284 page.expose_queries ()
286 # xxx create another plugin with the same query and a different layout (with_datatables)
287 # show that it worls as expected, one single api call to backend and 2 refreshed views
289 # the prelude object in page contains a summary of the requirements() for all plugins
290 # define {js,css}_{files,chunks}
291 prelude_env = page.prelude_env()
292 template_env.update(prelude_env)
293 result=render_to_response ('view-unfold1.html',template_env,
294 context_instance=RequestContext(request))