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