de7fcbcc82fac6e635d543e950c1918fec267501
[myslice.git] / plugins / querytable / __init__.py
1 from unfold.plugin import Plugin
2
3 class QueryTable (Plugin):
4
5     """A plugin for displaying a query as a list
6
7 More accurately, we consider a subject entity (say, a slice) 
8 that can be linked to any number of related entities (say, resources, or users)
9 The 'query' argument will correspond to the subject, while
10 'query_all' will fetch the complete list of 
11 possible candidates for the relationship.
12
13 Current implementation makes the following assumptions
14 * query will only retrieve for the related items a list of fields
15   that corresponds to the initial set of fields displayed in the table
16 * query_all on the contrary is expected to return the complete set of 
17   available attributes that may be of interest, so that using a QueryEditor
18   one can easily extend this table without having to query the backend
19 * checkboxes is a boolean flag, set to true if a rightmost column
20   with checkboxes is desired
21 * optionally pass columns as the initial set of columns
22   if None then this is taken from the query's fields
23 * id_key is the name of a column used internally in the plugin
24   for checkboxes management. Caller should specify a column that is present 
25   in the fields returned by 'query' and that has unique values.
26   If not specified, metadata will be used to find out a primary key.
27   However in the case of nodes & slice for example, the default key
28   as returned by the metadata would be 'urn', but it is not necessarily 
29   a good idea to show urn's initially - if at all.
30   This is why a slice view would use 'hrn' here instead.
31 * datatables_options are passed to dataTables as-is; 
32   however please refrain from passing an 'aoColumns' 
33   as we use 'aoColumnDefs' instead.
34 """
35
36     def __init__ (self, query=None, query_all=None, 
37                   checkboxes=False, columns=None, 
38                   id_key=None,
39                   datatables_options={}, **settings):
40         Plugin.__init__ (self, **settings)
41         self.query          = query
42         # Until we have a proper way to access queries in Python
43         self.query_all      = query_all
44         self.query_all_uuid = query_all.query_uuid if query_all else None
45         self.checkboxes     = checkboxes
46         # XXX We need to have some hidden columns until we properly handle dynamic queries
47         if columns is not None:
48             self.columns=columns
49             self.hidden_columns = []
50         elif self.query:
51             self.columns = self.query.fields
52             if query_all:
53                 # We need a list because sets are not JSON-serializable
54                 self.hidden_columns = list(self.query_all.fields - self.query.fields)
55             else:
56                 self.hidden_columns = []
57         else:
58             self.columns = []
59             self.hidden_columns = []
60         # needs to be json-serializable, and sets are not
61         self.columns=list(self.columns)
62         self.id_key=id_key
63         self.datatables_options=datatables_options
64         # if checkboxes were required, we tell datatables about this column's type
65         # so that sorting can take place on a selected-first basis (or -last of course)
66         # this relies on the template exposing the checkboxes 'th' with class 'checkbox'
67         if self.checkboxes:
68             # we use aoColumnDefs rather than aoColumns -- ignore user-provided aoColumns
69             if 'aoColumns' in self.datatables_options:
70                 print 'WARNING: querytable uses aoColumnDefs, your aoColumns spec. is discarded'
71                 del self.datatables_options['aoColumns']
72             # set aoColumnDefs in datatables_options - might already have stuff in there
73             aoColumnDefs = self.datatables_options.setdefault ('aoColumnDefs',[])
74             # here 'checkbox' is the class that we give to the <th> dom elem
75             # dom-checkbox is a sorting type that we define in querytable.js
76             aoColumnDefs.append ( {'aTargets': ['checkbox'], 'sSortDataType': 'dom-checkbox' } )
77
78     def template_file (self):
79         return "querytable.html"
80
81     def template_env (self, request):
82         env={}
83         env.update(self.__dict__)
84         env['columns']=self.columns
85         return env
86
87     def requirements (self):
88         reqs = {
89             'js_files' : [ 
90                 "js/spin.presets.js", "js/spin.min.js", "js/jquery.spin.js", 
91 # this one was in the slickgrid demo 
92 # http://mleibman.github.io/SlickGrid/examples/example-checkbox-row-select.html
93 # but triggers js errors when included - probably/maybe because of the jquery version ?
94 # it might be responsible for not being able to select a row by clicking anywhere in it ?
95 #                "http://mleibman.github.io/SlickGrid/lib/jquery-ui-1.8.16.custom.min.js",
96                 "js/jquery.event.drag-2.2.js",   # from slickgrid/lib
97                 "js/slick.core.js",
98                 "js/slick.autotooltips.js",       # from slickgrid/plugins/
99                 "js/slick.cellrangedecorator.js",       # from slickgrid/plugins/
100                 "js/slick.cellrangeselector.js",       # from slickgrid/plugins/
101                 "js/slick.cellcopymanager.js",       # from slickgrid/plugins/
102                 "js/slick.cellselectionmodel.js",       # from slickgrid/plugins/
103                 "js/slick.rowselectionmodel.js",       # from slickgrid/plugins/
104                 "js/slick.checkboxselectcolumn.js",       # from slickgrid/plugins/
105                 "js/slick.columnpicker.js",             # from slickgrid/controls/
106                 "js/slick.formatters.js",
107                 "js/slick.editors.js",
108                 "js/slick.grid.js",
109                 "js/slick.dataview.js",
110
111 #                "js/dataTables.js", "js/dataTables.bootstrap.js", "js/with-datatables.js",
112                 "js/manifold.js", "js/manifold-query.js", 
113                 "js/unfold-helper.js",
114                 # querytable.js needs to be loaded after dataTables.js as it extends 
115                 # dataTableExt.afnSortData
116                 "js/querytable.js", 
117                 ] ,
118             'css_files': [ 
119 #                "css/dataTables.bootstrap.css",
120                 # hopefully temporary, when/if datatables supports sPaginationType=bootstrap3
121                 # for now we use full_numbers, with our own ad hoc css 
122 #                "css/dataTables.full_numbers.css",
123                 "css/querytable.css" , 
124                 "http://mleibman.github.io/SlickGrid/slick.grid.css",
125 #                "http://mleibman.github.io/SlickGrid/css/smoothness/jquery-ui-1.8.16.custom.css",
126 #                "http://mleibman.github.io/SlickGrid/examples/examples.css",
127                 ],
128             }
129         return reqs
130
131     # the list of things passed to the js plugin
132     def json_settings_list (self):
133         return ['plugin_uuid', 'domid', 
134                 'query_uuid', 'query_all_uuid', 
135                 'checkboxes', 'datatables_options', 
136                 'columns','hidden_columns', 
137                 'id_key',]