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.

189 lines
7.9 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Search Panel
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import logging
  23. from odoo import _, api, fields, models
  24. from odoo.exceptions import UserError
  25. from odoo.osv import expression
  26. from odoo.tools import lazy
  27. _logger = logging.getLogger(__name__)
  28. class Base(models.AbstractModel):
  29. _inherit = 'base'
  30. #----------------------------------------------------------
  31. # Functions
  32. #----------------------------------------------------------
  33. @api.model
  34. def search_panel_select_range(self, field_name, **kwargs):
  35. """
  36. Return possible values of the field field_name (case select="one")
  37. and the parent field (if any) used to hierarchize them.
  38. :param field_name: the name of a many2one category field
  39. :return: {
  40. 'parent_field': parent field on the comodel of field, or False
  41. 'values': array of dictionaries containing some info on the records
  42. available on the comodel of the field 'field_name'.
  43. The display name (and possibly parent_field) are fetched.
  44. }
  45. """
  46. field = self._fields[field_name]
  47. supported_types = ['many2one']
  48. if field.type not in supported_types:
  49. raise UserError(_('Only types %(supported_types)s are supported for category (found type %(field_type)s)') % ({
  50. 'supported_types': supported_types, 'field_type': field.type}))
  51. Comodel = self.env[field.comodel_name]
  52. fields = ['display_name']
  53. parent_name = Comodel._parent_name if Comodel._parent_name in Comodel._fields else False
  54. if parent_name:
  55. fields.append(parent_name)
  56. return {
  57. 'parent_field': parent_name,
  58. 'values': Comodel.with_context(hierarchical_naming=False).search_read([], fields),
  59. }
  60. @api.model
  61. def search_panel_select_multi_range(self, field_name, **kwargs):
  62. """
  63. Return possible values of the field field_name (case select="multi"),
  64. possibly with counters and groups.
  65. :param field_name: the name of a filter field;
  66. possible types are many2one, many2many, selection.
  67. :param search_domain: base domain of search
  68. :param category_domain: domain generated by categories
  69. :param filter_domain: domain generated by filters
  70. :param comodel_domain: domain of field values (if relational)
  71. :param group_by: extra field to read on comodel, to group comodel records
  72. :param disable_counters: whether to count records by value
  73. :return: a list of possible values, each being a dict with keys
  74. 'id' (value),
  75. 'name' (value label),
  76. 'count' (how many records with that value),
  77. 'group_id' (value of group),
  78. 'group_name' (label of group).
  79. """
  80. field = self._fields[field_name]
  81. supported_types = ['many2one', 'many2many', 'selection']
  82. if field.type not in supported_types:
  83. raise UserError(_('Only types %(supported_types)s are supported for filter (found type %(field_type)s)') % ({
  84. 'supported_types': supported_types, 'field_type': field.type}))
  85. Comodel = self.env.get(field.comodel_name)
  86. model_domain = expression.AND([
  87. kwargs.get('search_domain', []),
  88. kwargs.get('category_domain', []),
  89. kwargs.get('filter_domain', []),
  90. [(field_name, '!=', False)],
  91. ])
  92. comodel_domain = kwargs.get('comodel_domain', [])
  93. disable_counters = kwargs.get('disable_counters', False)
  94. group_by = kwargs.get('group_by', False)
  95. if group_by:
  96. # determine the labeling of values returned by the group_by field
  97. group_by_field = Comodel._fields[group_by]
  98. if group_by_field.type == 'many2one':
  99. def group_id_name(value):
  100. return value or (False, _("Not Set"))
  101. elif group_by_field.type == 'selection':
  102. desc = Comodel.fields_get([group_by])[group_by]
  103. group_by_selection = dict(desc['selection'])
  104. group_by_selection[False] = _("Not Set")
  105. def group_id_name(value):
  106. return value, group_by_selection[value]
  107. else:
  108. def group_id_name(value):
  109. return (value, value) if value else (False, _("Not Set"))
  110. # get filter_values
  111. filter_values = []
  112. if field.type == 'many2one':
  113. counters = {}
  114. if not disable_counters:
  115. groups = self.read_group(model_domain, [field_name], [field_name])
  116. counters = {
  117. group[field_name][0]: group[field_name + '_count']
  118. for group in groups
  119. }
  120. # retrieve all possible values, and return them with their label and counter
  121. field_names = ['display_name', group_by] if group_by else ['display_name']
  122. records = Comodel.search_read(comodel_domain, field_names)
  123. for record in records:
  124. record_id = record['id']
  125. values = {
  126. 'id': record_id,
  127. 'name': record['display_name'],
  128. 'count': counters.get(record_id, 0),
  129. }
  130. if group_by:
  131. values['group_id'], values['group_name'] = group_id_name(record[group_by])
  132. filter_values.append(values)
  133. elif field.type == 'many2many':
  134. # retrieve all possible values, and return them with their label and counter
  135. field_names = ['display_name', group_by] if group_by else ['display_name']
  136. records = Comodel.search_read(comodel_domain, field_names)
  137. for record in records:
  138. record_id = record['id']
  139. values = {
  140. 'id': record_id,
  141. 'name': record['display_name'],
  142. 'count': 0,
  143. }
  144. if not disable_counters:
  145. count_domain = expression.AND([model_domain, [(field_name, 'in', record_id)]])
  146. values['count'] = self.search_count(count_domain)
  147. if group_by:
  148. values['group_id'], values['group_name'] = group_id_name(record[group_by])
  149. filter_values.append(values)
  150. elif field.type == 'selection':
  151. counters = {}
  152. if not disable_counters:
  153. groups = self.read_group(model_domain, [field_name], [field_name])
  154. counters = {
  155. group[field_name]: group[field_name + '_count']
  156. for group in groups
  157. }
  158. # retrieve all possible values, and return them with their label and counter
  159. selection = self.fields_get([field_name])[field_name]
  160. for value, label in selection:
  161. filter_values.append({
  162. 'id': value,
  163. 'name': label,
  164. 'count': counters.get(value, 0),
  165. })
  166. return filter_values