pretty-printed (4 chars margin for css please)
[myslice.git] / plugins / querytable / __init__.py
1 from unfold.plugin import Plugin
2
3 class QueryTable (Plugin):
4
5     # set checkboxes if a final column with checkboxes is desired
6     # pass columns as the initial set of columns
7     #   if None then this is taken from the query's fields
8     # also please refrain from passing an 'aoColumns' as datatables_options
9     # as we use 'aoColumnDefs' instead
10     def __init__ (self, query=None, query_all=None, 
11                   checkboxes=False, columns=None, 
12                   datatables_options={}, **settings):
13         Plugin.__init__ (self, **settings)
14         self.query          = query
15         # Until we have a proper way to access queries in Python
16         self.query_all      = query_all
17         self.query_all_uuid = query_all.query_uuid if query_all else None
18         self.checkboxes     = checkboxes
19         # XXX We need to have some hidden columns until we properly handle dynamic queries
20         if columns is not None:
21             self.columns=columns
22             self.hidden_columns = []
23         elif self.query:
24             self.columns = self.query.fields
25             if query_all:
26                 # We need a list because sets are not JSON-serializable
27                 self.hidden_columns = list(self.query_all.fields - self.query.fields)
28             else:
29                 self.hidden_columns = []
30         else:
31             self.columns = []
32             self.hidden_columns = []
33         self.datatables_options=datatables_options
34         # if checkboxes were required, we tell datatables about this column's type
35         # so that sorting can take place on a selected-first basis (or -last of course)
36         # this relies on the template exposing the checkboxes 'th' with class 'checkbox'
37         if self.checkboxes:
38             # we use aoColumnDefs rather than aoColumns -- ignore user-provided aoColumns
39             if 'aoColumns' in self.datatables_options:
40                 print 'WARNING: querytable uses aoColumnDefs, your aoColumns spec. is discarded'
41                 del self.datatables_options['aoColumns']
42             # set aoColumnDefs in datatables_options - might already have stuff in there
43             aoColumnDefs = self.datatables_options.setdefault ('aoColumnDefs',[])
44             # here 'checkbox' is the class that we give to the <th> dom elem
45             # dom-checkbox is a sorting type that we define in querytable.js
46             aoColumnDefs.append ( {'aTargets': ['checkbox'], 'sSortDataType': 'dom-checkbox' } )
47
48     def template_file (self):
49         return "querytable.html"
50
51     def template_env (self, request):
52         env={}
53         env.update(self.__dict__)
54         env['columns']=self.columns
55         return env
56
57     def requirements (self):
58         reqs = {
59             'js_files' : [ "js/spin.presets.js", "js/spin.min.js", "js/jquery.spin.js", 
60                            "js/dataTables.js", "js/dataTables.bootstrap.js", "js/with-datatables.js",
61                            "js/manifold.js", "js/manifold-query.js", 
62                            "js/unfold-helper.js",
63                           # querytable.js needs to be loaded after dataTables.js as it extends 
64                           # dataTableExt.afnSortData
65                            "js/querytable.js", 
66                            ] ,
67             'css_files': [ "css/dataTables.bootstrap.css",
68                            # hopefully temporary, when/if datatables supports sPaginationType=bootstrap3
69                            # for now we use full_numbers, with our own ad hoc css 
70                            "css/dataTables.full_numbers.css",
71                            "css/querytable.css" , 
72                            ],
73             }
74         return reqs
75
76     # the list of things passed to the js plugin
77     def json_settings_list (self):
78         return ['plugin_uuid', 'domid', 
79                 'query_uuid', 'query_all_uuid', 
80                 'checkboxes', 'datatables_options', 
81                 'hidden_columns']