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.loginrequired import LoginRequiredAutoLogoutView
10 from unfold.page import Page
11 from manifold.core.query import Query, AnalyzedQuery
12 from manifold.manifoldresult import ManifoldException
13 from manifold.metadata import MetaData as Metadata
14 #from myslice.viewutils import quickfilter_criterias, topmenu_items, the_user
15 from myslice.viewutils import topmenu_items, the_user
17 from plugins.raw import Raw
18 from plugins.stack import Stack
19 from plugins.tabs.tabs import Tabs
20 from plugins.lists.slicelist import SliceList
21 from plugins.hazelnut import Hazelnut
22 from plugins.resources_selected import ResourcesSelected
23 from plugins.googlemap import GoogleMap
24 from plugins.senslabmap.senslabmap import SensLabMap
25 from plugins.querycode import QueryCode
26 from plugins.query_editor import QueryEditor
27 from plugins.active_filters import ActiveFilters
28 from plugins.quickfilter import QuickFilter
29 from plugins.messages import Messages
30 #from plugins.updater import Updater
32 tmp_default_slice='ple.upmc.myslicedemo'
35 class SliceView (LoginRequiredAutoLogoutView):
37 def get (self,request, slicename=tmp_default_slice):
39 page.add_css_files ('css/slice-view.css')
40 page.expose_js_metadata()
42 metadata = page.get_metadata()
43 resource_md = metadata.details_by_object('resource')
44 resource_fields = [column['name'] for column in resource_md['column']]
46 user_md = metadata.details_by_object('user')
47 user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
49 # TODO The query to run is embedded in the URL
50 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
53 'resource.hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn',
56 #'application.measurement_point.counter'
59 query_resource_all = Query.get('resource').select(resource_fields)
60 query_user_all = Query.get('user').select(user_fields)
62 # Temporarily filter users which is slow
63 query_user_all = query_user_all.filter_by('authority_hrn', '==', 'ple.upmc')
65 aq = AnalyzedQuery(main_query, metadata=metadata)
66 page.enqueue_query(main_query, analyzed_query=aq)
67 page.enqueue_query(query_resource_all)
68 page.enqueue_query(query_user_all)
70 # Prepare the display according to all metadata
71 # (some parts will be pending, others can be triggered by users).
73 # For example slice measurements will not be requested by default...
75 # Create the base layout (Stack)...
78 title="Slice %s"%slicename,
82 # ... responsible for the slice properties...
86 Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename)
90 Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
95 title="Slice view for %s"%slicename,
101 # ... and for the relations
102 # XXX Let's hardcode resources for now
103 sq_resource = aq.subquery('resource')
104 sq_user = aq.subquery('user')
105 sq_lease = aq.subquery('lease')
106 sq_measurement = aq.subquery('measurement')
109 ############################################################################
112 # A stack inserted in the subquery tab that will hold all operations
113 # related to resources
116 stack_resources = Stack(
122 resource_query_editor = QueryEditor(
126 stack_resources.insert(resource_query_editor)
128 resource_active_filters = ActiveFilters(
132 stack_resources.insert(resource_active_filters)
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(SensLabMap(
161 title = 'SensLab Map',
162 domid = 'senslabmap',
163 query = sq_resource, # query slice resources
164 query_all = query_resource_all # query all resources
167 tab_resource_plugins.insert(GoogleMap(
169 title = 'Geographic view',
171 # tab's sons preferably turn this off
174 query_all = query_resource_all,
182 stack_resources.insert(tab_resource_plugins)
184 sq_plugin.insert(stack_resources)
186 ############################################################################
194 # activeid = 'checkboxes',
195 active_domid = 'checkboxes2',
197 sq_plugin.insert(tab_users)
199 tab_users.insert(Hazelnut(
202 domid = 'checkboxes2',
203 # tab's sons preferably turn this off
205 # this is the query at the core of the slice list
207 query_all = query_user_all,
209 datatables_options = {
210 # for now we turn off sorting on the checkboxes columns this way
211 # this of course should be automatic in hazelnut
212 'aoColumns' : [None, None, None, None, {'bSortable': False}],
213 'iDisplayLength' : 25,
214 'bLengthChange' : True,
218 tab_measurements = Tabs (
220 title = 'Measurements',
222 # activeid = 'checkboxes',
223 active_domid = 'checkboxes3',
225 sq_plugin.insert(tab_measurements)
227 tab_measurements.insert(Hazelnut(
230 domid = 'checkboxes3',
231 # tab's sons preferably turn this off
233 # this is the query at the core of the slice list
234 query = sq_measurement,
236 datatables_options = {
237 # for now we turn off sorting on the checkboxes columns this way
238 # this of course should be automatic in hazelnut
239 'aoColumns' : [None, None, None, None, {'bSortable': False}],
240 'iDisplayLength' : 25,
241 'bLengthChange' : True,
245 main_stack.insert(sq_plugin)
247 # --------------------------------------------------------------------------
250 main_stack.insert(ResourcesSelected(
252 title = 'Pending operations',
257 main_stack.insert(Messages(
259 title = "Runtime messages for slice %s"%slicename,
263 # main_stack.insert(Updater(
265 # title = "wont show up as non togglable by default",
266 # query = main_query,
267 # label = "Update slice",
272 # variables that will get passed to the view-unfold1.html template
275 # define 'unfold1_main' to the template engine - the main contents
276 template_env [ 'unfold1_main' ] = main_stack.render(request)
278 # more general variables expected in the template
279 template_env [ 'title' ] = 'Test view that combines various plugins'
280 # the menu items on the top
281 template_env [ 'topmenu_items' ] = topmenu_items('Slice', request)
282 # so we can sho who is logged
283 template_env [ 'username' ] = the_user (request)
285 # don't forget to run the requests
286 page.expose_queries ()
288 # xxx create another plugin with the same query and a different layout (with_datatables)
289 # show that it worls as expected, one single api call to backend and 2 refreshed views
291 # the prelude object in page contains a summary of the requirements() for all plugins
292 # define {js,css}_{files,chunks}
293 prelude_env = page.prelude_env()
294 template_env.update(prelude_env)
295 result=render_to_response ('view-unfold1.html',template_env,
296 context_instance=RequestContext(request))