edf871c7add4210ff9a266bb14dc2d8afe290393
[myslice.git] / trash / 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 django.contrib.auth.decorators import login_required
7 from django.http import HttpResponseRedirect
8
9 from unfold.page import Page
10 #from manifold.manifoldquery import ManifoldQuery
11 from manifold.core.query import Query, AnalyzedQuery
12
13 from plugins.raw.raw import Raw
14 from plugins.stack.stack import Stack
15 from plugins.tabs.tabs import Tabs
16 from plugins.lists.slicelist import SliceList
17 from plugins.hazelnut.hazelnut import Hazelnut 
18 from plugins.googlemap.googlemap import GoogleMap 
19 from plugins.senslabmap.senslabmap import SensLabMap
20 from plugins.querycode.querycode import QueryCode
21 from plugins.quickfilter.quickfilter import QuickFilter
22 from plugins.messages.messages import Messages
23
24 from manifold.manifoldresult import ManifoldException
25
26 from myslice.viewutils import quickfilter_criterias
27 from myslice.viewutils import topmenu_items, the_user
28
29 # XXX JORDAN
30 from manifold.metadata import MetaData as Metadata
31
32 tmp_default_slice='ple.inria.heartbeat'
33 debug = True
34
35 @login_required
36 def slice_view (request, slicename=tmp_default_slice):
37     # xxx Thierry - ugly hack
38     # fetching metadata here might fail - e.g. with an expired session..
39     # let's catch this early on and log out our user if needed
40     # it should of course be handled in a more generic way
41     try:
42         return _slice_view(request,slicename)
43     except ManifoldException, manifold_result:
44         # xxx needs a means to display this message to user...
45         from django.contrib.auth import logout
46         logout(request)
47         return HttpResponseRedirect ('/')
48     except Exception, e:
49         # xxx we need to sugarcoat this error message in some error template...
50         print "Unexpected exception",e
51         # return ...
52
53 def _slice_view (request, slicename):
54
55     page = Page(request)
56     page.expose_js_metadata()
57
58     # TODO The query to run is embedded in the URL
59     main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
60
61     # Get default fields from metadata unless specified
62     if not main_query.fields:
63         metadata = page.get_metadata()
64         md_fields = metadata.details_by_object('slice')
65         if debug:
66             print "METADATA", md_fields
67         # TODO Get default fields
68         main_query.fields = [
69                 'slice_hrn',
70                 'resource.hrn', 'resource.hostname', 'resource.type', 'resource.authority',
71                 'user.user_hrn',
72 #                'application.measurement_point.counter'
73         ]
74
75     aq = AnalyzedQuery(main_query)
76     page.enqueue_query(main_query, analyzed_query=aq)
77
78     # Prepare the display according to all metadata
79     # (some parts will be pending, others can be triggered by users).
80     # 
81     # For example slice measurements will not be requested by default...
82
83     # Create the base layout (Stack)...
84     main_plugin = Stack (
85         page=page,
86         title="Slice view for %s"%slicename,
87         domid='thestack',
88         togglable=False,
89         sons=[],
90     )
91
92     # ... responsible for the slice properties...
93
94
95     main_plugin.insert (
96         Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename))
97
98     main_plugin.insert(
99         Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
100     )
101
102
103     # ... and for the relations
104     # XXX Let's hardcode resources for now
105     sq = aq.subquery('resource')
106     
107     tab_resources = Tabs (
108         page         = page,
109         title        = 'Resources',
110         domid        = 'thetabs',
111         # activeid   = 'checkboxes',
112         active_domid = 'gmap',
113     )
114     main_plugin.insert(tab_resources)
115
116     tab_resources.insert(
117         Hazelnut ( 
118             page        = page,
119             title       = 'List',
120             domid       = 'checkboxes',
121             # tab's sons preferably turn this off
122             togglable   = False,
123             # this is the query at the core of the slice list
124             query       = sq,
125             checkboxes  = True,
126             datatables_options = { 
127                 # for now we turn off sorting on the checkboxes columns this way
128                 # this of course should be automatic in hazelnut
129                 'aoColumns'      : [None, None, None, None, {'bSortable': False}],
130                 'iDisplayLength' : 25,
131                 'bLengthChange'  : True,
132             },
133         )
134     )
135     tab_resources.insert(
136         GoogleMap (
137             page        = page,
138             title       = 'Geographic view',
139             domid       = 'gmap',
140             # tab's sons preferably turn this off
141             togglable   = False,
142             query       = sq,
143             # center on Paris
144             latitude    = 49.,
145             longitude   = 2.2,
146             zoom        = 3,
147         )
148     )
149
150     # XXX Let's hardcode users also for now
151     sq = aq.subquery('user')
152     
153     tab_users = Tabs (
154         page         = page,
155         title        = 'Users',
156         domid        = 'thetabs2',
157         # activeid   = 'checkboxes',
158         active_domid = 'checkboxes2',
159     )
160     main_plugin.insert(tab_users)
161
162     tab_users.insert(
163         Hazelnut ( 
164             page        = page,
165             title       = 'List',
166             domid       = 'checkboxes2',
167             # tab's sons preferably turn this off
168             togglable   = False,
169             # this is the query at the core of the slice list
170             query       = sq,
171             checkboxes  = True,
172             datatables_options = { 
173                 # for now we turn off sorting on the checkboxes columns this way
174                 # this of course should be automatic in hazelnut
175                 'aoColumns'      : [None, None, None, None, {'bSortable': False}],
176                 'iDisplayLength' : 25,
177                 'bLengthChange'  : True,
178             },
179         )
180     )
181
182     # XXX Let's hardcode measurements also for now
183     sq = aq.subquery('measurement')
184     
185     tab_users = Tabs (
186         page         = page,
187         title        = 'Measurements',
188         domid        = 'thetabs3',
189         # activeid   = 'checkboxes',
190         active_domid = 'checkboxes3',
191     )
192     main_plugin.insert(tab_users)
193
194     tab_users.insert(
195         Hazelnut ( 
196             page        = page,
197             title       = 'List',
198             domid       = 'checkboxes3',
199             # tab's sons preferably turn this off
200             togglable   = False,
201             # this is the query at the core of the slice list
202             query       = sq,
203             checkboxes  = True,
204             datatables_options = { 
205                 # for now we turn off sorting on the checkboxes columns this way
206                 # this of course should be automatic in hazelnut
207                 'aoColumns'      : [None, None, None, None, {'bSortable': False}],
208                 'iDisplayLength' : 25,
209                 'bLengthChange'  : True,
210             },
211         )
212     )
213
214     main_plugin.insert (
215         Messages (
216             page=page,
217             title="Runtime messages for slice %s"%slicename,
218             domid="msgs-pre",
219             levels="ALL",
220             ))
221     
222
223     # END OF JORDAN's CODE
224
225 #old#    main_plugin = Stack (
226 #old#        page=page,
227 #old#        title="Slice view for %s"%slicename,
228 #old#        domid='thestack',
229 #old#        togglable=False,
230 #old#        sons=[
231 #old#            Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename),
232 #old#            Messages (
233 #old#                page=page,
234 #old#                title="Runtime messages for slice %s"%slicename,
235 #old#                domid="msgs-pre",
236 #old#                levels="ALL",
237 #old#                ),
238 #old#            Tabs (
239 #old#                page=page,
240 #old#                title="2 tabs : w/ and w/o checkboxes",
241 #old#                domid='thetabs',
242 #old#                # active_domid='checkboxes',
243 #old#                active_domid='gmap',
244 #old#                sons=[
245 #old#                    Hazelnut ( 
246 #old#                        page=page,
247 #old#                        title='a sample and simple hazelnut',
248 #old#                        domid='simple',
249 #old#                        # tab's sons preferably turn this off
250 #old#                        togglable=False,
251 #old#                        # this is the query at the core of the slice list
252 #old#                        query=main_query,
253 #old#                        ),
254 #old#                    Hazelnut ( 
255 #old#                        page=page,
256 #old#                        title='with checkboxes',
257 #old#                        domid='checkboxes',
258 #old#                        # tab's sons preferably turn this off
259 #old#                        togglable=False,
260 #old#                        # this is the query at the core of the slice list
261 #old#                        query=main_query,
262 #old#                        checkboxes=True,
263 #old#                        datatables_options = { 
264 #old#                            # for now we turn off sorting on the checkboxes columns this way
265 #old#                            # this of course should be automatic in hazelnut
266 #old#                            'aoColumns' : [ None, None, None, None, {'bSortable': False} ],
267 #old#                            'iDisplayLength' : 25,
268 #old#                            'bLengthChange' : True,
269 #old#                            },
270 #old#                        ),
271 #old#                    GoogleMap (
272 #old#                        page=page,
273 #old#                        title='geographic view',
274 #old#                        domid='gmap',
275 #old#                        # tab's sons preferably turn this off
276 #old#                        togglable=False,
277 #old#                        query=main_query,
278 #old#                        # center on Paris
279 #old#                        latitude=49.,
280 #old#                        longitude=2.2,
281 #old#                        zoom=3,
282 #old#                        ),
283 #old#                    Raw (
284 #old##                    SensLabMap (
285 #old#                        page=page,
286 #old#                        title='3D view (disabled)',
287 #old#                        domid='smap',
288 #old##                        # tab's sons preferably turn this off
289 #old#                        togglable=False,
290 #old##                        query=main_query,
291 #old#                        html="""<p class='well'>
292 #old#Thierry: I am commeting off the use of <button class="btn btn-danger">SensLabMap</button> which,
293 #old# although rudimentarily ported to the django framework, 
294 #old#causes a weird behaviour especially wrt scrolling. 
295 #old#On my Mac <button class="btn btn-warning"> I cannot use the mouse to scroll</button> any longer
296 #old#if I keep this active, so for now it's disabled
297 #old#</p>""",
298 #old#                        ),
299 #old#                    ]),
300 #old#            Hazelnut ( 
301 #old#                page=page,
302 #old#                title='a hazelnut not in tabs',
303 #old#                domid='standalone',
304 #old#                # this is the query at the core of the slice list
305 #old#                query=main_query,
306 #old#                columns=['hrn','hostname'],
307 #old#                ),
308 #old#              # you don't *have to* set a domid, but if you plan on using toggled=persistent then it's required
309 #old#              # because domid is the key for storing toggle status in the browser
310 #old#            QueryCode (
311 #old#                page=page,
312 #old#                title='xmlrpc code (toggled=False)',
313 #old#                query=main_query,
314 #old##                domid='xmlrpc',
315 #old#                toggled=False,
316 #old#                ),
317 #old#            QuickFilter (
318 #old#                page=page,
319 #old#                title="QuickFilter - requires metadata (toggled=False)",
320 #old#                criterias=quickfilter_criterias,
321 #old#                domid='filters',
322 #old#                toggled=False,
323 #old#                ),
324 #old#            Messages (
325 #old#                page=page,
326 #old#                title="Runtime messages (again)",
327 #old#                domid="msgs-post",
328 #old#                )
329 #old#              ])
330
331     # variables that will get passed to the view-unfold1.html template
332     template_env = {}
333     
334     # define 'unfold1_main' to the template engine - the main contents
335     template_env [ 'unfold1_main' ] = main_plugin.render(request)
336
337     # more general variables expected in the template
338     template_env [ 'title' ] = 'Test view that combines various plugins'
339     # the menu items on the top
340     template_env [ 'topmenu_items' ] = topmenu_items('slice', request) 
341     # so we can sho who is logged
342     template_env [ 'username' ] = the_user (request) 
343
344     # don't forget to run the requests
345     page.expose_queries ()
346
347     # xxx create another plugin with the same query and a different layout (with_datatables)
348     # show that it worls as expected, one single api call to backend and 2 refreshed views
349
350     # the prelude object in page contains a summary of the requirements() for all plugins
351     # define {js,css}_{files,chunks}
352     prelude_env = page.prelude_env()
353     template_env.update(prelude_env)
354     result=render_to_response ('view-unfold1.html',template_env,
355                                context_instance=RequestContext(request))
356     return result