94b539c1b70331dd30960a4452dfb4147e5547dc
[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     # TODO The query to run is embedded in the URL
57     main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
58     query_resource_all = Query.get('resource').select('resource_hrn', 'hostname', 'type', 'network_hrn', 'latitude', 'longitude')
59
60     # Get default fields from metadata unless specified
61     if not main_query.fields:
62         metadata = page.get_metadata()
63         md_fields = metadata.details_by_object('slice')
64         if debug:
65             print "METADATA", md_fields
66         # TODO Get default fields
67         main_query.select(
68                 'slice_hrn',
69                 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn',
70                 #'lease.urn',
71                 'user.user_hrn',
72 #                'application.measurement_point.counter'
73         )
74
75     aq = AnalyzedQuery(main_query, metadata=metadata)
76     page.enqueue_query(main_query, analyzed_query=aq)
77     page.enqueue_query(query_resource_all)
78
79     # Prepare the display according to all metadata
80     # (some parts will be pending, others can be triggered by users).
81     # 
82     # For example slice measurements will not be requested by default...
83
84     # Create the base layout (Stack)...
85     main_plugin = Stack (
86         page=page,
87         title="Slice !!view for %s"%slicename,
88         sons=[],
89     )
90
91     # ... responsible for the slice properties...
92
93
94     main_plugin.insert (
95         Raw (page=page,togglable=False, toggled=True,html="<h2> Slice page for %s</h2>"%slicename)
96     )
97
98     main_plugin.insert(
99         Raw (page=page,togglable=False, toggled=True,html='<b>Description:</b> TODO')
100     )
101
102     sq_plugin = Tabs (
103         page=page,
104         title="Slice view for %s"%slicename,
105         togglable=False,
106         sons=[],
107     )
108
109
110     # ... and for the relations
111     # XXX Let's hardcode resources for now
112     sq_resource = aq.subquery('resource')
113     sq_user     = aq.subquery('user')
114     sq_lease    = aq.subquery('lease')
115     sq_measurement = aq.subquery('measurement')
116     
117
118     ############################################################################
119     # RESOURCES
120     # 
121     # A stack inserted in the subquery tab that will hold all operations
122     # related to resources
123     # 
124     
125     stack_resources = Stack(
126         page = page,
127         title        = 'Resources',
128         sons=[],
129     )
130
131     resource_query_editor = QueryEditor(
132         page  = page,
133         query = sq_resource,
134     )
135     stack_resources.insert(resource_query_editor)
136
137     # --------------------------------------------------------------------------
138     # Different displays = DataTables + GoogleMaps
139     #
140     tab_resource_plugins = Tabs(
141         page    = page,
142         sons = []
143     )
144
145     tab_resource_plugins.insert(Hazelnut( 
146         page           = page,
147         title          = 'List',
148         domid          = 'checkboxes',
149         # this is the query at the core of the slice list
150         query          = sq_resource,
151         query_all_uuid = query_resource_all.query_uuid,
152         checkboxes     = True,
153         datatables_options = { 
154             # for now we turn off sorting on the checkboxes columns this way
155             # this of course should be automatic in hazelnut
156             'aoColumns'      : [None, None, None, None, {'bSortable': False}],
157             'iDisplayLength' : 25,
158             'bLengthChange'  : True,
159         },
160     ))
161
162     tab_resource_plugins.insert(GoogleMap(
163         page        = page,
164         title       = 'Geographic view',
165         domid       = 'gmap',
166         # tab's sons preferably turn this off
167         togglable   = False,
168         query       = sq_resource,
169         query_all_uuid = query_resource_all.query_uuid,
170         checkboxes     = True,
171         # center on Paris
172         latitude    = 49.,
173         longitude   = 2.2,
174         zoom        = 3,
175     ))
176
177     stack_resources.insert(tab_resource_plugins)
178
179     # --------------------------------------------------------------------------
180     # ResourcesSelected
181     #
182     stack_resources.insert(ResourcesSelected(
183         page                = page,
184         title               = 'Pending operations',
185         resource_query_uuid = sq_resource,
186         lease_query_uuid    = sq_lease,
187         togglable           = True,
188     ))
189
190     sq_plugin.insert(stack_resources)
191
192     ############################################################################
193     # USERS
194     # 
195
196     tab_users = Tabs(
197         page         = page,
198         title        = 'Users',
199         domid        = 'thetabs2',
200         # activeid   = 'checkboxes',
201         active_domid = 'checkboxes2',
202     )
203     sq_plugin.insert(tab_users)
204
205     tab_users.insert(Hazelnut( 
206         page        = page,
207         title       = 'List',
208         domid       = 'checkboxes2',
209         # tab's sons preferably turn this off
210         togglable   = False,
211         # this is the query at the core of the slice list
212         query       = sq_user,
213         checkboxes  = True,
214         datatables_options = { 
215             # for now we turn off sorting on the checkboxes columns this way
216             # this of course should be automatic in hazelnut
217             'aoColumns'      : [None, None, None, None, {'bSortable': False}],
218             'iDisplayLength' : 25,
219             'bLengthChange'  : True,
220         },
221     ))
222
223     tab_measurements = Tabs (
224         page         = page,
225         title        = 'Measurements',
226         domid        = 'thetabs3',
227         # activeid   = 'checkboxes',
228         active_domid = 'checkboxes3',
229     )
230     sq_plugin.insert(tab_measurements)
231
232     tab_measurements.insert(Hazelnut( 
233         page        = page,
234         title       = 'List',
235         domid       = 'checkboxes3',
236         # tab's sons preferably turn this off
237         togglable   = False,
238         # this is the query at the core of the slice list
239         query       = sq_measurement,
240         checkboxes  = True,
241         datatables_options = { 
242             # for now we turn off sorting on the checkboxes columns this way
243             # this of course should be automatic in hazelnut
244             'aoColumns'      : [None, None, None, None, {'bSortable': False}],
245             'iDisplayLength' : 25,
246             'bLengthChange'  : True,
247         },
248     ))
249
250     main_plugin.insert(sq_plugin)
251
252     main_plugin.insert(Messages(
253         page   = page,
254         title  = "Runtime messages for slice %s"%slicename,
255         domid  = "msgs-pre",
256         levels = "ALL",
257     ))
258     main_plugin.insert(Updater(
259         page   = page,
260         title  = "wont show up as non togglable by default",
261         query  = main_query,
262         label  = "Update slice",
263     ))
264     
265
266
267     # variables that will get passed to the view-unfold1.html template
268     template_env = {}
269     
270     # define 'unfold1_main' to the template engine - the main contents
271     template_env [ 'unfold1_main' ] = main_plugin.render(request)
272
273     # more general variables expected in the template
274     template_env [ 'title' ] = 'Test view that combines various plugins'
275     # the menu items on the top
276     template_env [ 'topmenu_items' ] = topmenu_items('Slice', request) 
277     # so we can sho who is logged
278     template_env [ 'username' ] = the_user (request) 
279
280     # don't forget to run the requests
281     page.expose_queries ()
282
283     # xxx create another plugin with the same query and a different layout (with_datatables)
284     # show that it worls as expected, one single api call to backend and 2 refreshed views
285
286     # the prelude object in page contains a summary of the requirements() for all plugins
287     # define {js,css}_{files,chunks}
288     prelude_env = page.prelude_env()
289     template_env.update(prelude_env)
290     result=render_to_response ('view-unfold1.html',template_env,
291                                context_instance=RequestContext(request))
292     return result