OCA reporting engine fork for dev and update.
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.

217 lines
7.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. # Copyright 2015-2019 Onestein (<https://www.onestein.eu>)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from collections import defaultdict
  4. from odoo import api, models
  5. NO_BI_MODELS = ["fetchmail.server"]
  6. NO_BI_TTYPES = ["many2many", "one2many", "html", "binary", "reference"]
  7. def dict_for_field(field):
  8. return {
  9. "id": field.id,
  10. "name": field.name,
  11. "description": field.field_description,
  12. "type": field.ttype,
  13. "relation": field.relation,
  14. "custom": False,
  15. "model_id": field.model_id.id,
  16. "model": field.model_id.model,
  17. "model_name": field.model_id.name,
  18. }
  19. def dict_for_model(model):
  20. return {"id": model.id, "name": model.name, "model": model.model}
  21. class IrModel(models.Model):
  22. _inherit = "ir.model"
  23. @api.model
  24. def _filter_bi_models(self, model):
  25. def _check_name(model_model):
  26. if model_model in NO_BI_MODELS:
  27. return 1
  28. return 0
  29. def _check_startswith(model_model):
  30. if (
  31. model_model.startswith("workflow")
  32. or model_model.startswith("ir.")
  33. or model_model.startswith("base_")
  34. ):
  35. return 1
  36. return 0
  37. def _check_contains(model_model):
  38. if (
  39. "mail" in model_model
  40. or "report" in model_model
  41. or "edi." in model_model
  42. ):
  43. return 1
  44. return 0
  45. def _check_unknown(model_name):
  46. if model_name == "Unknown" or "." in model_name:
  47. return 1
  48. return 0
  49. model_model = model["model"]
  50. model_name = model["name"]
  51. count_check = 0
  52. count_check += _check_name(model_model)
  53. count_check += _check_startswith(model_model)
  54. count_check += _check_contains(model_model)
  55. count_check += _check_unknown(model_name)
  56. if not count_check:
  57. return self.env["ir.model.access"].check(model["model"], "read", False)
  58. return False
  59. def get_model_list(self, model_table_map):
  60. if not model_table_map:
  61. return []
  62. domain = [
  63. ("model_id", "in", list(model_table_map.keys())),
  64. ("store", "=", True),
  65. ("ttype", "=", "many2one"),
  66. ]
  67. fields = self.env["ir.model.fields"].sudo().search(domain)
  68. model_list = []
  69. for field in fields:
  70. for table_alias in model_table_map[field.model_id.id]:
  71. model_list.append(
  72. dict(
  73. dict_for_field(field),
  74. table_alias=table_alias,
  75. join_node=-1,
  76. )
  77. )
  78. return model_list
  79. def get_relation_list(self, model_table_map):
  80. if not model_table_map:
  81. return []
  82. model_names = {}
  83. for model in self.sudo().browse(model_table_map.keys()):
  84. model_names.update({model.model: model.id})
  85. domain = [
  86. ("relation", "in", list(model_names.keys())),
  87. ("store", "=", True),
  88. ("ttype", "=", "many2one"),
  89. ]
  90. fields = self.env["ir.model.fields"].sudo().search(domain)
  91. relation_list = []
  92. for field in fields:
  93. model_id = model_names[field.relation]
  94. for join_node in model_table_map[model_id]:
  95. relation_list.append(
  96. dict(dict_for_field(field), join_node=join_node, table_alias=-1)
  97. )
  98. return relation_list
  99. @api.model
  100. def _get_related_models_domain(self, model_table_map):
  101. domain = [("transient", "=", False)]
  102. if model_table_map:
  103. model_list = self.get_model_list(model_table_map)
  104. relation_list = self.get_relation_list(model_table_map)
  105. model_ids = [f["model_id"] for f in relation_list + model_list]
  106. model_ids += list(model_table_map.keys())
  107. relations = [f["relation"] for f in model_list]
  108. domain += ["|", ("id", "in", model_ids), ("model", "in", relations)]
  109. return domain
  110. @api.model
  111. def get_related_models(self, model_table_map):
  112. """Return list of model dicts for all models that can be
  113. joined with the already selected models.
  114. """
  115. domain = self._get_related_models_domain(model_table_map)
  116. return self.sudo().search(domain, order="name asc")
  117. @api.model
  118. def get_models(self, table_model_map=None):
  119. """Return list of model dicts for all available models."""
  120. self = self.with_context(lang=self.env.user.lang)
  121. model_table_map = defaultdict(list)
  122. for k, v in (table_model_map or {}).items():
  123. model_table_map[v].append(k)
  124. models = self.get_related_models(model_table_map)
  125. # filter out abstract models (they do not have DB tables)
  126. non_abstract_models = self.env.registry.models.keys()
  127. models = models.filtered(lambda m: m.model in non_abstract_models)
  128. return list(map(dict_for_model, models))
  129. @api.model
  130. def get_join_nodes(self, field_data, new_field):
  131. """Return list of field dicts of join nodes
  132. Return all possible join nodes to add new_field to the query
  133. containing model_ids.
  134. """
  135. def remove_duplicate_nodes(join_nodes):
  136. seen = set()
  137. nodes_list = []
  138. for node in join_nodes:
  139. node_tuple = tuple(node.items())
  140. if node_tuple not in seen:
  141. seen.add(node_tuple)
  142. nodes_list.append(node)
  143. return nodes_list
  144. self = self.with_context(lang=self.env.user.lang)
  145. keys = []
  146. model_table_map = defaultdict(list)
  147. for field in field_data:
  148. model_table_map[field["model_id"]].append(field["table_alias"])
  149. if field.get("join_node", -1) != -1:
  150. keys.append((field["table_alias"], field["id"]))
  151. # nodes in current model
  152. existing_aliases = model_table_map[new_field["model_id"]]
  153. join_nodes = [{"table_alias": alias} for alias in existing_aliases]
  154. # nodes in past selected models
  155. for field in self.get_model_list(model_table_map):
  156. if new_field["model"] == field["relation"]:
  157. if (field["table_alias"], field["id"]) not in keys:
  158. join_nodes.append(field)
  159. # nodes in new model
  160. for field in self.get_relation_list(model_table_map):
  161. if new_field["model_id"] == field["model_id"]:
  162. if (field["table_alias"], field["id"]) not in keys:
  163. join_nodes.append(field)
  164. return remove_duplicate_nodes(join_nodes)
  165. @api.model
  166. def get_fields(self, model_id):
  167. self = self.with_context(lang=self.env.user.lang)
  168. fields = (
  169. self.env["ir.model.fields"]
  170. .sudo()
  171. .search(
  172. [
  173. ("model_id", "=", model_id),
  174. ("store", "=", True),
  175. ("name", "not in", models.MAGIC_COLUMNS),
  176. ("ttype", "not in", NO_BI_TTYPES),
  177. ],
  178. order="field_description desc",
  179. )
  180. )
  181. fields_dict = list(map(dict_for_field, fields))
  182. return fields_dict