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.

133 lines
4.1 KiB

  1. # Copyright 2017-2018 Onestein (<http://www.onestein.eu>)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. import logging
  4. from odoo import _, api, models, tools
  5. from odoo.exceptions import UserError
  6. _logger = logging.getLogger(__name__)
  7. @api.model
  8. def _bi_view(_name):
  9. return _name[0:6] == 'x_bve.'
  10. @api.model_cr_context
  11. def _auto_init(self):
  12. """ Initialize the database schema of ``self``:
  13. - create the corresponding table,
  14. - create/update the necessary columns/tables for fields,
  15. - initialize new columns on existing rows,
  16. - add the SQL constraints given on the model,
  17. - add the indexes on indexed fields,
  18. Also prepare post-init stuff to:
  19. - add foreign key constraints,
  20. - reflect models, fields, relations and constraints,
  21. - mark fields to recompute on existing records.
  22. Note: you should not override this method. Instead, you can modify
  23. the model's database schema by overriding method :meth:`~.init`,
  24. which is called right after this one.
  25. """
  26. # This monkey patch is meant to fix an error (probably
  27. # introduced by https://github.com/odoo/odoo/pull/15412), while
  28. # running an update all. The _auto_init() method invoked during
  29. # an update all is the one of BaseModel, and not the one of Base.
  30. # START OF patch
  31. # TODO: find better ways to do this patch
  32. if _bi_view(self._name):
  33. return
  34. # END of patch
  35. models.raise_on_invalid_object_name(self._name)
  36. # This prevents anything called by this method (in particular default
  37. # values) from prefetching a field for which the corresponding column
  38. # has not been added in database yet!
  39. self = self.with_context(prefetch_fields=False)
  40. self.pool.post_init(self._reflect)
  41. cr = self._cr
  42. parent_store_compute = False
  43. update_custom_fields = self._context.get('update_custom_fields', False)
  44. must_create_table = not tools.table_exists(cr, self._table)
  45. if self._auto:
  46. if must_create_table:
  47. tools.create_model_table(cr, self._table, self._description)
  48. if self._parent_store:
  49. if not tools.column_exists(cr, self._table, 'parent_left'):
  50. self._create_parent_columns()
  51. parent_store_compute = True
  52. self._check_removed_columns(log=False)
  53. # update the database schema for fields
  54. columns = tools.table_columns(cr, self._table)
  55. def recompute(field):
  56. _logger.info("Storing computed values of %s", field)
  57. recs = self.with_context(active_test=False).search([])
  58. recs._recompute_todo(field)
  59. for field in self._fields.values():
  60. if not field.store:
  61. continue
  62. if field.manual and not update_custom_fields:
  63. continue # don't update custom fields
  64. new = field.update_db(self, columns)
  65. if new and field.compute:
  66. self.pool.post_init(recompute, field)
  67. if self._auto:
  68. self._add_sql_constraints()
  69. if must_create_table:
  70. self._execute_sql()
  71. if parent_store_compute:
  72. self._parent_store_compute()
  73. models.BaseModel._auto_init = _auto_init
  74. class Base(models.AbstractModel):
  75. _inherit = 'base'
  76. @api.model
  77. def _auto_init(self):
  78. if not _bi_view(self._name):
  79. super(Base, self)._auto_init()
  80. @api.model
  81. def _setup_complete(self):
  82. if not _bi_view(self._name):
  83. super(Base, self)._setup_complete()
  84. else:
  85. self.pool.models[self._name]._log_access = False
  86. @api.model
  87. def _read_group_process_groupby(self, gb, query):
  88. if not _bi_view(self._name):
  89. return super(Base, self)._read_group_process_groupby(gb, query)
  90. split = gb.split(':')
  91. if split[0] not in self._fields:
  92. raise UserError(
  93. _('No data to be displayed.'))
  94. return super(Base, self)._read_group_process_groupby(gb, query)
  95. @api.model
  96. def _add_magic_fields(self):
  97. if _bi_view(self._name):
  98. self._log_access = False
  99. return super(Base, self)._add_magic_fields()