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