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.

583 lines
22 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. over_condition = fields.Text(
  93. states={
  94. 'draft': [
  95. ('readonly', False),
  96. ],
  97. },
  98. readonly=True,
  99. help="Condition to be inserted in the OVER part "
  100. "of the ID's row_number function.\n"
  101. "For instance, 'ORDER BY t1.id' would create "
  102. "IDs ordered in the same way as t1's IDs; otherwise "
  103. "IDs are assigned with no specific order.",
  104. )
  105. er_diagram_image = fields.Binary(compute='_compute_er_diagram_image')
  106. _sql_constraints = [
  107. ('name_uniq',
  108. 'unique(name)',
  109. _('Custom BI View names must be unique!')),
  110. ]
  111. @api.depends('line_ids')
  112. def _compute_er_diagram_image(self):
  113. for bve_view in self:
  114. graph = pydot.Dot(graph_type='graph')
  115. table_model_map = {}
  116. for line in bve_view.field_ids:
  117. if line.table_alias not in table_model_map:
  118. table_alias_node = pydot.Node(
  119. line.model_id.name + ' ' + line.table_alias,
  120. style="filled",
  121. shape='box',
  122. fillcolor="#DDDDDD"
  123. )
  124. table_model_map[line.table_alias] = table_alias_node
  125. graph.add_node(table_model_map[line.table_alias])
  126. field_node = pydot.Node(
  127. line.table_alias + '.' + line.field_id.field_description,
  128. label=line.description,
  129. style="filled",
  130. fillcolor="green"
  131. )
  132. graph.add_node(field_node)
  133. graph.add_edge(pydot.Edge(
  134. table_model_map[line.table_alias],
  135. field_node
  136. ))
  137. for line in bve_view.relation_ids:
  138. field_description = line.field_id.field_description
  139. table_alias = line.table_alias
  140. diamond_node = pydot.Node(
  141. line.ttype + ' ' + table_alias + '.' + field_description,
  142. label=table_alias + '.' + field_description,
  143. style="filled",
  144. shape='diamond',
  145. fillcolor="#D2D2FF"
  146. )
  147. graph.add_node(diamond_node)
  148. graph.add_edge(pydot.Edge(
  149. table_model_map[table_alias],
  150. diamond_node,
  151. labelfontcolor="#D2D2FF",
  152. color="blue"
  153. ))
  154. graph.add_edge(pydot.Edge(
  155. diamond_node,
  156. table_model_map[line.join_node],
  157. labelfontcolor="black",
  158. color="blue"
  159. ))
  160. try:
  161. png_base64_image = base64.b64encode(graph.create_png())
  162. bve_view.er_diagram_image = png_base64_image
  163. except:
  164. bve_view.er_diagram_image = False
  165. def _create_view_arch(self):
  166. self.ensure_one()
  167. def _get_field_def(line):
  168. field_type = line.view_field_type
  169. return '<field name="%s" type="%s" />' % (line.name, field_type)
  170. bve_field_lines = self.field_ids.filtered('view_field_type')
  171. return list(map(_get_field_def, bve_field_lines))
  172. def _create_tree_view_arch(self):
  173. self.ensure_one()
  174. def _get_field_attrs(line):
  175. attr = line.list_attr
  176. res = attr and '%s="%s"' % (attr, line.description) or ''
  177. return '<field name="%s" %s />' % (line.name, res)
  178. bve_field_lines = self.field_ids.filtered(lambda l: l.in_list)
  179. return list(map(_get_field_attrs, bve_field_lines))
  180. def _create_bve_view(self):
  181. self.ensure_one()
  182. View = self.env['ir.ui.view'].sudo()
  183. # delete old views
  184. View.search([('model', '=', self.model_name)]).unlink()
  185. # create views
  186. View.create([{
  187. 'name': 'Pivot Analysis',
  188. 'type': 'pivot',
  189. 'model': self.model_name,
  190. 'priority': 16,
  191. 'arch': """<?xml version="1.0"?>
  192. <pivot string="Pivot Analysis">
  193. {}
  194. </pivot>
  195. """.format("".join(self._create_view_arch()))
  196. }, {
  197. 'name': 'Graph Analysis',
  198. 'type': 'graph',
  199. 'model': self.model_name,
  200. 'priority': 16,
  201. 'arch': """<?xml version="1.0"?>
  202. <graph string="Graph Analysis"
  203. type="bar" stacked="True">
  204. {}
  205. </graph>
  206. """.format("".join(self._create_view_arch()))
  207. }, {
  208. 'name': 'Search BI View',
  209. 'type': 'search',
  210. 'model': self.model_name,
  211. 'priority': 16,
  212. 'arch': """<?xml version="1.0"?>
  213. <search string="Search BI View">
  214. {}
  215. </search>
  216. """.format("".join(self._create_view_arch()))
  217. }])
  218. # create Tree view
  219. tree_view = View.create({
  220. 'name': 'Tree Analysis',
  221. 'type': 'tree',
  222. 'model': self.model_name,
  223. 'priority': 16,
  224. 'arch': """<?xml version="1.0"?>
  225. <tree string="List Analysis" create="false">
  226. {}
  227. </tree>
  228. """.format("".join(self._create_tree_view_arch()))
  229. })
  230. # set the Tree view as the default one
  231. action = self.env['ir.actions.act_window'].sudo().create({
  232. 'name': self.name,
  233. 'res_model': self.model_name,
  234. 'type': 'ir.actions.act_window',
  235. 'view_type': 'form',
  236. 'view_mode': 'tree,graph,pivot',
  237. 'view_id': tree_view.id,
  238. 'context': "{'service_name': '%s'}" % self.name,
  239. })
  240. self.write({
  241. 'action_id': action.id,
  242. 'view_id': tree_view.id,
  243. 'state': 'created'
  244. })
  245. def _build_access_rules(self, model):
  246. self.ensure_one()
  247. if not self.group_ids:
  248. self.env['ir.model.access'].sudo().create({
  249. 'name': 'read access to ' + self.model_name,
  250. 'model_id': model.id,
  251. 'perm_read': True,
  252. })
  253. else:
  254. # read access only to model
  255. access_vals = [{
  256. 'name': 'read access to ' + self.model_name,
  257. 'model_id': model.id,
  258. 'group_id': group.id,
  259. 'perm_read': True
  260. } for group in self.group_ids]
  261. self.env['ir.model.access'].sudo().create(access_vals)
  262. def _create_sql_view(self):
  263. self.ensure_one()
  264. view_name = self.model_name.replace('.', '_')
  265. query = self.query and self.query.replace('\n', ' ')
  266. # robustness in case something went wrong
  267. self._cr.execute('DROP TABLE IF EXISTS %s', (AsIs(view_name), ))
  268. # create postgres view
  269. try:
  270. with self.env.cr.savepoint():
  271. self.env.cr.execute('CREATE or REPLACE VIEW %s as (%s)', (
  272. AsIs(view_name), AsIs(query), ))
  273. except Exception as e:
  274. raise UserError(
  275. _("Error creating the view '{query}':\n{error}")
  276. .format(
  277. query=query,
  278. error=e))
  279. @api.depends('line_ids', 'state', 'over_condition')
  280. def _compute_sql_query(self):
  281. for bve_view in self:
  282. tables_map = {}
  283. select_str = '\n CAST(row_number() OVER ({}) as integer) AS id' \
  284. .format(bve_view.over_condition or '')
  285. for line in bve_view.field_ids:
  286. table = line.table_alias
  287. select = line.field_id.name
  288. as_name = line.name
  289. select_str += ',\n {}.{} AS {}'.format(table, select, as_name)
  290. if line.table_alias not in tables_map:
  291. table = self.env[line.field_id.model_id.model]._table
  292. tables_map[line.table_alias] = table
  293. seen = set()
  294. from_str = ""
  295. if not bve_view.relation_ids and bve_view.field_ids:
  296. first_line = bve_view.field_ids[0]
  297. table = tables_map[first_line.table_alias]
  298. from_str = "{} AS {}".format(table, first_line.table_alias)
  299. for line in bve_view.relation_ids:
  300. table = tables_map[line.table_alias]
  301. table_format = "{} AS {}".format(table, line.table_alias)
  302. if not from_str:
  303. from_str += table_format
  304. seen.add(line.table_alias)
  305. if line.table_alias not in seen:
  306. seen.add(line.table_alias)
  307. from_str += "\n"
  308. from_str += " LEFT" if line.left_join else ""
  309. from_str += " JOIN {} ON {}.id = {}.{}".format(
  310. table_format,
  311. line.join_node, line.table_alias, line.field_id.name
  312. )
  313. if line.join_node not in seen:
  314. from_str += "\n"
  315. seen.add(line.join_node)
  316. from_str += " LEFT" if line.left_join else ""
  317. from_str += " JOIN {} AS {} ON {}.{} = {}.id".format(
  318. tables_map[line.join_node], line.join_node,
  319. line.table_alias, line.field_id.name, line.join_node
  320. )
  321. bve_view.query = """SELECT %s\n\nFROM %s
  322. """ % (AsIs(select_str), AsIs(from_str),)
  323. def action_translations(self):
  324. self.ensure_one()
  325. if self.state != 'created':
  326. return
  327. self = self.sudo()
  328. model = self.env['ir.model'].search([('model', '=', self.model_name)])
  329. IrTranslation = self.env['ir.translation']
  330. IrTranslation.translate_fields('ir.model', model.id)
  331. for field in model.field_id:
  332. IrTranslation.translate_fields('ir.model.fields', field.id)
  333. return {
  334. 'name': 'Translations',
  335. 'res_model': 'ir.translation',
  336. 'type': 'ir.actions.act_window',
  337. 'view_mode': 'tree',
  338. 'view_id': self.env.ref('base.view_translation_dialog_tree').id,
  339. 'target': 'current',
  340. 'flags': {'search_view': True, 'action_buttons': True},
  341. 'domain': [
  342. '|',
  343. '&',
  344. ('res_id', 'in', model.field_id.ids),
  345. ('name', '=', 'ir.model.fields,field_description'),
  346. '&',
  347. ('res_id', '=', model.id),
  348. ('name', '=', 'ir.model,name')
  349. ],
  350. }
  351. def action_create(self):
  352. self.ensure_one()
  353. # consistency checks
  354. self._check_invalid_lines()
  355. self._check_groups_consistency()
  356. # force removal of dirty views in case something went wrong
  357. self.sudo().action_reset()
  358. # create sql view
  359. self._create_sql_view()
  360. # create model and fields
  361. bve_fields = self.line_ids.filtered(lambda l: not l.join_node)
  362. model = self.env['ir.model'].sudo().with_context(bve=True).create({
  363. 'name': self.name,
  364. 'model': self.model_name,
  365. 'state': 'manual',
  366. 'field_id': [(0, 0, f) for f in bve_fields._prepare_field_vals()],
  367. })
  368. # give access rights
  369. self._build_access_rules(model)
  370. # create tree, graph and pivot views
  371. self._create_bve_view()
  372. def _check_groups_consistency(self):
  373. self.ensure_one()
  374. if not self.group_ids:
  375. return
  376. for line_model in self.line_ids.mapped('model_id'):
  377. res_count = self.env['ir.model.access'].sudo().search([
  378. ('model_id', '=', line_model.id),
  379. ('perm_read', '=', True),
  380. '|',
  381. ('group_id', '=', False),
  382. ('group_id', 'in', self.group_ids.ids),
  383. ], limit=1)
  384. if not res_count:
  385. access_records = self.env['ir.model.access'].sudo().search([
  386. ('model_id', '=', line_model.id),
  387. ('perm_read', '=', True),
  388. ])
  389. group_list = ''
  390. for group in access_records.mapped('group_id'):
  391. group_list += ' * %s\n' % (group.full_name, )
  392. msg_title = _(
  393. 'The model "%s" cannot be accessed by users with the '
  394. 'selected groups only.' % (line_model.name, ))
  395. msg_details = _(
  396. 'At least one of the following groups must be added:')
  397. raise UserError(_(
  398. '%s\n\n%s\n%s' % (msg_title, msg_details, group_list,)
  399. ))
  400. def _check_invalid_lines(self):
  401. self.ensure_one()
  402. if not self.line_ids:
  403. raise ValidationError(_('No data to process.'))
  404. if any(not line.model_id for line in self.line_ids):
  405. invalid_lines = self.line_ids.filtered(lambda l: not l.model_id)
  406. missing_models = set(invalid_lines.mapped('model_name'))
  407. missing_models = ', '.join(missing_models)
  408. raise ValidationError(_(
  409. 'Following models are missing: %s.\n'
  410. 'Probably some modules were uninstalled.' % (missing_models,)
  411. ))
  412. if any(not line.field_id for line in self.line_ids):
  413. invalid_lines = self.line_ids.filtered(lambda l: not l.field_id)
  414. missing_fields = set(invalid_lines.mapped('field_name'))
  415. missing_fields = ', '.join(missing_fields)
  416. raise ValidationError(_(
  417. 'Following fields are missing: %s.' % (missing_fields,)
  418. ))
  419. def open_view(self):
  420. self.ensure_one()
  421. self._check_invalid_lines()
  422. [action] = self.action_id.read()
  423. action['display_name'] = _('BI View')
  424. return action
  425. @api.multi
  426. def copy(self, default=None):
  427. self.ensure_one()
  428. default = dict(default or {}, name=_("%s (copy)") % self.name)
  429. return super().copy(default=default)
  430. def action_reset(self):
  431. self.ensure_one()
  432. has_menus = False
  433. if self.action_id:
  434. action = 'ir.actions.act_window,%d' % (self.action_id.id,)
  435. menus = self.env['ir.ui.menu'].search([
  436. ('action', '=', action)
  437. ])
  438. has_menus = True if menus else False
  439. menus.unlink()
  440. if self.action_id.view_id:
  441. self.sudo().action_id.view_id.unlink()
  442. self.sudo().action_id.unlink()
  443. self.env['ir.ui.view'].sudo().search(
  444. [('model', '=', self.model_name)]).unlink()
  445. models_to_delete = self.env['ir.model'].sudo().search([
  446. ('model', '=', self.model_name)])
  447. if models_to_delete:
  448. models_to_delete.unlink()
  449. table_name = self.model_name.replace('.', '_')
  450. tools.drop_view_if_exists(self.env.cr, table_name)
  451. self.state = 'draft'
  452. if has_menus:
  453. return {'type': 'ir.actions.client', 'tag': 'reload'}
  454. def unlink(self):
  455. if self.filtered(lambda v: v.state == 'created'):
  456. raise UserError(
  457. _('You cannot delete a created view! '
  458. 'Reset the view to draft first.'))
  459. return super().unlink()
  460. @api.model
  461. def _sync_lines_and_data(self, data):
  462. line_ids = [(5, 0, 0)]
  463. fields_info = []
  464. if data:
  465. fields_info = json.loads(data)
  466. table_model_map = {}
  467. for item in fields_info:
  468. if item.get('join_node', -1) == -1:
  469. table_model_map[item['table_alias']] = item['model_id']
  470. for sequence, field_info in enumerate(fields_info, start=1):
  471. join_model_id = False
  472. join_node = field_info.get('join_node', -1)
  473. if join_node != -1 and table_model_map.get(join_node):
  474. join_model_id = int(table_model_map[join_node])
  475. line_ids += [(0, False, {
  476. 'sequence': sequence,
  477. 'model_id': field_info['model_id'],
  478. 'table_alias': field_info['table_alias'],
  479. 'description': field_info['description'],
  480. 'field_id': field_info['id'],
  481. 'ttype': field_info['type'],
  482. 'row': field_info['row'],
  483. 'column': field_info['column'],
  484. 'measure': field_info['measure'],
  485. 'in_list': field_info['list'],
  486. 'relation': field_info.get('relation'),
  487. 'join_node': field_info.get('join_node'),
  488. 'join_model_id': join_model_id,
  489. })]
  490. return line_ids
  491. @api.constrains('line_ids')
  492. def _constraint_line_ids(self):
  493. models_with_tables = self.env.registry.models.keys()
  494. for view in self:
  495. nodes = view.line_ids.filtered(lambda n: n.join_node)
  496. nodes_models = nodes.mapped('table_alias')
  497. nodes_models += nodes.mapped('join_node')
  498. not_nodes = view.line_ids.filtered(lambda n: not n.join_node)
  499. not_nodes_models = not_nodes.mapped('table_alias')
  500. err_msg = _('Inconsistent lines.')
  501. if set(nodes_models) - set(not_nodes_models):
  502. raise ValidationError(err_msg)
  503. if len(set(not_nodes_models) - set(nodes_models)) > 1:
  504. raise ValidationError(err_msg)
  505. models = view.line_ids.mapped('model_id')
  506. if models.filtered(lambda m: m.model not in models_with_tables):
  507. raise ValidationError(_('Abstract models not supported.'))
  508. @api.model
  509. def get_clean_list(self, data_dict):
  510. serialized_data = json.loads(data_dict)
  511. table_alias_list = set()
  512. for item in serialized_data:
  513. if item.get('join_node', -1) in [-1, False]:
  514. table_alias_list.add(item['table_alias'])
  515. for item in serialized_data:
  516. if item.get('join_node', -1) not in [-1, False]:
  517. if item['table_alias'] not in table_alias_list:
  518. serialized_data.remove(item)
  519. elif item['join_node'] not in table_alias_list:
  520. serialized_data.remove(item)
  521. return json.dumps(serialized_data)