You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
7.0 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (C) 2018 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import logging
  20. from odoo import api, models, fields
  21. from odoo.osv import expression
  22. from odoo.addons.muk_utils.tools import utils
  23. _logger = logging.getLogger(__name__)
  24. class Base(models.AbstractModel):
  25. _inherit = 'base'
  26. #----------------------------------------------------------
  27. # Helper Methods
  28. #----------------------------------------------------------
  29. def _check_parent_field(self):
  30. if self._parent_name not in self._fields:
  31. raise TypeError("The parent (%s) field does not exist." % self._parent_name)
  32. def _build_search_childs_domain(self, parent_id, domain=[]):
  33. self._check_parent_field()
  34. parent_domain = [[self._parent_name, '=', parent_id]]
  35. return expression.AND(parent_domain, domain) if domain else parent_domain
  36. #----------------------------------------------------------
  37. # Hierarchy Methods
  38. #----------------------------------------------------------
  39. @api.model
  40. def search_parents(self, domain=[], order=None):
  41. """ This method finds the top level elements of the hierarchy for a given search query.
  42. :param domain: a search domain <reference/orm/domains> (default: empty list)
  43. :param order: a string to define the sort order of the query (default: none)
  44. :returns: the top level elements for the given search query
  45. """
  46. return self.browse(self._search_parents(domain=domain, order=order))
  47. @api.model
  48. def search_read_parents(self, domain=[], fields=None, order=None):
  49. """ This method finds the top level elements of the hierarchy for a given search query.
  50. :param domain: a search domain <reference/orm/domains> (default: empty list)
  51. :param fields: a list of fields to read (default: all fields of the model)
  52. :param order: a string to define the sort order of the query (default: none)
  53. :returns: the top level elements for the given search query
  54. """
  55. records = self.search_parents(domain=domain, order=order)
  56. if not records:
  57. return []
  58. if fields and fields == ['id']:
  59. return [{'id': record.id} for record in records]
  60. result = records.read(fields)
  61. if len(result) <= 1:
  62. return result
  63. index = {vals['id']: vals for vals in result}
  64. return [index[record.id] for record in records if record.id in index]
  65. @api.model
  66. def _search_parents(self, domain=[], order=None):
  67. self._check_parent_field()
  68. self.check_access_rights('read')
  69. if expression.is_false(self, domain):
  70. return []
  71. query = self._where_calc(domain)
  72. self._apply_ir_rules(query, 'read')
  73. from_clause, where_clause, where_clause_arguments = query.get_sql()
  74. parent_where = where_clause and (" WHERE %s" % where_clause) or ''
  75. parent_query = 'SELECT "%s".id FROM ' % self._table + from_clause + parent_where
  76. no_parent_clause ='"{table}"."{field}" IS NULL'.format(
  77. table=self._table,
  78. field=self._parent_name
  79. )
  80. no_access_clause ='"{table}"."{field}" NOT IN ({query})'.format(
  81. table=self._table,
  82. field=self._parent_name,
  83. query=parent_query
  84. )
  85. parent_clause = '({0} OR {1})'.format(
  86. no_parent_clause,
  87. no_access_clause
  88. )
  89. order_by = self._generate_order_by(order, query)
  90. from_clause, where_clause, where_clause_params = query.get_sql()
  91. where_str = (
  92. where_clause and
  93. (" WHERE %s AND %s" % (where_clause, parent_clause)) or
  94. (" WHERE %s" % parent_clause)
  95. )
  96. query_str = 'SELECT "%s".id FROM ' % self._table + from_clause + where_str + order_by
  97. complete_where_clause_params = where_clause_params + where_clause_arguments
  98. self._cr.execute(query_str, complete_where_clause_params)
  99. return utils.uniquify_list([x[0] for x in self._cr.fetchall()])
  100. @api.model
  101. def search_childs(self, parent_id, domain=[], offset=0, limit=None, order=None, count=False):
  102. """ This method finds the direct child elements of the parent record for a given search query.
  103. :param parent_id: the integer representing the ID of the parent record
  104. :param domain: a search domain <reference/orm/domains> (default: empty list)
  105. :param offset: the number of results to ignore (default: none)
  106. :param limit: maximum number of records to return (default: all)
  107. :param order: a string to define the sort order of the query (default: none)
  108. :param count: counts and returns the number of matching records (default: False)
  109. :returns: the top level elements for the given search query
  110. """
  111. domain = self._build_search_childs_domain(parent_id, domain=domain)
  112. return self.search(domain, offset=offset, limit=limit, order=order, count=count)
  113. @api.model
  114. def search_read_childs(self, parent_id, domain=[], fields=None, offset=0, limit=None, order=None):
  115. """ This method finds the direct child elements of the parent record for a given search query.
  116. :param parent_id: the integer representing the ID of the parent record
  117. :param domain: a search domain <reference/orm/domains> (default: empty list)
  118. :param fields: a list of fields to read (default: all fields of the model)
  119. :param offset: the number of results to ignore (default: none)
  120. :param limit: maximum number of records to return (default: all)
  121. :param order: a string to define the sort order of the query (default: none)
  122. :returns: the top level elements for the given search query
  123. """
  124. domain = self._build_search_childs_domain(parent_id, domain=domain)
  125. return self.search_read(domain=domain, fields=fields, offset=offset, limit=limit, order=order)