ee030451c9568dad501c6f35580751f920945ae3
[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
38     # TODO The query to run is embedded in the URL
39     main_query = Query({'action': 'get', 'object': 'slice'}).filter_by('slice_hrn', '=', slicename)
40
41     # Get default fields from metadata unless specified
42     if not main_query.fields:
43         md_fields = page.get_metadata()
44         md_fields = md_fields.details_by_subject('slice')
45         if debug:
46             print "METADATA", md_fields
47         # TODO Get default fields
48         main_query.fields = ['slice_hrn', 'resource.hrn', 'resource.hostname', 'resource.type', 'resource.authority']
49
50 #old#    main_query = ManifoldQuery (action='get',
51 #old#                                subject='resource',
52 #old#                                timestamp='latest',
53 #old#                                fields=['network','type','hrn','hostname'],
54 #old#                                filters= [ [ 'slice_hrn', '=', slicename, ] ],
55 #old#                                )
56     page.enqueue_query(main_query)
57
58     # Prepare the display according to all metadata
59     # (some parts will be pending, others can be triggered by users).
60     # 
61     # For example slice measurements will not be requested by default...
62
63     # Create the base layout (Stack)...
64     main_plugin = Stack (
65         page=page,
66         title="Slice view for %s"%slicename,
67         domid='thestack',
68         togglable=False,
69         sons=[
70             Messages (
71                 page=page,
72                 title="Runtime messages for slice %s"%slicename,
73                 domid="msgs-pre",
74                 levels="ALL",
75                 ),
76             Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename),
77         ]
78     )
79
80     # ... responsible for the slice properties...
81
82     main_plugin.insert(
83         Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
84     )
85
86
87     # ... and for the relations
88     # XXX Let's hardcode resources for now
89     aq = AnalyzedQuery(main_query)
90     sq = aq.subquery('resource')
91     
92     tab_resources = Tabs (
93         page         = page,
94         title        = 'Resources',
95         domid        = 'thetabs',
96         # activeid   = 'checkboxes',
97         active_domid = 'gmap',
98     )
99     main_plugin.insert(tab_resources)
100
101     tab_resources.insert(
102         Hazelnut ( 
103             page        = page,
104             title       = 'List',
105             domid       = 'checkboxes',
106             # tab's sons preferably turn this off
107             togglable   = False,
108             # this is the query at the core of the slice list
109             query       = sq,
110             checkboxes  = True,
111             datatables_options = { 
112                 # for now we turn off sorting on the checkboxes columns this way
113                 # this of course should be automatic in hazelnut
114                 'aoColumns'      : [None, None, None, None, {'bSortable': False}],
115                 'iDisplayLength' : 25,
116                 'bLengthChange'  : True,
117             },
118         )
119     )
120     tab_resources.insert(
121         GoogleMap (
122             page        = page,
123             title       = 'Geographic view',
124             domid       = 'gmap',
125             # tab's sons preferably turn this off
126             togglable   = False,
127             query       = sq,
128             # center on Paris
129             latitude    = 49.,
130             longitude   = 2.2,
131             zoom        = 3,
132         )
133     )
134
135     # END OF JORDAN's CODE
136
137 #old#    main_plugin = Stack (
138 #old#        page=page,
139 #old#        title="Slice view for %s"%slicename,
140 #old#        domid='thestack',
141 #old#        togglable=False,
142 #old#        sons=[
143 #old#            Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename),
144 #old#            Messages (
145 #old#                page=page,
146 #old#                title="Runtime messages for slice %s"%slicename,
147 #old#                domid="msgs-pre",
148 #old#                levels="ALL",
149 #old#                ),
150 #old#            Tabs (
151 #old#                page=page,
152 #old#                title="2 tabs : w/ and w/o checkboxes",
153 #old#                domid='thetabs',
154 #old#                # active_domid='checkboxes',
155 #old#                active_domid='gmap',
156 #old#                sons=[
157 #old#                    Hazelnut ( 
158 #old#                        page=page,
159 #old#                        title='a sample and simple hazelnut',
160 #old#                        domid='simple',
161 #old#                        # tab's sons preferably turn this off
162 #old#                        togglable=False,
163 #old#                        # this is the query at the core of the slice list
164 #old#                        query=main_query,
165 #old#                        ),
166 #old#                    Hazelnut ( 
167 #old#                        page=page,
168 #old#                        title='with checkboxes',
169 #old#                        domid='checkboxes',
170 #old#                        # tab's sons preferably turn this off
171 #old#                        togglable=False,
172 #old#                        # this is the query at the core of the slice list
173 #old#                        query=main_query,
174 #old#                        checkboxes=True,
175 #old#                        datatables_options = { 
176 #old#                            # for now we turn off sorting on the checkboxes columns this way
177 #old#                            # this of course should be automatic in hazelnut
178 #old#                            'aoColumns' : [ None, None, None, None, {'bSortable': False} ],
179 #old#                            'iDisplayLength' : 25,
180 #old#                            'bLengthChange' : True,
181 #old#                            },
182 #old#                        ),
183 #old#                    GoogleMap (
184 #old#                        page=page,
185 #old#                        title='geographic view',
186 #old#                        domid='gmap',
187 #old#                        # tab's sons preferably turn this off
188 #old#                        togglable=False,
189 #old#                        query=main_query,
190 #old#                        # center on Paris
191 #old#                        latitude=49.,
192 #old#                        longitude=2.2,
193 #old#                        zoom=3,
194 #old#                        ),
195 #old#                    Raw (
196 #old##                    SensLabMap (
197 #old#                        page=page,
198 #old#                        title='3D view (disabled)',
199 #old#                        domid='smap',
200 #old##                        # tab's sons preferably turn this off
201 #old#                        togglable=False,
202 #old##                        query=main_query,
203 #old#                        html="""<p class='well'>
204 #old#Thierry: I am commeting off the use of <button class="btn btn-danger">SensLabMap</button> which,
205 #old# although rudimentarily ported to the django framework, 
206 #old#causes a weird behaviour especially wrt scrolling. 
207 #old#On my Mac <button class="btn btn-warning"> I cannot use the mouse to scroll</button> any longer
208 #old#if I keep this active, so for now it's disabled
209 #old#</p>""",
210 #old#                        ),
211 #old#                    ]),
212 #old#            Hazelnut ( 
213 #old#                page=page,
214 #old#                title='a hazelnut not in tabs',
215 #old#                domid='standalone',
216 #old#                # this is the query at the core of the slice list
217 #old#                query=main_query,
218 #old#                columns=['hrn','hostname'],
219 #old#                ),
220 #old#              # you don't *have to* set a domid, but if you plan on using toggled=persistent then it's required
221 #old#              # because domid is the key for storing toggle status in the browser
222 #old#            QueryCode (
223 #old#                page=page,
224 #old#                title='xmlrpc code (toggled=False)',
225 #old#                query=main_query,
226 #old##                domid='xmlrpc',
227 #old#                toggled=False,
228 #old#                ),
229 #old#            QuickFilter (
230 #old#                page=page,
231 #old#                title="QuickFilter - requires metadata (toggled=False)",
232 #old#                criterias=quickfilter_criterias,
233 #old#                domid='filters',
234 #old#                toggled=False,
235 #old#                ),
236 #old#            Messages (
237 #old#                page=page,
238 #old#                title="Runtime messages (again)",
239 #old#                domid="msgs-post",
240 #old#                )
241 #old#              ])
242
243     # variables that will get passed to the view-unfold1.html template
244     template_env = {}
245     
246     # define 'unfold1_main' to the template engine - the main contents
247     template_env [ 'unfold1_main' ] = main_plugin.render(request)
248
249     # more general variables expected in the template
250     template_env [ 'title' ] = 'Test view that combines various plugins'
251     # the menu items on the top
252     template_env [ 'topmenu_items' ] = topmenu_items('slice', request) 
253     # so we can sho who is logged
254     template_env [ 'username' ] = the_user (request) 
255
256     # don't forget to run the requests
257     page.expose_queries ()
258
259     # xxx create another plugin with the same query and a different layout (with_datatables)
260     # show that it worls as expected, one single api call to backend and 2 refreshed views
261
262     # the prelude object in page contains a summary of the requirements() for all plugins
263     # define {js,css}_{files,chunks}
264     prelude_env = page.prelude_env()
265     template_env.update(prelude_env)
266     result=render_to_response ('view-unfold1.html',template_env,
267                                context_instance=RequestContext(request))
268     return result