whoops, properly restores users area
[myslice.git] / portal / sliceview.py
1 from django.template                 import RequestContext
2 from django.shortcuts                import render_to_response
3
4 from portal.templateviews            import LoginRequiredAutoLogoutView
5
6 from unfold.page                     import Page
7 from manifold.core.query             import Query, AnalyzedQuery
8
9 from myslice.viewutils               import topmenu_items, the_user
10
11 from plugins.raw.raw                 import Raw
12 from plugins.stack.stack             import Stack
13 from plugins.tabs.tabs               import Tabs
14 from plugins.hazelnut                import Hazelnut 
15 from plugins.resources_selected      import ResourcesSelected
16 from plugins.googlemaps              import GoogleMaps
17 from plugins.senslabmap.senslabmap   import SensLabMap
18 from plugins.querycode.querycode     import QueryCode
19 from plugins.query_editor            import QueryEditor
20 from plugins.active_filters          import ActiveFilters
21 from plugins.quickfilter.quickfilter import QuickFilter
22 from plugins.messages.messages       import Messages
23 #from plugins.updater                 import Updater
24
25 tmp_default_slice='ple.upmc.myslicedemo'
26
27 # temporary : turn off the users part to speed things up
28 do_query_users=True
29
30 class SliceView (LoginRequiredAutoLogoutView):
31
32     def get (self,request, slicename=tmp_default_slice):
33     
34         page = Page(request)
35         page.add_css_files ('css/slice-view.css')
36         page.add_js_chunks ('$(function() { console.log("sliceview: jQuery version " + $.fn.jquery); });')
37         page.add_js_chunks ('$(function() { console.log("users turned %s"); });'%"on" if do_query_users else "off")
38         page.expose_js_metadata()
39     
40         metadata = page.get_metadata()
41         resource_md = metadata.details_by_object('resource')
42         resource_fields = [column['name'] for column in resource_md['column']]
43     
44         user_md = metadata.details_by_object('user')
45         user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
46     
47         # TODO The query to run is embedded in the URL
48         main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
49         main_query.select(
50                 'slice_hrn',
51                 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn',
52                 #'lease.urn',
53                 'user.user_hrn',
54                 #'application.measurement_point.counter'
55         )
56     
57         query_resource_all = Query.get('resource').select(resource_fields)
58         if do_query_users:
59             query_user_all = Query.get('user').select(user_fields)
60     
61         aq = AnalyzedQuery(main_query, metadata=metadata)
62         page.enqueue_query(main_query, analyzed_query=aq)
63         page.enqueue_query(query_resource_all)
64         if do_query_users:
65             page.enqueue_query(query_user_all)
66     
67         # ... and for the relations
68         # XXX Let's hardcode resources for now
69         sq_resource    = aq.subquery('resource')
70         sq_user        = aq.subquery('user')
71         sq_lease       = aq.subquery('lease')
72         sq_measurement = aq.subquery('measurement')
73         
74     
75         # Prepare the display according to all metadata
76         # (some parts will be pending, others can be triggered by users).
77         # 
78         # For example slice measurements will not be requested by default...
79     
80         # Create the base layout (Stack)...
81         main_stack = Stack (
82             page=page,
83             title="Slice %s"%slicename,
84             sons=[],
85         )
86     
87         # ... responsible for the slice properties...
88     
89         # a nice header
90         main_stack.insert (
91             Raw (page=page,
92                  togglable=False, 
93                  toggled=True,
94                  html="<h2 class='well well-lg'> Slice %s</h2>"%slicename)
95         )
96     
97         # --------------------------------------------------------------------------
98         # ResourcesSelected
99         #
100         main_stack.insert(ResourcesSelected(
101             page                = page,
102             title               = 'Pending operations',
103             query               = main_query,
104             togglable           = True,
105             domid               = 'pending',
106             outline_complete    = True,
107         ))
108     
109 #        main_stack.insert(
110 #            Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
111 #        )
112     
113         # the resources part is made of a Stack, 
114         # with first a Tabs (List, Geographic), 
115         # and second the QueryEditor to tweak the set of attributes to show
116         resources_as_list = Hazelnut( 
117             page       = page,
118             title      = 'Resources as a List',
119             domid      = 'checkboxes',
120             # this is the query at the core of the slice list
121             query      = sq_resource,
122             query_all  = query_resource_all,
123             checkboxes = True,
124             datatables_options = { 
125                 # for now we turn off sorting on the checkboxes columns this way
126                 # this of course should be automatic in hazelnut
127                 'aoColumns'      : [None, None, None, None, {'bSortable': False}],
128                 'iDisplayLength' : 25,
129                 'bLengthChange'  : True,
130                 },
131             )
132
133         resources_as_map = GoogleMaps(
134             page       = page,
135             title      = 'Geographic view',
136             domid      = 'gmap',
137             # tab's sons preferably turn this off
138             togglable  = False,
139             query      = sq_resource,
140             query_all  = query_resource_all,
141             checkboxes = True,
142             # center on Paris
143             latitude   = 49.,
144             longitude  = 2.2,
145             zoom       = 3,
146         )
147
148         resources_query_editor = QueryEditor(
149             page  = page,
150             query = sq_resource,
151             )
152         resources_active_filters = ActiveFilters(
153             page  = page,
154             query = sq_resource,
155             )
156
157         resources_area = Stack (
158             page=page,
159             domid="resources",
160             togglable=True,
161             title="Resources",
162             outline_complete=True,
163             sons = [
164                 Tabs ( page=page, 
165                        sons=[ resources_as_list, resources_as_map, ] 
166                        ),
167                 Stack ( page=page,
168                         title="Customize",
169                         togglable=True,
170                         sons = [ resources_query_editor, resources_active_filters, ]
171                         ),
172                 ]
173             )
174
175         main_stack.insert (resources_area)
176
177
178 #        sq_plugin = Tabs (
179 #            page=page,
180 #            title="Slice view for %s"%slicename,
181 #            togglable=True,
182 #            sons=[],
183 #        )
184     
185     
186         ############################################################################
187         # RESOURCES
188         # 
189         # A stack inserted in the subquery tab that will hold all operations
190         # related to resources
191         # 
192         
193 #        stack_resources = Stack(
194 #            page = page,
195 #            title = 'Resources',
196 #            sons=[],
197 #        )
198     
199 #        stack_resources.insert(resource_active_filters)
200 #    
201 #    
202 #        stack_resources.insert(tab_resource_plugins)
203     
204 #        sq_plugin.insert(stack_resources)
205     
206         ############################################################################
207         # USERS
208         # 
209     
210         if do_query_users:
211             tab_users = Tabs(
212                 page                = page,
213                 domid               = 'users',
214                 outline_complete    = True,
215                 togglable           = True,
216                 title               = 'Users',
217                 active_domid        = 'checkboxes2',
218                 )
219             main_stack.insert(tab_users)
220     
221             tab_users.insert(Hazelnut( 
222                 page        = page,
223                 title       = 'Users List',
224                 domid       = 'checkboxes2',
225                 # tab's sons preferably turn this off
226                 togglable   = False,
227                 # this is the query at the core of the slice list
228                 query       = sq_user,
229                 query_all  = query_user_all,
230                 checkboxes  = True,
231                 datatables_options = { 
232                     # for now we turn off sorting on the checkboxes columns this way
233                     # this of course should be automatic in hazelnut
234                     'aoColumns'      : [None, None, None, None, {'bSortable': False}],
235                     'iDisplayLength' : 25,
236                     'bLengthChange'  : True,
237                 },
238             ))
239     
240         tab_measurements = Tabs (
241             page                = page,
242             active_domid        = 'checkboxes3',
243             outline_complete    = True,
244             togglable           = True,
245             title               = 'Measurements',
246             domid               = 'measurements',
247         )
248         main_stack.insert(tab_measurements)
249     
250         tab_measurements.insert(Hazelnut( 
251             page        = page,
252             title       = 'Measurements',
253             domid       = 'checkboxes3',
254             # tab's sons preferably turn this off
255             togglable   = False,
256             # this is the query at the core of the slice list
257             query       = sq_measurement,
258             checkboxes  = True,
259             datatables_options = { 
260                 # for now we turn off sorting on the checkboxes columns this way
261                 # this of course should be automatic in hazelnut
262                 'aoColumns'      : [None, None, None, None, {'bSortable': False}],
263                 'iDisplayLength' : 25,
264                 'bLengthChange'  : True,
265             },
266         ))
267     
268         main_stack.insert(Messages(
269             page   = page,
270             title  = "Runtime messages for slice %s"%slicename,
271             domid  = "msgs-pre",
272             levels = "ALL",
273             # plain messages are probably less nice for production but more reliable for development for now
274             transient = False,
275             # these make sense only in non-transient mode..
276             togglable = True,
277             toggled = 'persistent',
278             outline_complete = True,
279         ))
280     
281     
282         # variables that will get passed to the view-unfold1.html template
283         template_env = {}
284         
285         # define 'unfold1_main' to the template engine - the main contents
286         template_env [ 'unfold1_main' ] = main_stack.render(request)
287     
288         # more general variables expected in the template
289         template_env [ 'title' ] = '%(slicename)s'%locals()
290         # the menu items on the top
291         template_env [ 'topmenu_items' ] = topmenu_items('Slice', request) 
292         # so we can sho who is logged
293         template_env [ 'username' ] = the_user (request) 
294     
295         # don't forget to run the requests
296         page.expose_queries ()
297     
298         # xxx create another plugin with the same query and a different layout (with_datatables)
299         # show that it worls as expected, one single api call to backend and 2 refreshed views
300     
301         # the prelude object in page contains a summary of the requirements() for all plugins
302         # define {js,css}_{files,chunks}
303         prelude_env = page.prelude_env()
304         template_env.update(prelude_env)
305         result=render_to_response ('view-unfold1.html',template_env,
306                                    context_instance=RequestContext(request))
307         return result