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.

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