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