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.

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