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.

575 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. self.env.cr.execute('CREATE or REPLACE VIEW %s as (%s)', (
  270. AsIs(view_name), AsIs(query), ))
  271. @api.depends('line_ids', 'state', 'over_condition')
  272. def _compute_sql_query(self):
  273. for bve_view in self:
  274. tables_map = {}
  275. select_str = '\n CAST(row_number() OVER ({}) as integer) AS id' \
  276. .format(bve_view.over_condition or '')
  277. for line in bve_view.field_ids:
  278. table = line.table_alias
  279. select = line.field_id.name
  280. as_name = line.name
  281. select_str += ',\n {}.{} AS {}'.format(table, select, as_name)
  282. if line.table_alias not in tables_map:
  283. table = self.env[line.field_id.model_id.model]._table
  284. tables_map[line.table_alias] = table
  285. seen = set()
  286. from_str = ""
  287. if not bve_view.relation_ids and bve_view.field_ids:
  288. first_line = bve_view.field_ids[0]
  289. table = tables_map[first_line.table_alias]
  290. from_str = "{} AS {}".format(table, first_line.table_alias)
  291. for line in bve_view.relation_ids:
  292. table = tables_map[line.table_alias]
  293. table_format = "{} AS {}".format(table, line.table_alias)
  294. if not from_str:
  295. from_str += table_format
  296. seen.add(line.table_alias)
  297. if line.table_alias not in seen:
  298. seen.add(line.table_alias)
  299. from_str += "\n"
  300. from_str += " LEFT" if line.left_join else ""
  301. from_str += " JOIN {} ON {}.id = {}.{}".format(
  302. table_format,
  303. line.join_node, line.table_alias, line.field_id.name
  304. )
  305. if line.join_node not in seen:
  306. from_str += "\n"
  307. seen.add(line.join_node)
  308. from_str += " LEFT" if line.left_join else ""
  309. from_str += " JOIN {} AS {} ON {}.{} = {}.id".format(
  310. tables_map[line.join_node], line.join_node,
  311. line.table_alias, line.field_id.name, line.join_node
  312. )
  313. bve_view.query = """SELECT %s\n\nFROM %s
  314. """ % (AsIs(select_str), AsIs(from_str),)
  315. def action_translations(self):
  316. self.ensure_one()
  317. if self.state != 'created':
  318. return
  319. self = self.sudo()
  320. model = self.env['ir.model'].search([('model', '=', self.model_name)])
  321. IrTranslation = self.env['ir.translation']
  322. IrTranslation.translate_fields('ir.model', model.id)
  323. for field in model.field_id:
  324. IrTranslation.translate_fields('ir.model.fields', field.id)
  325. return {
  326. 'name': 'Translations',
  327. 'res_model': 'ir.translation',
  328. 'type': 'ir.actions.act_window',
  329. 'view_mode': 'tree',
  330. 'view_id': self.env.ref('base.view_translation_dialog_tree').id,
  331. 'target': 'current',
  332. 'flags': {'search_view': True, 'action_buttons': True},
  333. 'domain': [
  334. '|',
  335. '&',
  336. ('res_id', 'in', model.field_id.ids),
  337. ('name', '=', 'ir.model.fields,field_description'),
  338. '&',
  339. ('res_id', '=', model.id),
  340. ('name', '=', 'ir.model,name')
  341. ],
  342. }
  343. def action_create(self):
  344. self.ensure_one()
  345. # consistency checks
  346. self._check_invalid_lines()
  347. self._check_groups_consistency()
  348. # force removal of dirty views in case something went wrong
  349. self.sudo().action_reset()
  350. # create sql view
  351. self._create_sql_view()
  352. # create model and fields
  353. bve_fields = self.line_ids.filtered(lambda l: not l.join_node)
  354. model = self.env['ir.model'].sudo().with_context(bve=True).create({
  355. 'name': self.name,
  356. 'model': self.model_name,
  357. 'state': 'manual',
  358. 'field_id': [(0, 0, f) for f in bve_fields._prepare_field_vals()],
  359. })
  360. # give access rights
  361. self._build_access_rules(model)
  362. # create tree, graph and pivot views
  363. self._create_bve_view()
  364. def _check_groups_consistency(self):
  365. self.ensure_one()
  366. if not self.group_ids:
  367. return
  368. for line_model in self.line_ids.mapped('model_id'):
  369. res_count = self.env['ir.model.access'].sudo().search([
  370. ('model_id', '=', line_model.id),
  371. ('perm_read', '=', True),
  372. '|',
  373. ('group_id', '=', False),
  374. ('group_id', 'in', self.group_ids.ids),
  375. ], limit=1)
  376. if not res_count:
  377. access_records = self.env['ir.model.access'].sudo().search([
  378. ('model_id', '=', line_model.id),
  379. ('perm_read', '=', True),
  380. ])
  381. group_list = ''
  382. for group in access_records.mapped('group_id'):
  383. group_list += ' * %s\n' % (group.full_name, )
  384. msg_title = _(
  385. 'The model "%s" cannot be accessed by users with the '
  386. 'selected groups only.' % (line_model.name, ))
  387. msg_details = _(
  388. 'At least one of the following groups must be added:')
  389. raise UserError(_(
  390. '%s\n\n%s\n%s' % (msg_title, msg_details, group_list,)
  391. ))
  392. def _check_invalid_lines(self):
  393. self.ensure_one()
  394. if not self.line_ids:
  395. raise ValidationError(_('No data to process.'))
  396. if any(not line.model_id for line in self.line_ids):
  397. invalid_lines = self.line_ids.filtered(lambda l: not l.model_id)
  398. missing_models = set(invalid_lines.mapped('model_name'))
  399. missing_models = ', '.join(missing_models)
  400. raise ValidationError(_(
  401. 'Following models are missing: %s.\n'
  402. 'Probably some modules were uninstalled.' % (missing_models,)
  403. ))
  404. if any(not line.field_id for line in self.line_ids):
  405. invalid_lines = self.line_ids.filtered(lambda l: not l.field_id)
  406. missing_fields = set(invalid_lines.mapped('field_name'))
  407. missing_fields = ', '.join(missing_fields)
  408. raise ValidationError(_(
  409. 'Following fields are missing: %s.' % (missing_fields,)
  410. ))
  411. def open_view(self):
  412. self.ensure_one()
  413. self._check_invalid_lines()
  414. [action] = self.action_id.read()
  415. action['display_name'] = _('BI View')
  416. return action
  417. @api.multi
  418. def copy(self, default=None):
  419. self.ensure_one()
  420. default = dict(default or {}, name=_("%s (copy)") % self.name)
  421. return super().copy(default=default)
  422. def action_reset(self):
  423. self.ensure_one()
  424. has_menus = False
  425. if self.action_id:
  426. action = 'ir.actions.act_window,%d' % (self.action_id.id,)
  427. menus = self.env['ir.ui.menu'].search([
  428. ('action', '=', action)
  429. ])
  430. has_menus = True if menus else False
  431. menus.unlink()
  432. if self.action_id.view_id:
  433. self.sudo().action_id.view_id.unlink()
  434. self.sudo().action_id.unlink()
  435. self.env['ir.ui.view'].sudo().search(
  436. [('model', '=', self.model_name)]).unlink()
  437. models_to_delete = self.env['ir.model'].sudo().search([
  438. ('model', '=', self.model_name)])
  439. if models_to_delete:
  440. models_to_delete.unlink()
  441. table_name = self.model_name.replace('.', '_')
  442. tools.drop_view_if_exists(self.env.cr, table_name)
  443. self.state = 'draft'
  444. if has_menus:
  445. return {'type': 'ir.actions.client', 'tag': 'reload'}
  446. def unlink(self):
  447. if self.filtered(lambda v: v.state == 'created'):
  448. raise UserError(
  449. _('You cannot delete a created view! '
  450. 'Reset the view to draft first.'))
  451. return super().unlink()
  452. @api.model
  453. def _sync_lines_and_data(self, data):
  454. line_ids = [(5, 0, 0)]
  455. fields_info = []
  456. if data:
  457. fields_info = json.loads(data)
  458. table_model_map = {}
  459. for item in fields_info:
  460. if item.get('join_node', -1) == -1:
  461. table_model_map[item['table_alias']] = item['model_id']
  462. for sequence, field_info in enumerate(fields_info, start=1):
  463. join_model_id = False
  464. join_node = field_info.get('join_node', -1)
  465. if join_node != -1 and table_model_map.get(join_node):
  466. join_model_id = int(table_model_map[join_node])
  467. line_ids += [(0, False, {
  468. 'sequence': sequence,
  469. 'model_id': field_info['model_id'],
  470. 'table_alias': field_info['table_alias'],
  471. 'description': field_info['description'],
  472. 'field_id': field_info['id'],
  473. 'ttype': field_info['type'],
  474. 'row': field_info['row'],
  475. 'column': field_info['column'],
  476. 'measure': field_info['measure'],
  477. 'in_list': field_info['list'],
  478. 'relation': field_info.get('relation'),
  479. 'join_node': field_info.get('join_node'),
  480. 'join_model_id': join_model_id,
  481. })]
  482. return line_ids
  483. @api.constrains('line_ids')
  484. def _constraint_line_ids(self):
  485. models_with_tables = self.env.registry.models.keys()
  486. for view in self:
  487. nodes = view.line_ids.filtered(lambda n: n.join_node)
  488. nodes_models = nodes.mapped('table_alias')
  489. nodes_models += nodes.mapped('join_node')
  490. not_nodes = view.line_ids.filtered(lambda n: not n.join_node)
  491. not_nodes_models = not_nodes.mapped('table_alias')
  492. err_msg = _('Inconsistent lines.')
  493. if set(nodes_models) - set(not_nodes_models):
  494. raise ValidationError(err_msg)
  495. if len(set(not_nodes_models) - set(nodes_models)) > 1:
  496. raise ValidationError(err_msg)
  497. models = view.line_ids.mapped('model_id')
  498. if models.filtered(lambda m: m.model not in models_with_tables):
  499. raise ValidationError(_('Abstract models not supported.'))
  500. @api.model
  501. def get_clean_list(self, data_dict):
  502. serialized_data = json.loads(data_dict)
  503. table_alias_list = set()
  504. for item in serialized_data:
  505. if item.get('join_node', -1) in [-1, False]:
  506. table_alias_list.add(item['table_alias'])
  507. for item in serialized_data:
  508. if item.get('join_node', -1) not in [-1, False]:
  509. if item['table_alias'] not in table_alias_list:
  510. serialized_data.remove(item)
  511. elif item['join_node'] not in table_alias_list:
  512. serialized_data.remove(item)
  513. return json.dumps(serialized_data)