Browse Source

publish muk_utils - 12.0

pull/30/head
MuK IT GmbH 6 years ago
parent
commit
7757f76112
  1. 2
      muk_utils/__manifest__.py
  2. 20
      muk_utils/models/base.py

2
muk_utils/__manifest__.py

@ -19,7 +19,7 @@
{ {
"name": "MuK Utils", "name": "MuK Utils",
"summary": """Utility Features""", "summary": """Utility Features""",
"version": '12.0.1.6.22',
"version": '12.0.1.6.24',
"category": 'Extra Tools', "category": 'Extra Tools',
"license": "AGPL-3", "license": "AGPL-3",
"author": "MuK IT", "author": "MuK IT",

20
muk_utils/models/base.py

@ -79,17 +79,18 @@ class Base(models.AbstractModel):
#---------------------------------------------------------- #----------------------------------------------------------
@api.model @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. """ This method finds the top level elements of the hierarchy for a given search query.
:param domain: a search domain <reference/orm/domains> (default: empty list) :param domain: a search domain <reference/orm/domains> (default: empty list)
:param order: a string to define the sort order of the query (default: none) :param order: a string to define the sort order of the query (default: none)
:returns: the top level elements for the given search query :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 @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. """ This method finds the top level elements of the hierarchy for a given search query.
:param domain: a search domain <reference/orm/domains> (default: empty list) :param domain: a search domain <reference/orm/domains> (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) :param order: a string to define the sort order of the query (default: none)
:returns: the top level elements for the given search query :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: if not records:
return [] return []
if fields and fields == ['id']: 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] return [index[record.id] for record in records if record.id in index]
@api.model @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_parent_field()
self.check_access_rights('read') self.check_access_rights('read')
if expression.is_false(self, domain): if expression.is_false(self, domain):
@ -134,13 +135,18 @@ class Base(models.AbstractModel):
) )
order_by = self._generate_order_by(order, query) order_by = self._generate_order_by(order, query)
from_clause, where_clause, where_clause_params = query.get_sql() from_clause, where_clause, where_clause_params = query.get_sql()
where_str = ( where_str = (
where_clause and where_clause and
(" WHERE %s AND %s" % (where_clause, parent_clause)) or (" WHERE %s AND %s" % (where_clause, parent_clause)) or
(" WHERE %s" % parent_clause) (" 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 complete_where_clause_params = where_clause_params + where_clause_arguments
self._cr.execute(query_str, complete_where_clause_params) self._cr.execute(query_str, complete_where_clause_params)
return utils.uniquify_list([x[0] for x in self._cr.fetchall()]) return utils.uniquify_list([x[0] for x in self._cr.fetchall()])

Loading…
Cancel
Save