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.

561 lines
21 KiB

  1. # Copyright 2015-2019 Onestein (<https://www.onestein.eu>)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. import base64
  4. import json
  5. import pydot
  6. from psycopg2.extensions import AsIs
  7. from odoo import _, api, fields, models, tools
  8. from odoo.exceptions import UserError, ValidationError
  9. class BveView(models.Model):
  10. _name = 'bve.view'
  11. _description = 'BI View Editor'
  12. @api.depends('group_ids', 'group_ids.users')
  13. def _compute_users(self):
  14. for bve_view in self.sudo():
  15. if bve_view.group_ids:
  16. bve_view.user_ids = bve_view.group_ids.mapped('users')
  17. else:
  18. bve_view.user_ids = self.env['res.users'].sudo().search([])
  19. @api.depends('name')
  20. def _compute_model_name(self):
  21. for bve_view in self:
  22. name = [x for x in bve_view.name.lower() if x.isalnum()]
  23. model_name = ''.join(name).replace('_', '.').replace(' ', '.')
  24. bve_view.model_name = 'x_bve.' + model_name
  25. def _compute_serialized_data(self):
  26. for bve_view in self:
  27. serialized_data = []
  28. for line in bve_view.line_ids.sorted(key=lambda r: r.sequence):
  29. serialized_data.append({
  30. 'sequence': line.sequence,
  31. 'model_id': line.model_id.id,
  32. 'id': line.field_id.id,
  33. 'name': line.name,
  34. 'model_name': line.model_id.name,
  35. 'model': line.model_id.model,
  36. 'type': line.ttype,
  37. 'table_alias': line.table_alias,
  38. 'description': line.description,
  39. 'row': line.row,
  40. 'column': line.column,
  41. 'measure': line.measure,
  42. 'list': line.in_list,
  43. 'join_node': line.join_node,
  44. 'relation': line.relation,
  45. })
  46. bve_view.data = json.dumps(serialized_data)
  47. def _inverse_serialized_data(self):
  48. for bve_view in self:
  49. line_ids = self._sync_lines_and_data(bve_view.data)
  50. bve_view.write({'line_ids': line_ids})
  51. name = fields.Char(required=True, copy=False)
  52. model_name = fields.Char(compute='_compute_model_name', store=True)
  53. note = fields.Text(string='Notes')
  54. state = fields.Selection([
  55. ('draft', 'Draft'),
  56. ('created', 'Created')
  57. ], default='draft', copy=False)
  58. data = fields.Char(
  59. compute='_compute_serialized_data',
  60. inverse='_inverse_serialized_data',
  61. help="Use the special query builder to define the query "
  62. "to generate your report dataset. "
  63. "NOTE: To be edited, the query should be in 'Draft' status.")
  64. line_ids = fields.One2many(
  65. 'bve.view.line',
  66. 'bve_view_id',
  67. string='Lines')
  68. field_ids = fields.One2many(
  69. 'bve.view.line',
  70. 'bve_view_id',
  71. domain=['|', ('join_node', '=', -1), ('join_node', '=', False)],
  72. string='Fields')
  73. relation_ids = fields.One2many(
  74. 'bve.view.line',
  75. 'bve_view_id',
  76. domain=[('join_node', '!=', -1), ('join_node', '!=', False)],
  77. string='Relations')
  78. action_id = fields.Many2one('ir.actions.act_window', string='Action')
  79. view_id = fields.Many2one('ir.ui.view', string='View')
  80. group_ids = fields.Many2many(
  81. 'res.groups',
  82. string='Groups',
  83. help="User groups allowed to see the generated report; "
  84. "if NO groups are specified the report will be public "
  85. "for everyone.")
  86. user_ids = fields.Many2many(
  87. 'res.users',
  88. string='Users',
  89. compute='_compute_users',
  90. store=True)
  91. query = fields.Text(compute='_compute_sql_query')
  92. er_diagram_image = fields.Binary(compute='_compute_er_diagram_image')
  93. _sql_constraints = [
  94. ('name_uniq',
  95. 'unique(name)',
  96. _('Custom BI View names must be unique!')),
  97. ]
  98. @api.depends('line_ids')
  99. def _compute_er_diagram_image(self):
  100. for bve_view in self:
  101. graph = pydot.Dot(graph_type='graph')
  102. table_model_map = {}
  103. for line in bve_view.field_ids:
  104. if line.table_alias not in table_model_map:
  105. table_alias_node = pydot.Node(
  106. line.model_id.name + ' ' + line.table_alias,
  107. style="filled",
  108. shape='box',
  109. fillcolor="#DDDDDD"
  110. )
  111. table_model_map[line.table_alias] = table_alias_node
  112. graph.add_node(table_model_map[line.table_alias])
  113. field_node = pydot.Node(
  114. line.table_alias + '.' + line.field_id.field_description,
  115. label=line.description,
  116. style="filled",
  117. fillcolor="green"
  118. )
  119. graph.add_node(field_node)
  120. graph.add_edge(pydot.Edge(
  121. table_model_map[line.table_alias],
  122. field_node
  123. ))
  124. for line in bve_view.relation_ids:
  125. field_description = line.field_id.field_description
  126. table_alias = line.table_alias
  127. diamond_node = pydot.Node(
  128. line.ttype + ' ' + table_alias + '.' + field_description,
  129. label=table_alias + '.' + field_description,
  130. style="filled",
  131. shape='diamond',
  132. fillcolor="#D2D2FF"
  133. )
  134. graph.add_node(diamond_node)
  135. graph.add_edge(pydot.Edge(
  136. table_model_map[table_alias],
  137. diamond_node,
  138. labelfontcolor="#D2D2FF",
  139. color="blue"
  140. ))
  141. graph.add_edge(pydot.Edge(
  142. diamond_node,
  143. table_model_map[line.join_node],
  144. labelfontcolor="black",
  145. color="blue"
  146. ))
  147. try:
  148. png_base64_image = base64.b64encode(graph.create_png())
  149. bve_view.er_diagram_image = png_base64_image
  150. except:
  151. bve_view.er_diagram_image = False
  152. def _create_view_arch(self):
  153. self.ensure_one()
  154. def _get_field_def(line):
  155. field_type = line.view_field_type
  156. return '<field name="%s" type="%s" />' % (line.name, field_type)
  157. bve_field_lines = self.field_ids.filtered('view_field_type')
  158. return list(map(_get_field_def, bve_field_lines))
  159. def _create_tree_view_arch(self):
  160. self.ensure_one()
  161. def _get_field_attrs(line):
  162. attr = line.list_attr
  163. res = attr and '%s="%s"' % (attr, line.description) or ''
  164. return '<field name="%s" %s />' % (line.name, res)
  165. bve_field_lines = self.field_ids.filtered(lambda l: l.in_list)
  166. return list(map(_get_field_attrs, bve_field_lines))
  167. def _create_bve_view(self):
  168. self.ensure_one()
  169. View = self.env['ir.ui.view'].sudo()
  170. # delete old views
  171. View.search([('model', '=', self.model_name)]).unlink()
  172. # create views
  173. View.create([{
  174. 'name': 'Pivot Analysis',
  175. 'type': 'pivot',
  176. 'model': self.model_name,
  177. 'priority': 16,
  178. 'arch': """<?xml version="1.0"?>
  179. <pivot string="Pivot Analysis">
  180. {}
  181. </pivot>
  182. """.format("".join(self._create_view_arch()))
  183. }, {
  184. 'name': 'Graph Analysis',
  185. 'type': 'graph',
  186. 'model': self.model_name,
  187. 'priority': 16,
  188. 'arch': """<?xml version="1.0"?>
  189. <graph string="Graph Analysis"
  190. type="bar" stacked="True">
  191. {}
  192. </graph>
  193. """.format("".join(self._create_view_arch()))
  194. }, {
  195. 'name': 'Search BI View',
  196. 'type': 'search',
  197. 'model': self.model_name,
  198. 'priority': 16,
  199. 'arch': """<?xml version="1.0"?>
  200. <search string="Search BI View">
  201. {}
  202. </search>
  203. """.format("".join(self._create_view_arch()))
  204. }])
  205. # create Tree view
  206. tree_view = View.create({
  207. 'name': 'Tree Analysis',
  208. 'type': 'tree',
  209. 'model': self.model_name,
  210. 'priority': 16,
  211. 'arch': """<?xml version="1.0"?>
  212. <tree string="List Analysis" create="false">
  213. {}
  214. </tree>
  215. """.format("".join(self._create_tree_view_arch()))
  216. })
  217. # set the Tree view as the default one
  218. action = self.env['ir.actions.act_window'].sudo().create({
  219. 'name': self.name,
  220. 'res_model': self.model_name,
  221. 'type': 'ir.actions.act_window',
  222. 'view_type': 'form',
  223. 'view_mode': 'tree,graph,pivot',
  224. 'view_id': tree_view.id,
  225. 'context': "{'service_name': '%s'}" % self.name,
  226. })
  227. self.write({
  228. 'action_id': action.id,
  229. 'view_id': tree_view.id,
  230. 'state': 'created'
  231. })
  232. def _build_access_rules(self, model):
  233. self.ensure_one()
  234. if not self.group_ids:
  235. self.env['ir.model.access'].sudo().create({
  236. 'name': 'read access to ' + self.model_name,
  237. 'model_id': model.id,
  238. 'perm_read': True,
  239. })
  240. else:
  241. # read access only to model
  242. access_vals = [{
  243. 'name': 'read access to ' + self.model_name,
  244. 'model_id': model.id,
  245. 'group_id': group.id,
  246. 'perm_read': True
  247. } for group in self.group_ids]
  248. self.env['ir.model.access'].sudo().create(access_vals)
  249. def _create_sql_view(self):
  250. self.ensure_one()
  251. view_name = self.model_name.replace('.', '_')
  252. query = self.query and self.query.replace('\n', ' ')
  253. # robustness in case something went wrong
  254. self._cr.execute('DROP TABLE IF EXISTS %s', (AsIs(view_name), ))
  255. # create postgres view
  256. self.env.cr.execute('CREATE or REPLACE VIEW %s as (%s)', (
  257. AsIs(view_name), AsIs(query), ))
  258. @api.depends('line_ids', 'state')
  259. def _compute_sql_query(self):
  260. for bve_view in self:
  261. tables_map = {}
  262. select_str = '\n CAST(row_number() OVER () as integer) AS id'
  263. for line in bve_view.field_ids:
  264. table = line.table_alias
  265. select = line.field_id.name
  266. as_name = line.name
  267. select_str += ',\n {}.{} AS {}'.format(table, select, as_name)
  268. if line.table_alias not in tables_map:
  269. table = self.env[line.field_id.model_id.model]._table
  270. tables_map[line.table_alias] = table
  271. seen = set()
  272. from_str = ""
  273. if not bve_view.relation_ids and bve_view.field_ids:
  274. first_line = bve_view.field_ids[0]
  275. table = tables_map[first_line.table_alias]
  276. from_str = "{} AS {}".format(table, first_line.table_alias)
  277. for line in bve_view.relation_ids:
  278. table = tables_map[line.table_alias]
  279. table_format = "{} AS {}".format(table, line.table_alias)
  280. if not from_str:
  281. from_str += table_format
  282. seen.add(line.table_alias)
  283. if line.table_alias not in seen:
  284. seen.add(line.table_alias)
  285. from_str += "\n"
  286. from_str += " LEFT" if line.left_join else ""
  287. from_str += " JOIN {} ON {}.id = {}.{}".format(
  288. table_format,
  289. line.join_node, line.table_alias, line.field_id.name
  290. )
  291. if line.join_node not in seen:
  292. from_str += "\n"
  293. seen.add(line.join_node)
  294. from_str += " LEFT" if line.left_join else ""
  295. from_str += " JOIN {} AS {} ON {}.{} = {}.id".format(
  296. tables_map[line.join_node], line.join_node,
  297. line.table_alias, line.field_id.name, line.join_node
  298. )
  299. bve_view.query = """SELECT %s\n\nFROM %s
  300. """ % (AsIs(select_str), AsIs(from_str),)
  301. def action_translations(self):
  302. self.ensure_one()
  303. if self.state != 'created':
  304. return
  305. self = self.sudo()
  306. model = self.env['ir.model'].search([('model', '=', self.model_name)])
  307. IrTranslation = self.env['ir.translation']
  308. IrTranslation.translate_fields('ir.model', model.id)
  309. for field in model.field_id:
  310. IrTranslation.translate_fields('ir.model.fields', field.id)
  311. return {
  312. 'name': 'Translations',
  313. 'res_model': 'ir.translation',
  314. 'type': 'ir.actions.act_window',
  315. 'view_mode': 'tree',
  316. 'view_id': self.env.ref('base.view_translation_dialog_tree').id,
  317. 'target': 'current',
  318. 'flags': {'search_view': True, 'action_buttons': True},
  319. 'domain': [
  320. '|',
  321. '&',
  322. ('res_id', 'in', model.field_id.ids),
  323. ('name', '=', 'ir.model.fields,field_description'),
  324. '&',
  325. ('res_id', '=', model.id),
  326. ('name', '=', 'ir.model,name')
  327. ],
  328. }
  329. def action_create(self):
  330. self.ensure_one()
  331. # consistency checks
  332. self._check_invalid_lines()
  333. self._check_groups_consistency()
  334. # force removal of dirty views in case something went wrong
  335. self.sudo().action_reset()
  336. # create sql view
  337. self._create_sql_view()
  338. # create model and fields
  339. bve_fields = self.line_ids.filtered(lambda l: not l.join_node)
  340. model = self.env['ir.model'].sudo().with_context(bve=True).create({
  341. 'name': self.name,
  342. 'model': self.model_name,
  343. 'state': 'manual',
  344. 'field_id': [(0, 0, f) for f in bve_fields._prepare_field_vals()],
  345. })
  346. # give access rights
  347. self._build_access_rules(model)
  348. # create tree, graph and pivot views
  349. self._create_bve_view()
  350. def _check_groups_consistency(self):
  351. self.ensure_one()
  352. if not self.group_ids:
  353. return
  354. for line_model in self.line_ids.mapped('model_id'):
  355. res_count = self.env['ir.model.access'].sudo().search([
  356. ('model_id', '=', line_model.id),
  357. ('perm_read', '=', True),
  358. '|',
  359. ('group_id', '=', False),
  360. ('group_id', 'in', self.group_ids.ids),
  361. ], limit=1)
  362. if not res_count:
  363. access_records = self.env['ir.model.access'].sudo().search([
  364. ('model_id', '=', line_model.id),
  365. ('perm_read', '=', True),
  366. ])
  367. group_list = ''
  368. for group in access_records.mapped('group_id'):
  369. group_list += ' * %s\n' % (group.full_name, )
  370. msg_title = _(
  371. 'The model "%s" cannot be accessed by users with the '
  372. 'selected groups only.' % (line_model.name, ))
  373. msg_details = _(
  374. 'At least one of the following groups must be added:')
  375. raise UserError(_(
  376. '%s\n\n%s\n%s' % (msg_title, msg_details, group_list,)
  377. ))
  378. def _check_invalid_lines(self):
  379. self.ensure_one()
  380. if not self.line_ids:
  381. raise ValidationError(_('No data to process.'))
  382. if any(not line.model_id for line in self.line_ids):
  383. invalid_lines = self.line_ids.filtered(lambda l: not l.model_id)
  384. missing_models = set(invalid_lines.mapped('model_name'))
  385. missing_models = ', '.join(missing_models)
  386. raise ValidationError(_(
  387. 'Following models are missing: %s.\n'
  388. 'Probably some modules were uninstalled.' % (missing_models,)
  389. ))
  390. if any(not line.field_id for line in self.line_ids):
  391. invalid_lines = self.line_ids.filtered(lambda l: not l.field_id)
  392. missing_fields = set(invalid_lines.mapped('field_name'))
  393. missing_fields = ', '.join(missing_fields)
  394. raise ValidationError(_(
  395. 'Following fields are missing: %s.' % (missing_fields,)
  396. ))
  397. def open_view(self):
  398. self.ensure_one()
  399. self._check_invalid_lines()
  400. [action] = self.action_id.read()
  401. action['display_name'] = _('BI View')
  402. return action
  403. @api.multi
  404. def copy(self, default=None):
  405. self.ensure_one()
  406. default = dict(default or {}, name=_("%s (copy)") % self.name)
  407. return super().copy(default=default)
  408. def action_reset(self):
  409. self.ensure_one()
  410. has_menus = False
  411. if self.action_id:
  412. action = 'ir.actions.act_window,%d' % (self.action_id.id,)
  413. menus = self.env['ir.ui.menu'].search([
  414. ('action', '=', action)
  415. ])
  416. has_menus = True if menus else False
  417. menus.unlink()
  418. if self.action_id.view_id:
  419. self.sudo().action_id.view_id.unlink()
  420. self.sudo().action_id.unlink()
  421. self.env['ir.ui.view'].sudo().search(
  422. [('model', '=', self.model_name)]).unlink()
  423. models_to_delete = self.env['ir.model'].sudo().search([
  424. ('model', '=', self.model_name)])
  425. if models_to_delete:
  426. models_to_delete.unlink()
  427. table_name = self.model_name.replace('.', '_')
  428. tools.drop_view_if_exists(self.env.cr, table_name)
  429. self.state = 'draft'
  430. if has_menus:
  431. return {'type': 'ir.actions.client', 'tag': 'reload'}
  432. def unlink(self):
  433. if self.filtered(lambda v: v.state == 'created'):
  434. raise UserError(
  435. _('You cannot delete a created view! '
  436. 'Reset the view to draft first.'))
  437. return super().unlink()
  438. @api.model
  439. def _sync_lines_and_data(self, data):
  440. line_ids = [(5, 0, 0)]
  441. fields_info = []
  442. if data:
  443. fields_info = json.loads(data)
  444. table_model_map = {}
  445. for item in fields_info:
  446. if item.get('join_node', -1) == -1:
  447. table_model_map[item['table_alias']] = item['model_id']
  448. for sequence, field_info in enumerate(fields_info, start=1):
  449. join_model_id = False
  450. join_node = field_info.get('join_node', -1)
  451. if join_node != -1 and table_model_map.get(join_node):
  452. join_model_id = int(table_model_map[join_node])
  453. line_ids += [(0, False, {
  454. 'sequence': sequence,
  455. 'model_id': field_info['model_id'],
  456. 'table_alias': field_info['table_alias'],
  457. 'description': field_info['description'],
  458. 'field_id': field_info['id'],
  459. 'ttype': field_info['type'],
  460. 'row': field_info['row'],
  461. 'column': field_info['column'],
  462. 'measure': field_info['measure'],
  463. 'in_list': field_info['list'],
  464. 'relation': field_info.get('relation'),
  465. 'join_node': field_info.get('join_node'),
  466. 'join_model_id': join_model_id,
  467. })]
  468. return line_ids
  469. @api.constrains('line_ids')
  470. def _constraint_line_ids(self):
  471. models_with_tables = self.env.registry.models.keys()
  472. for view in self:
  473. nodes = view.line_ids.filtered(lambda n: n.join_node)
  474. nodes_models = nodes.mapped('table_alias')
  475. nodes_models += nodes.mapped('join_node')
  476. not_nodes = view.line_ids.filtered(lambda n: not n.join_node)
  477. not_nodes_models = not_nodes.mapped('table_alias')
  478. err_msg = _('Inconsistent lines.')
  479. if set(nodes_models) - set(not_nodes_models):
  480. raise ValidationError(err_msg)
  481. if len(set(not_nodes_models) - set(nodes_models)) > 1:
  482. raise ValidationError(err_msg)
  483. models = view.line_ids.mapped('model_id')
  484. if models.filtered(lambda m: m.model not in models_with_tables):
  485. raise ValidationError(_('Abstract models not supported.'))
  486. @api.model
  487. def get_clean_list(self, data_dict):
  488. serialized_data = json.loads(data_dict)
  489. table_alias_list = set()
  490. for item in serialized_data:
  491. if item.get('join_node', -1) in [-1, False]:
  492. table_alias_list.add(item['table_alias'])
  493. for item in serialized_data:
  494. if item.get('join_node', -1) not in [-1, False]:
  495. if item['table_alias'] not in table_alias_list:
  496. serialized_data.remove(item)
  497. elif item['join_node'] not in table_alias_list:
  498. serialized_data.remove(item)
  499. return json.dumps(serialized_data)