kept the code for both possible approaches wrt checkboxes
[myslice.git] / trash / simpleview.py
1 # just one instance of QueryTable, nothing more, nothing less
2 from django.views.generic.base import TemplateView
3 from django.template import RequestContext
4 from django.shortcuts import render_to_response
5
6 from manifold.core.query import Query, AnalyzedQuery
7
8 from unfold.page import Page
9
10 from ui.topmenu import topmenu_items, the_user
11
12 from plugins.querytable import QueryTable
13
14 class SimpleView (TemplateView):
15
16     def get (self, request, slicename='ple.inria.f14'):
17
18         page=Page(request)
19         page.expose_js_metadata()
20         metadata = page.get_metadata()
21         resource_md = metadata.details_by_object('resource')
22         resource_fields = [column['name'] for column in resource_md['column']]
23
24         main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
25         main_query.select(
26                 'slice_hrn',
27                 'resource.hrn', 'resource.hostname', 'resource.type', 
28                 'resource.network_hrn',
29                 'lease.urn',
30                 'user.user_hrn',
31                 #'application.measurement_point.counter'
32         )
33         # for internal use in the querytable plugin;
34         # needs to be a unique column present for each returned record
35         main_query_key = 'hrn'
36     
37         query_resource_all = Query.get('resource').select(resource_fields)
38         
39         aq = AnalyzedQuery(main_query, metadata=metadata)
40         page.enqueue_query(main_query, analyzed_query=aq)
41         page.enqueue_query(query_resource_all)
42
43         sq_resource    = aq.subquery('resource')
44
45         resources_as_list = QueryTable( 
46             page       = page,
47             domid      = 'resources-list',
48             title      = 'List view',
49             # this is the query at the core of the slice list
50             query      = sq_resource,
51             query_all  = query_resource_all,
52             # safer to use 'hrn' as the internal unique key for this plugin
53             id_key     = main_query_key,
54             checkboxes = True,
55             datatables_options = { 
56                 'iDisplayLength': 25,
57                 'bLengthChange' : True,
58                 'bAutoWidth'    : True,
59                 },
60             )
61
62         # variables that will get passed to the view-unfold1.html template
63         template_env = {}
64         
65         # define 'unfold_main' to the template engine - the main contents
66         template_env [ 'unfold_main' ] = resources_as_list.render(request)
67     
68         # more general variables expected in the template
69         template_env [ 'title' ] = 'simple %(slicename)s'%locals()
70         # the menu items on the top
71         template_env [ 'topmenu_items' ] = topmenu_items('Slice', request) 
72         # so we can sho who is logged
73         template_env [ 'username' ] = the_user (request) 
74     
75         # don't forget to run the requests
76         page.expose_queries ()
77
78         # the prelude object in page contains a summary of the requirements() for all plugins
79         # define {js,css}_{files,chunks}
80         prelude_env = page.prelude_env()
81         template_env.update(prelude_env)
82         result=render_to_response ('view-unfold1.html',template_env,
83                                    context_instance=RequestContext(request))
84         return result