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