9d08cee8dcec7f61d796d6599fc0f792f0c4e81e
[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 from django.contrib.auth.decorators  import login_required
6 from django.http                     import HttpResponseRedirect
7
8 from unfold.page                     import Page
9 from manifold.core.query             import Query, AnalyzedQuery
10 from manifold.manifoldresult         import ManifoldException
11 from manifold.metadata               import MetaData as Metadata
12 from myslice.viewutils               import quickfilter_criterias, topmenu_items, the_user
13
14 from plugins.raw.raw                 import Raw
15 from plugins.stack.stack             import Stack
16 from plugins.tabs.tabs               import Tabs
17 from plugins.lists.slicelist         import SliceList
18 from plugins.hazelnut.hazelnut       import Hazelnut 
19 from plugins.resources_selected      import ResourcesSelected
20 from plugins.googlemap.googlemap     import GoogleMap 
21 from plugins.senslabmap.senslabmap   import SensLabMap
22 from plugins.querycode.querycode     import QueryCode
23 from plugins.query_editor            import QueryEditor
24 from plugins.quickfilter.quickfilter import QuickFilter
25 from plugins.messages.messages       import Messages
26 from plugins.updater.updater         import Updater
27
28 tmp_default_slice='ple.inria.myslicedemo'
29 debug = True
30
31 @login_required
32 def slice_view (request, slicename=tmp_default_slice):
33     # xxx Thierry - ugly hack
34     # fetching metadata here might fail - e.g. with an expired session..
35     # let's catch this early on and log out our user if needed
36     # it should of course be handled in a more generic way
37     try:
38         return _slice_view(request,slicename)
39     except ManifoldException, manifold_result:
40         # xxx needs a means to display this message to user...
41         from django.contrib.auth import logout
42         logout(request)
43         return HttpResponseRedirect ('/')
44     except Exception, e:
45         # xxx we need to sugarcoat this error message in some error template...
46         print "Unexpected exception",e
47         import traceback
48         traceback.print_exc()
49         # return ...
50
51 def _slice_view (request, slicename):
52
53     page = Page(request)
54     page.expose_js_metadata()
55
56     metadata = page.get_metadata()
57     resource_md = metadata.details_by_object('resource')
58     resource_fields = [column['name'] for column in resource_md['column']]
59
60     # TODO The query to run is embedded in the URL
61     main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
62     main_query.select(
63             'slice_hrn',
64             'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn',
65             #'lease.urn',
66             'user.user_hrn',
67             #'application.measurement_point.counter'
68     )
69
70     query_resource_all = Query.get('resource').select(resource_fields)
71
72     aq = AnalyzedQuery(main_query, metadata=metadata)
73     page.enqueue_query(main_query, analyzed_query=aq)
74     page.enqueue_query(query_resource_all)
75
76     # Prepare the display according to all metadata
77     # (some parts will be pending, others can be triggered by users).
78     # 
79     # For example slice measurements will not be requested by default...
80
81     # Create the base layout (Stack)...
82     main_plugin = Stack (
83         page=page,
84         title="Slice !!view for %s"%slicename,
85         sons=[],
86     )
87
88     # ... responsible for the slice properties...
89
90
91     main_plugin.insert (
92         Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename)
93     )
94
95     main_plugin.insert(
96         Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
97     )
98
99     sq_plugin = Tabs (
100         page=page,
101         title="Slice view for %s"%slicename,
102         togglable=False,
103         sons=[],
104     )
105
106
107     # ... and for the relations
108     # XXX Let's hardcode resources for now
109     sq_resource = aq.subquery('resource')
110     sq_user     = aq.subquery('user')
111     sq_lease    = aq.subquery('lease')
112     sq_measurement = aq.subquery('measurement')
113     
114
115     ############################################################################
116     # RESOURCES
117     # 
118     # A stack inserted in the subquery tab that will hold all operations
119     # related to resources
120     # 
121     
122     stack_resources = Stack(
123         page = page,
124         title        = 'Resources',
125         sons=[],
126     )
127
128     resource_query_editor = QueryEditor(
129         page  = page,
130         query = sq_resource,
131     )
132     stack_resources.insert(resource_query_editor)
133
134     # --------------------------------------------------------------------------
135     # Different displays = DataTables + GoogleMaps
136     #
137     tab_resource_plugins = Tabs(
138         page    = page,
139         sons = []
140     )
141
142     tab_resource_plugins.insert(Hazelnut( 
143         page       = page,
144         title      = 'List',
145         domid      = 'checkboxes',
146         # this is the query at the core of the slice list
147         query      = sq_resource,
148         query_all  = query_resource_all,
149         checkboxes = True,
150         datatables_options = { 
151             # for now we turn off sorting on the checkboxes columns this way
152             # this of course should be automatic in hazelnut
153             'aoColumns'      : [None, None, None, None, {'bSortable': False}],
154             'iDisplayLength' : 25,
155             'bLengthChange'  : True,
156         },
157     ))
158
159     tab_resource_plugins.insert(GoogleMap(
160         page       = page,
161         title      = 'Geographic view',
162         domid      = 'gmap',
163         # tab's sons preferably turn this off
164         togglable  = False,
165         query      = sq_resource,
166         query_all  = query_resource_all,
167         checkboxes = True,
168         # center on Paris
169         latitude   = 49.,
170         longitude  = 2.2,
171         zoom       = 3,
172     ))
173
174     stack_resources.insert(tab_resource_plugins)
175
176     # --------------------------------------------------------------------------
177     # ResourcesSelected
178     #
179     stack_resources.insert(ResourcesSelected(
180         page                = page,
181         title               = 'Pending operations',
182         resource_query_uuid = sq_resource,
183         lease_query_uuid    = sq_lease,
184         togglable           = True,
185     ))
186
187     sq_plugin.insert(stack_resources)
188
189     ############################################################################
190     # USERS
191     # 
192
193     tab_users = Tabs(
194         page         = page,
195         title        = 'Users',
196         domid        = 'thetabs2',
197         # activeid   = 'checkboxes',
198         active_domid = 'checkboxes2',
199     )
200     sq_plugin.insert(tab_users)
201
202     tab_users.insert(Hazelnut( 
203         page        = page,
204         title       = 'List',
205         domid       = 'checkboxes2',
206         # tab's sons preferably turn this off
207         togglable   = False,
208         # this is the query at the core of the slice list
209         query       = sq_user,
210         checkboxes  = True,
211         datatables_options = { 
212             # for now we turn off sorting on the checkboxes columns this way
213             # this of course should be automatic in hazelnut
214             'aoColumns'      : [None, None, None, None, {'bSortable': False}],
215             'iDisplayLength' : 25,
216             'bLengthChange'  : True,
217         },
218     ))
219
220     tab_measurements = Tabs (
221         page         = page,
222         title        = 'Measurements',
223         domid        = 'thetabs3',
224         # activeid   = 'checkboxes',
225         active_domid = 'checkboxes3',
226     )
227     sq_plugin.insert(tab_measurements)
228
229     tab_measurements.insert(Hazelnut( 
230         page        = page,
231         title       = 'List',
232         domid       = 'checkboxes3',
233         # tab's sons preferably turn this off
234         togglable   = False,
235         # this is the query at the core of the slice list
236         query       = sq_measurement,
237         checkboxes  = True,
238         datatables_options = { 
239             # for now we turn off sorting on the checkboxes columns this way
240             # this of course should be automatic in hazelnut
241             'aoColumns'      : [None, None, None, None, {'bSortable': False}],
242             'iDisplayLength' : 25,
243             'bLengthChange'  : True,
244         },
245     ))
246
247     main_plugin.insert(sq_plugin)
248
249     main_plugin.insert(Messages(
250         page   = page,
251         title  = "Runtime messages for slice %s"%slicename,
252         domid  = "msgs-pre",
253         levels = "ALL",
254     ))
255     main_plugin.insert(Updater(
256         page   = page,
257         title  = "wont show up as non togglable by default",
258         query  = main_query,
259         label  = "Update slice",
260     ))
261     
262
263
264     # variables that will get passed to the view-unfold1.html template
265     template_env = {}
266     
267     # define 'unfold1_main' to the template engine - the main contents
268     template_env [ 'unfold1_main' ] = main_plugin.render(request)
269
270     # more general variables expected in the template
271     template_env [ 'title' ] = 'Test view that combines various plugins'
272     # the menu items on the top
273     template_env [ 'topmenu_items' ] = topmenu_items('Slice', request) 
274     # so we can sho who is logged
275     template_env [ 'username' ] = the_user (request) 
276
277     # don't forget to run the requests
278     page.expose_queries ()
279
280     # xxx create another plugin with the same query and a different layout (with_datatables)
281     # show that it worls as expected, one single api call to backend and 2 refreshed views
282
283     # the prelude object in page contains a summary of the requirements() for all plugins
284     # define {js,css}_{files,chunks}
285     prelude_env = page.prelude_env()
286     template_env.update(prelude_env)
287     result=render_to_response ('view-unfold1.html',template_env,
288                                context_instance=RequestContext(request))
289     return result