From 7757f761127d5718d05f5fcfd3e9fd9da270c08d Mon Sep 17 00:00:00 2001 From: MuK IT GmbH Date: Sat, 20 Apr 2019 12:00:19 +0000 Subject: [PATCH] publish muk_utils - 12.0 --- muk_utils/__manifest__.py | 2 +- muk_utils/models/base.py | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/muk_utils/__manifest__.py b/muk_utils/__manifest__.py index 6172c7d..0ecd8c4 100644 --- a/muk_utils/__manifest__.py +++ b/muk_utils/__manifest__.py @@ -19,7 +19,7 @@ { "name": "MuK Utils", "summary": """Utility Features""", - "version": '12.0.1.6.22', + "version": '12.0.1.6.24', "category": 'Extra Tools', "license": "AGPL-3", "author": "MuK IT", diff --git a/muk_utils/models/base.py b/muk_utils/models/base.py index b959de9..f857e60 100644 --- a/muk_utils/models/base.py +++ b/muk_utils/models/base.py @@ -79,17 +79,18 @@ class Base(models.AbstractModel): #---------------------------------------------------------- @api.model - def search_parents(self, domain=[], order=None): + def search_parents(self, domain=[], offset=0, limit=None, order=None, count=False): """ This method finds the top level elements of the hierarchy for a given search query. :param domain: a search domain (default: empty list) :param order: a string to define the sort order of the query (default: none) :returns: the top level elements for the given search query """ - return self.browse(self._search_parents(domain=domain, order=order)) + res = self._search_parents(domain=domain, offset=offset, limit=limit, order=order, count=count) + return res if count else self.browse(res) @api.model - def search_read_parents(self, domain=[], fields=None, order=None): + def search_read_parents(self, domain=[], fields=None, offset=0, limit=None, order=None): """ This method finds the top level elements of the hierarchy for a given search query. :param domain: a search domain (default: empty list) @@ -97,7 +98,7 @@ class Base(models.AbstractModel): :param order: a string to define the sort order of the query (default: none) :returns: the top level elements for the given search query """ - records = self.search_parents(domain=domain, order=order) + records = self.search_parents(domain=domain, offset=offset, limit=limit, order=order) if not records: return [] if fields and fields == ['id']: @@ -109,7 +110,7 @@ class Base(models.AbstractModel): return [index[record.id] for record in records if record.id in index] @api.model - def _search_parents(self, domain=[], order=None): + def _search_parents(self, domain=[], offset=0, limit=None, order=None, count=False): self._check_parent_field() self.check_access_rights('read') if expression.is_false(self, domain): @@ -134,13 +135,18 @@ class Base(models.AbstractModel): ) order_by = self._generate_order_by(order, query) from_clause, where_clause, where_clause_params = query.get_sql() - where_str = ( where_clause and (" WHERE %s AND %s" % (where_clause, parent_clause)) or (" WHERE %s" % parent_clause) ) - query_str = 'SELECT "%s".id FROM ' % self._table + from_clause + where_str + order_by + if count: + query_str = 'SELECT count(1) FROM ' + from_clause + where_str + self._cr.execute(query_str, where_clause_params) + return self._cr.fetchone()[0] + limit_str = limit and ' limit %d' % limit or '' + offset_str = offset and ' offset %d' % offset or '' + query_str = 'SELECT "%s".id FROM ' % self._table + from_clause + where_str + order_by + limit_str + offset_str complete_where_clause_params = where_clause_params + where_clause_arguments self._cr.execute(query_str, complete_where_clause_params) return utils.uniquify_list([x[0] for x in self._cr.fetchall()])