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