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.

292 lines
9.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015-2017 Onestein (<http://www.onestein.eu>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, models
  5. NO_BI_MODELS = [
  6. 'temp.range',
  7. 'account.statement.operation.template',
  8. 'fetchmail.server'
  9. ]
  10. NO_BI_FIELDS = [
  11. 'id',
  12. 'create_uid',
  13. 'create_date',
  14. 'write_uid',
  15. 'write_date'
  16. ]
  17. NO_BI_TTYPES = [
  18. 'many2many',
  19. 'one2many',
  20. 'html',
  21. 'binary',
  22. 'reference'
  23. ]
  24. def dict_for_field(field):
  25. return {
  26. 'id': field.id,
  27. 'name': field.name,
  28. 'description': field.field_description,
  29. 'type': field.ttype,
  30. 'relation': field.relation,
  31. 'custom': False,
  32. 'model_id': field.model_id.id,
  33. 'model': field.model_id.model,
  34. 'model_name': field.model_id.name
  35. }
  36. def dict_for_model(model):
  37. return {
  38. 'id': model.id,
  39. 'name': model.name,
  40. 'model': model.model
  41. }
  42. class IrModel(models.Model):
  43. _inherit = 'ir.model'
  44. @api.model
  45. def _filter_bi_models(self, model):
  46. def _check_name(model_model):
  47. if model_model in NO_BI_MODELS:
  48. return 1
  49. return 0
  50. def _check_startswith(model_model):
  51. if model_model.startswith('workflow') or \
  52. model_model.startswith('ir.') or \
  53. model_model.startswith('base_'):
  54. return 1
  55. return 0
  56. def _check_contains(model_model):
  57. if 'mail' in model_model or \
  58. 'report' in model_model or \
  59. 'edi.' in model_model:
  60. return 1
  61. return 0
  62. def _check_unknow(model_name):
  63. if model_name == 'Unknow' or '.' in model_name:
  64. return 1
  65. return 0
  66. model_model = model['model']
  67. model_name = model['name']
  68. count_check = 0
  69. count_check += _check_name(model_model)
  70. count_check += _check_startswith(model_model)
  71. count_check += _check_contains(model_model)
  72. count_check += _check_unknow(model_name)
  73. if not count_check:
  74. return self.env['ir.model.access'].check(
  75. model['model'], 'read', False)
  76. return False
  77. @api.model
  78. def sort_filter_models(self, models_list):
  79. res = sorted(
  80. filter(self._filter_bi_models, models_list),
  81. key=lambda x: x['name'])
  82. return res
  83. @api.model
  84. def _search_fields(self, domain):
  85. Fields = self.env['ir.model.fields']
  86. fields = Fields.sudo().search(domain)
  87. return fields
  88. @api.model
  89. def get_related_fields(self, model_ids):
  90. """ Return list of field dicts for all fields that can be
  91. joined with models in model_ids
  92. """
  93. def get_model_list(model_ids):
  94. model_list = []
  95. domain = [('model_id', 'in', model_ids.values()),
  96. ('store', '=', True),
  97. ('ttype', 'in', ['many2one'])]
  98. filtered_fields = self._search_fields(domain)
  99. for model in model_ids.items():
  100. for field in filtered_fields:
  101. if model[1] == field.model_id.id:
  102. model_list.append(
  103. dict(dict_for_field(field),
  104. join_node=-1,
  105. table_alias=model[0])
  106. )
  107. return model_list
  108. def get_relation_list(model_ids, model_names):
  109. relation_list = []
  110. domain = [('relation', 'in', model_names.values()),
  111. ('store', '=', True),
  112. ('ttype', 'in', ['many2one'])]
  113. filtered_fields = self._search_fields(domain)
  114. for model in model_ids.items():
  115. for field in filtered_fields:
  116. if model_names[model[1]] == field['relation']:
  117. relation_list.append(
  118. dict(dict_for_field(field),
  119. join_node=model[0],
  120. table_alias=-1)
  121. )
  122. return relation_list
  123. models = self.sudo().browse(model_ids.values())
  124. model_names = {}
  125. for model in models:
  126. model_names.update({model.id: model.model})
  127. model_list = get_model_list(model_ids)
  128. relation_list = get_relation_list(model_ids, model_names)
  129. return relation_list + model_list
  130. @api.model
  131. def get_related_models(self, model_ids):
  132. """ Return list of model dicts for all models that can be
  133. joined with the already selected models.
  134. """
  135. def _get_field(fields, orig, target):
  136. field_list = []
  137. for f in fields:
  138. if f[orig] == -1:
  139. field_list.append(f[target])
  140. return field_list
  141. def _get_list_id(model_ids, fields):
  142. list_model = model_ids.values()
  143. list_model += _get_field(fields, 'table_alias', 'model_id')
  144. return list_model
  145. def _get_list_relation(fields):
  146. list_model = _get_field(fields, 'join_node', 'relation')
  147. return list_model
  148. models_list = []
  149. related_fields = self.get_related_fields(model_ids)
  150. list_id = _get_list_id(model_ids, related_fields)
  151. list_model = _get_list_relation(related_fields)
  152. domain = ['|',
  153. ('id', 'in', list_id),
  154. ('model', 'in', list_model)]
  155. for model in self.sudo().search(domain):
  156. models_list.append(dict_for_model(model))
  157. return self.sort_filter_models(models_list)
  158. @api.model
  159. def get_models(self):
  160. """ Return list of model dicts for all available models.
  161. """
  162. models_list = []
  163. for model in self.search([('transient', '=', False)]):
  164. models_list.append(dict_for_model(model))
  165. return self.sort_filter_models(models_list)
  166. @api.model
  167. def get_join_nodes(self, field_data, new_field):
  168. """ Return list of field dicts of join nodes
  169. Return all possible join nodes to add new_field to the query
  170. containing model_ids.
  171. """
  172. def _get_model_ids(field_data):
  173. model_ids = dict([(field['table_alias'],
  174. field['model_id']) for field in field_data])
  175. return model_ids
  176. def _get_join_nodes_dict(model_ids, new_field):
  177. join_nodes = []
  178. for alias, model_id in model_ids.items():
  179. if model_id == new_field['model_id']:
  180. join_nodes.append({'table_alias': alias})
  181. for field in self.get_related_fields(model_ids):
  182. c = [field['join_node'] == -1, field['table_alias'] == -1]
  183. a = (new_field['model'] == field['relation'])
  184. b = (new_field['model_id'] == field['model_id'])
  185. if (a and c[0]) or (b and c[1]):
  186. join_nodes.append(field)
  187. return join_nodes
  188. def remove_duplicate_nodes(join_nodes):
  189. seen = set()
  190. nodes_list = []
  191. for node in join_nodes:
  192. node_tuple = tuple(node.items())
  193. if node_tuple not in seen:
  194. seen.add(node_tuple)
  195. nodes_list.append(node)
  196. return nodes_list
  197. model_ids = _get_model_ids(field_data)
  198. keys = [(field['table_alias'], field['id'])
  199. for field in field_data if field.get('join_node', -1) != -1]
  200. join_nodes = _get_join_nodes_dict(model_ids, new_field)
  201. join_nodes = remove_duplicate_nodes(join_nodes)
  202. return filter(
  203. lambda x: 'id' not in x or
  204. (x['table_alias'], x['id']) not in keys, join_nodes)
  205. @api.model
  206. def get_fields(self, model_id):
  207. domain = [
  208. ('model_id', '=', model_id),
  209. ('store', '=', True),
  210. ('name', 'not in', NO_BI_FIELDS),
  211. ('ttype', 'not in', NO_BI_TTYPES)
  212. ]
  213. fields_dict = []
  214. filtered_fields = self._search_fields(domain)
  215. for field in filtered_fields:
  216. fields_dict.append(
  217. {'id': field.id,
  218. 'model_id': model_id,
  219. 'name': field.name,
  220. 'description': field.field_description,
  221. 'type': field.ttype,
  222. 'custom': False,
  223. 'model': field.model_id.model,
  224. 'model_name': field.model_id.name
  225. }
  226. )
  227. sorted_fields = sorted(
  228. fields_dict,
  229. key=lambda x: x['description'],
  230. reverse=True
  231. )
  232. return sorted_fields
  233. @api.model
  234. def create(self, vals):
  235. if self._context and self._context.get('bve'):
  236. vals['state'] = 'base'
  237. res = super(IrModel, self).create(vals)
  238. # this sql update is necessary since a write method here would
  239. # be not working (an orm constraint is restricting the modification
  240. # of the state field while updating ir.model)
  241. q = "UPDATE ir_model SET state = 'manual' WHERE id = %s"
  242. self.env.cr.execute(q, (res.id, ))
  243. # # update registry
  244. if self._context.get('bve'):
  245. # setup models; this reloads custom models in registry
  246. self.pool.setup_models(self._cr, partial=(not self.pool.ready))
  247. # signal that registry has changed
  248. self.pool.signal_registry_change()
  249. return res