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