X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=plugins%2Fquerygrid%2F__init__.py;fp=plugins%2Fquerygrid%2F__init__.py;h=5b0c4662b674b77b31e04a39cc5d6ed544f54c4f;hb=b3f21324b9818b110814ce42d566b49de06f9a59;hp=0000000000000000000000000000000000000000;hpb=e3614e2c58f507c272b61cc169400a101277ee1b;p=myslice.git diff --git a/plugins/querygrid/__init__.py b/plugins/querygrid/__init__.py new file mode 100644 index 00000000..5b0c4662 --- /dev/null +++ b/plugins/querygrid/__init__.py @@ -0,0 +1,137 @@ +from unfold.plugin import Plugin + +class QueryGrid (Plugin): + + """A plugin for displaying a query as a list + +More accurately, we consider a subject entity (say, a slice) +that can be linked to any number of related entities (say, resources, or users) +The 'query' argument will correspond to the subject, while +'query_all' will fetch the complete list of +possible candidates for the relationship. + +Current implementation makes the following assumptions +* query will only retrieve for the related items a list of fields + that corresponds to the initial set of fields displayed in the table +* query_all on the contrary is expected to return the complete set of + available attributes that may be of interest, so that using a QueryEditor + one can easily extend this table without having to query the backend +* checkboxes is a boolean flag, set to true if a rightmost column + with checkboxes is desired +* optionally pass columns as the initial set of columns + if None then this is taken from the query's fields +* id_key is the name of a column used internally in the plugin + for checkboxes management. Caller should specify a column that is present + in the fields returned by 'query' and that has unique values. + If not specified, metadata will be used to find out a primary key. + However in the case of nodes & slice for example, the default key + as returned by the metadata would be 'urn', but it is not necessarily + a good idea to show urn's initially - if at all. + This is why a slice view would use 'hrn' here instead. +* datatables_options are passed to dataTables as-is; + however please refrain from passing an 'aoColumns' + as we use 'aoColumnDefs' instead. +""" + + def __init__ (self, query=None, query_all=None, + checkboxes=False, columns=None, + id_key=None, + datatables_options={}, **settings): + Plugin.__init__ (self, **settings) + self.query = query + # Until we have a proper way to access queries in Python + self.query_all = query_all + self.query_all_uuid = query_all.query_uuid if query_all else None + self.checkboxes = checkboxes + # XXX We need to have some hidden columns until we properly handle dynamic queries + if columns is not None: + self.columns=columns + self.hidden_columns = [] + elif self.query: + self.columns = self.query.fields + if query_all: + # We need a list because sets are not JSON-serializable + self.hidden_columns = list(self.query_all.fields - self.query.fields) + else: + self.hidden_columns = [] + else: + self.columns = [] + self.hidden_columns = [] + # needs to be json-serializable, and sets are not + self.columns=list(self.columns) + self.id_key=id_key + self.datatables_options=datatables_options + # if checkboxes were required, we tell datatables about this column's type + # so that sorting can take place on a selected-first basis (or -last of course) + # this relies on the template exposing the checkboxes 'th' with class 'checkbox' + if self.checkboxes: + # we use aoColumnDefs rather than aoColumns -- ignore user-provided aoColumns + if 'aoColumns' in self.datatables_options: + print 'WARNING: querygrid uses aoColumnDefs, your aoColumns spec. is discarded' + del self.datatables_options['aoColumns'] + # set aoColumnDefs in datatables_options - might already have stuff in there + aoColumnDefs = self.datatables_options.setdefault ('aoColumnDefs',[]) + # here 'checkbox' is the class that we give to the