move over imports for logout_on_manifold_exception
[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 from django.contrib.auth.decorators  import login_required
6
7 from unfold.page                     import Page
8 from manifold.core.query             import Query, AnalyzedQuery
9 from manifold.manifoldresult         import ManifoldException
10 from manifold.metadata               import MetaData as Metadata
11 # need to remove this dep.
12 from trash.trashutils                import quickfilter_criterias
13 from myslice.viewutils               import topmenu_items, the_user, logout_on_manifold_exception
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 @logout_on_manifold_exception
34 @login_required
35 def slice_view (request, slicename=tmp_default_slice):
36
37     page = Page(request)
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     query_user_all = Query.get('user').select(user_fields)
59
60     aq = AnalyzedQuery(main_query, metadata=metadata)
61     page.enqueue_query(main_query, analyzed_query=aq)
62     page.enqueue_query(query_resource_all)
63     page.enqueue_query(query_user_all)
64
65     # Prepare the display according to all metadata
66     # (some parts will be pending, others can be triggered by users).
67     # 
68     # For example slice measurements will not be requested by default...
69
70     # Create the base layout (Stack)...
71     main_plugin = Stack (
72         page=page,
73         title="Slice !!view for %s"%slicename,
74         sons=[],
75     )
76
77     # ... responsible for the slice properties...
78
79
80     main_plugin.insert (
81         Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename)
82     )
83
84     main_plugin.insert(
85         Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
86     )
87
88     sq_plugin = Tabs (
89         page=page,
90         title="Slice view for %s"%slicename,
91         togglable=False,
92         sons=[],
93     )
94
95
96     # ... and for the relations
97     # XXX Let's hardcode resources for now
98     sq_resource = aq.subquery('resource')
99     sq_user     = aq.subquery('user')
100     sq_lease    = aq.subquery('lease')
101     sq_measurement = aq.subquery('measurement')
102     
103
104     ############################################################################
105     # RESOURCES
106     # 
107     # A stack inserted in the subquery tab that will hold all operations
108     # related to resources
109     # 
110     
111     stack_resources = Stack(
112         page = page,
113         title        = 'Resources',
114         sons=[],
115     )
116
117     resource_query_editor = QueryEditor(
118         page  = page,
119         query = sq_resource,
120     )
121     stack_resources.insert(resource_query_editor)
122
123     resource_active_filters = ActiveFilters(
124         page  = page,
125         query = sq_resource,
126     )
127     stack_resources.insert(resource_active_filters)
128
129     # --------------------------------------------------------------------------
130     # Different displays = DataTables + GoogleMaps
131     #
132     tab_resource_plugins = Tabs(
133         page    = page,
134         sons = []
135     )
136
137     tab_resource_plugins.insert(Hazelnut( 
138         page       = page,
139         title      = 'List',
140         domid      = 'checkboxes',
141         # this is the query at the core of the slice list
142         query      = sq_resource,
143         query_all  = query_resource_all,
144         checkboxes = True,
145         datatables_options = { 
146             # for now we turn off sorting on the checkboxes columns this way
147             # this of course should be automatic in hazelnut
148             'aoColumns'      : [None, None, None, None, {'bSortable': False}],
149             'iDisplayLength' : 25,
150             'bLengthChange'  : True,
151         },
152     ))
153
154     tab_resource_plugins.insert(GoogleMaps(
155         page       = page,
156         title      = 'Geographic view',
157         domid      = 'gmap',
158         # tab's sons preferably turn this off
159         togglable  = False,
160         query      = sq_resource,
161         query_all  = query_resource_all,
162         checkboxes = True,
163         # center on Paris
164         latitude   = 49.,
165         longitude  = 2.2,
166         zoom       = 3,
167     ))
168
169     stack_resources.insert(tab_resource_plugins)
170
171     sq_plugin.insert(stack_resources)
172
173     ############################################################################
174     # USERS
175     # 
176
177     tab_users = Tabs(
178         page         = page,
179         title        = 'Users',
180         domid        = 'thetabs2',
181         # activeid   = 'checkboxes',
182         active_domid = 'checkboxes2',
183     )
184     sq_plugin.insert(tab_users)
185
186     tab_users.insert(Hazelnut( 
187         page        = page,
188         title       = 'List',
189         domid       = 'checkboxes2',
190         # tab's sons preferably turn this off
191         togglable   = False,
192         # this is the query at the core of the slice list
193         query       = sq_user,
194         query_all  = query_user_all,
195         checkboxes  = True,
196         datatables_options = { 
197             # for now we turn off sorting on the checkboxes columns this way
198             # this of course should be automatic in hazelnut
199             'aoColumns'      : [None, None, None, None, {'bSortable': False}],
200             'iDisplayLength' : 25,
201             'bLengthChange'  : True,
202         },
203     ))
204
205     tab_measurements = Tabs (
206         page         = page,
207         title        = 'Measurements',
208         domid        = 'thetabs3',
209         # activeid   = 'checkboxes',
210         active_domid = 'checkboxes3',
211     )
212     sq_plugin.insert(tab_measurements)
213
214     tab_measurements.insert(Hazelnut( 
215         page        = page,
216         title       = 'List',
217         domid       = 'checkboxes3',
218         # tab's sons preferably turn this off
219         togglable   = False,
220         # this is the query at the core of the slice list
221         query       = sq_measurement,
222         checkboxes  = True,
223         datatables_options = { 
224             # for now we turn off sorting on the checkboxes columns this way
225             # this of course should be automatic in hazelnut
226             'aoColumns'      : [None, None, None, None, {'bSortable': False}],
227             'iDisplayLength' : 25,
228             'bLengthChange'  : True,
229         },
230     ))
231
232     main_plugin.insert(sq_plugin)
233
234     # --------------------------------------------------------------------------
235     # ResourcesSelected
236     #
237     main_plugin.insert(ResourcesSelected(
238         page                = page,
239         title               = 'Pending operations',
240         query               = main_query,
241         togglable           = True,
242     ))
243
244     main_plugin.insert(Messages(
245         page   = page,
246         title  = "Runtime messages for slice %s"%slicename,
247         domid  = "msgs-pre",
248         levels = "ALL",
249     ))
250 #    main_plugin.insert(Updater(
251 #        page   = page,
252 #        title  = "wont show up as non togglable by default",
253 #        query  = main_query,
254 #        label  = "Update slice",
255 #    ))
256     
257
258
259     # variables that will get passed to the view-unfold1.html template
260     template_env = {}
261     
262     # define 'unfold1_main' to the template engine - the main contents
263     template_env [ 'unfold1_main' ] = main_plugin.render(request)
264
265     # more general variables expected in the template
266     template_env [ 'title' ] = '%(slicename)s'%locals()
267     # the menu items on the top
268     template_env [ 'topmenu_items' ] = topmenu_items('Slice', request) 
269     # so we can sho who is logged
270     template_env [ 'username' ] = the_user (request) 
271
272     # don't forget to run the requests
273     page.expose_queries ()
274
275     # xxx create another plugin with the same query and a different layout (with_datatables)
276     # show that it worls as expected, one single api call to backend and 2 refreshed views
277
278     # the prelude object in page contains a summary of the requirements() for all plugins
279     # define {js,css}_{files,chunks}
280     prelude_env = page.prelude_env()
281     template_env.update(prelude_env)
282     result=render_to_response ('view-unfold1.html',template_env,
283                                context_instance=RequestContext(request))
284     return result