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