diff options
| author | Aaron Bohy <aab@odoo.com> | 2019-03-27 13:38:46 +0000 |
|---|---|---|
| committer | Aaron Bohy <aab@odoo.com> | 2019-04-02 11:57:00 +0000 |
| commit | ca19b17fd3e09d2f4e53208542f4356d3a6ee25a (patch) | |
| tree | 63b092ff0681647cfeba79db64bca051ca45ed0c /addons/web/models/models.py | |
| parent | d0164d834bb18f37f15a129e911f7d8e1c54b7c3 (diff) | |
[IMP] web: fetch records alongside read_group
This rev. adds params to the 'web_read_group' method, allowing to
search_read records inside each groups in a single RPC, instead of
doing a 'web_read_group' RPC followed by n 'search_read' RPCs,
where n is the number of opened groups.
Part of task 1915702
Diffstat (limited to 'addons/web/models/models.py')
| -rw-r--r-- | addons/web/models/models.py | 63 |
1 files changed, 58 insertions, 5 deletions
diff --git a/addons/web/models/models.py b/addons/web/models/models.py index 271f8b831da..e85de4f6c45 100644 --- a/addons/web/models/models.py +++ b/addons/web/models/models.py @@ -18,11 +18,41 @@ class Base(models.AbstractModel): _inherit = 'base' @api.model + def web_search_read(self, domain=None, fields=None, offset=0, limit=None, order=None): + """ + Performs a search_read and a search_count. + + :param domain: search domain + :param fields: list of fields to read + :param limit: maximum number of records to read + :param offset: number of records to skip + :param order: columns to sort results + :return: { + 'records': array of read records (result of a call to 'search_read') + 'length': number of records matching the domain (result of a call to 'search_count') + } + """ + records = self.search_read(domain, fields, offset=offset, limit=limit, order=order) + if not records: + return { + 'length': 0, + 'records': [] + } + if limit and len(records) == limit: + length = self.search_count(domain) + else: + length = len(records) + offset + return { + 'length': length, + 'records': records + } + + @api.model def web_read_group(self, domain, fields, groupby, limit=None, offset=0, orderby=False, - lazy=True): + lazy=True, expand=False, expand_limit=None, expand_orderby=False): """ - Returns the result of a read_group and the total number of groups matching the search - domain. + Returns the result of a read_group (and optionally search for and read records inside each + group), and the total number of groups matching the search domain. :param domain: search domain :param fields: list of fields to read (see ``fields``` param of ``read_group``) @@ -31,13 +61,16 @@ class Base(models.AbstractModel): :param offset: see ``offset`` param of ``read_group`` :param orderby: see ``orderby`` param of ``read_group`` :param lazy: see ``lazy`` param of ``read_group`` + :param expand: if true, and groupby only contains one field, read records inside each group + :param expand_limit: maximum number of records to read in each group + :param expand_orderby: order to apply when reading records in each group :return: { 'groups': array of read groups 'length': total number of groups } """ - groups = self.read_group(domain, fields, groupby, offset=offset, limit=limit, - orderby=orderby, lazy=lazy) + groups = self._web_read_group(domain, fields, groupby, limit, offset, orderby, lazy, expand, + expand_limit, expand_orderby) if not groups: length = 0 @@ -52,6 +85,26 @@ class Base(models.AbstractModel): } @api.model + def _web_read_group(self, domain, fields, groupby, limit=None, offset=0, orderby=False, + lazy=True, expand=False, expand_limit=None, expand_orderby=False): + """ + Performs a read_group and optionally a web_search_read for each group. + See ``web_read_group`` for params description. + + :returns: array of groups + """ + groups = self.read_group(domain, fields, groupby, offset=offset, limit=limit, + orderby=orderby, lazy=lazy) + + if expand and len(groupby) == 1: + for group in groups: + group['__data'] = self.web_search_read(domain=group['__domain'], fields=fields, + offset=0, limit=expand_limit, + order=expand_orderby) + + return groups + + @api.model def read_progress_bar(self, domain, group_by, progress_bar): """ Gets the data needed for all the kanban column progressbars. |
