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