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.

176 lines
8.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. @api.model
  30. def _check_parent_field(self):
  31. if self._parent_name not in self._fields:
  32. raise TypeError("The parent (%s) field does not exist." % self._parent_name)
  33. @api.model
  34. def _build_search_childs_domain(self, parent_id, domain=[]):
  35. self._check_parent_field()
  36. parent_domain = [[self._parent_name, '=', parent_id]]
  37. return expression.AND([parent_domain, domain]) if domain else parent_domain
  38. @api.model
  39. def _check_context_bin_size(self, field):
  40. return any(key in self.env.context for key in ['bin_size', 'bin_size_%s' % (field)])
  41. #----------------------------------------------------------
  42. # Security
  43. #----------------------------------------------------------
  44. @api.multi
  45. def _filter_access(self, operation):
  46. if self.check_access_rights(operation, False):
  47. return self._filter_access_rules(operation)
  48. return self.env[self._name]
  49. @api.multi
  50. def _filter_access_ids(self, operation):
  51. return self._filter_access(operation).ids
  52. @api.multi
  53. def check_access(self, operation, raise_exception=False):
  54. try:
  55. access_right = self.check_access_rights(operation, raise_exception)
  56. access_rule = self.check_access_rule(operation) is None
  57. return access_right and access_rule
  58. except AccessError:
  59. if raise_exception:
  60. raise
  61. return False
  62. #----------------------------------------------------------
  63. # Hierarchy Methods
  64. #----------------------------------------------------------
  65. @api.model
  66. def search_parents(self, domain=[], order=None):
  67. """ This method finds the top level elements of the hierarchy for a given search query.
  68. :param domain: a search domain <reference/orm/domains> (default: empty list)
  69. :param order: a string to define the sort order of the query (default: none)
  70. :returns: the top level elements for the given search query
  71. """
  72. return self.browse(self._search_parents(domain=domain, order=order))
  73. @api.model
  74. def search_read_parents(self, domain=[], fields=None, order=None):
  75. """ This method finds the top level elements of the hierarchy for a given search query.
  76. :param domain: a search domain <reference/orm/domains> (default: empty list)
  77. :param fields: a list of fields to read (default: all fields of the model)
  78. :param order: a string to define the sort order of the query (default: none)
  79. :returns: the top level elements for the given search query
  80. """
  81. records = self.search_parents(domain=domain, order=order)
  82. if not records:
  83. return []
  84. if fields and fields == ['id']:
  85. return [{'id': record.id} for record in records]
  86. result = records.read(fields)
  87. if len(result) <= 1:
  88. return result
  89. index = {vals['id']: vals for vals in result}
  90. return [index[record.id] for record in records if record.id in index]
  91. @api.model
  92. def _search_parents(self, domain=[], order=None):
  93. self._check_parent_field()
  94. self.check_access_rights('read')
  95. if expression.is_false(self, domain):
  96. return []
  97. query = self._where_calc(domain)
  98. self._apply_ir_rules(query, 'read')
  99. from_clause, where_clause, where_clause_arguments = query.get_sql()
  100. parent_where = where_clause and (" WHERE %s" % where_clause) or ''
  101. parent_query = 'SELECT "%s".id FROM ' % self._table + from_clause + parent_where
  102. no_parent_clause ='"{table}"."{field}" IS NULL'.format(
  103. table=self._table,
  104. field=self._parent_name
  105. )
  106. no_access_clause ='"{table}"."{field}" NOT IN ({query})'.format(
  107. table=self._table,
  108. field=self._parent_name,
  109. query=parent_query
  110. )
  111. parent_clause = '({0} OR {1})'.format(
  112. no_parent_clause,
  113. no_access_clause
  114. )
  115. order_by = self._generate_order_by(order, query)
  116. from_clause, where_clause, where_clause_params = query.get_sql()
  117. where_str = (
  118. where_clause and
  119. (" WHERE %s AND %s" % (where_clause, parent_clause)) or
  120. (" WHERE %s" % parent_clause)
  121. )
  122. query_str = 'SELECT "%s".id FROM ' % self._table + from_clause + where_str + order_by
  123. complete_where_clause_params = where_clause_params + where_clause_arguments
  124. self._cr.execute(query_str, complete_where_clause_params)
  125. return utils.uniquify_list([x[0] for x in self._cr.fetchall()])
  126. @api.model
  127. def search_childs(self, parent_id, domain=[], offset=0, limit=None, order=None, count=False):
  128. """ This method finds the direct child elements of the parent record for a given search query.
  129. :param parent_id: the integer representing the ID of the parent record
  130. :param domain: a search domain <reference/orm/domains> (default: empty list)
  131. :param offset: the number of results to ignore (default: none)
  132. :param limit: maximum number of records to return (default: all)
  133. :param order: a string to define the sort order of the query (default: none)
  134. :param count: counts and returns the number of matching records (default: False)
  135. :returns: the top level elements for the given search query
  136. """
  137. domain = self._build_search_childs_domain(parent_id, domain=domain)
  138. return self.search(domain, offset=offset, limit=limit, order=order, count=count)
  139. @api.model
  140. def search_read_childs(self, parent_id, domain=[], fields=None, offset=0, limit=None, order=None):
  141. """ This method finds the direct child elements of the parent record for a given search query.
  142. :param parent_id: the integer representing the ID of the parent record
  143. :param domain: a search domain <reference/orm/domains> (default: empty list)
  144. :param fields: a list of fields to read (default: all fields of the model)
  145. :param offset: the number of results to ignore (default: none)
  146. :param limit: maximum number of records to return (default: all)
  147. :param order: a string to define the sort order of the query (default: none)
  148. :returns: the top level elements for the given search query
  149. """
  150. domain = self._build_search_childs_domain(parent_id, domain=domain)
  151. return self.search_read(domain=domain, fields=fields, offset=offset, limit=limit, order=order)