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.

232 lines
7.6 KiB

7 years ago
  1. # Copyright (C) 2017 - Today: GRAP (http://www.grap.coop)
  2. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import re
  5. from odoo import _, api, fields, models
  6. from odoo.exceptions import UserError
  7. class BiSQLViewField(models.Model):
  8. _name = 'bi.sql.view.field'
  9. _order = 'sequence'
  10. _TTYPE_SELECTION = [
  11. ('boolean', 'boolean'),
  12. ('char', 'char'),
  13. ('date', 'date'),
  14. ('datetime', 'datetime'),
  15. ('float', 'float'),
  16. ('integer', 'integer'),
  17. ('many2one', 'many2one'),
  18. ('selection', 'selection'),
  19. ]
  20. _GRAPH_TYPE_SELECTION = [
  21. ('col', 'Column'),
  22. ('row', 'Row'),
  23. ('measure', 'Measure'),
  24. ]
  25. _TREE_VISIBILITY_SELECTION = [
  26. ('unavailable', 'Unavailable'),
  27. ('hidden', 'Hidden'),
  28. ('available', 'Available'),
  29. ]
  30. # Mapping to guess Odoo field type, from SQL column type
  31. _SQL_MAPPING = {
  32. 'boolean': 'boolean',
  33. 'bigint': 'integer',
  34. 'integer': 'integer',
  35. 'double precision': 'float',
  36. 'numeric': 'float',
  37. 'text': 'char',
  38. 'character varying': 'char',
  39. 'date': 'datetime',
  40. 'timestamp without time zone': 'datetime',
  41. }
  42. name = fields.Char(string='Name', required=True, readonly=True)
  43. sql_type = fields.Char(
  44. string='SQL Type', required=True, readonly=True,
  45. help="SQL Type in the database")
  46. sequence = fields.Integer(string='sequence', required=True, readonly=True)
  47. bi_sql_view_id = fields.Many2one(
  48. string='SQL View', comodel_name='bi.sql.view', ondelete='cascade')
  49. is_index = fields.Boolean(
  50. string='Is Index', help="Check this box if you want to create"
  51. " an index on that field. This is recommended for searchable and"
  52. " groupable fields, to reduce duration")
  53. is_group_by = fields.Boolean(
  54. string='Is Group by', help="Check this box if you want to create"
  55. " a 'group by' option in the search view")
  56. index_name = fields.Char(
  57. string='Index Name', compute='_compute_index_name')
  58. graph_type = fields.Selection(
  59. string='Graph Type', selection=_GRAPH_TYPE_SELECTION)
  60. tree_visibility = fields.Selection(
  61. string='Tree Visibility', selection=_TREE_VISIBILITY_SELECTION,
  62. default='available', required=True)
  63. field_description = fields.Char(
  64. string='Field Description', help="This will be used as the name"
  65. " of the Odoo field, displayed for users")
  66. ttype = fields.Selection(
  67. string='Field Type', selection=_TTYPE_SELECTION, help="Type of the"
  68. " Odoo field that will be created. Keep empty if you don't want to"
  69. " create a new field. If empty, this field will not be displayed"
  70. " neither available for search or group by function")
  71. selection = fields.Text(
  72. string='Selection Options', default='[]',
  73. help="For 'Selection' Odoo field.\n"
  74. " List of options, specified as a Python expression defining a list of"
  75. " (key, label) pairs. For example:"
  76. " [('blue','Blue'), ('yellow','Yellow')]")
  77. many2one_model_id = fields.Many2one(
  78. comodel_name='ir.model', string='Model',
  79. help="For 'Many2one' Odoo field.\n"
  80. " Comodel of the field.")
  81. # Constrains Section
  82. @api.constrains('is_index')
  83. @api.multi
  84. def _check_index_materialized(self):
  85. for rec in self.filtered(lambda x: x.is_index):
  86. if not rec.bi_sql_view_id.is_materialized:
  87. raise UserError(_(
  88. 'You can not create indexes on non materialized views'))
  89. # Compute Section
  90. @api.multi
  91. def _compute_index_name(self):
  92. for sql_field in self:
  93. sql_field.index_name = '%s_%s' % (
  94. sql_field.bi_sql_view_id.view_name, sql_field.name)
  95. # Overload Section
  96. @api.multi
  97. def create(self, vals):
  98. field_without_prefix = vals['name'][2:]
  99. # guess field description
  100. field_description = re.sub(
  101. r'\w+', lambda m: m.group(0).capitalize(),
  102. field_without_prefix.replace('_id', '').replace('_', ' '))
  103. # Guess ttype
  104. # Don't execute as simple .get() in the dict to manage
  105. # correctly the type 'character varying(x)'
  106. ttype = False
  107. for k, v in self._SQL_MAPPING.items():
  108. if k in vals['sql_type']:
  109. ttype = v
  110. # Guess many2one_model_id
  111. many2one_model_id = False
  112. if vals['sql_type'] == 'integer' and(
  113. vals['name'][-3:] == '_id'):
  114. ttype = 'many2one'
  115. model_name = self._model_mapping().get(field_without_prefix, '')
  116. many2one_model_id = self.env['ir.model'].search(
  117. [('model', '=', model_name)]).id
  118. vals.update({
  119. 'ttype': ttype,
  120. 'field_description': field_description,
  121. 'many2one_model_id': many2one_model_id,
  122. })
  123. return super(BiSQLViewField, self).create(vals)
  124. # Custom Section
  125. @api.model
  126. def _model_mapping(self):
  127. """Return dict of key value, to try to guess the model based on a
  128. field name. Sample :
  129. {'account_id': 'account.account'; 'product_id': 'product.product'}
  130. """
  131. relation_fields = self.env['ir.model.fields'].search([
  132. ('ttype', '=', 'many2one')])
  133. res = {}
  134. keys_to_pop = []
  135. for field in relation_fields:
  136. if field.name in res and res.get(field.name) != field.relation:
  137. # The field name is not predictive
  138. keys_to_pop.append(field.name)
  139. else:
  140. res.update({field.name: field.relation})
  141. for key in list(set(keys_to_pop)):
  142. res.pop(key)
  143. return res
  144. @api.multi
  145. def _prepare_model_field(self):
  146. self.ensure_one()
  147. return {
  148. 'name': self.name,
  149. 'field_description': self.field_description,
  150. 'model_id': self.bi_sql_view_id.model_id.id,
  151. 'ttype': self.ttype,
  152. 'selection': self.ttype == 'selection' and self.selection or False,
  153. 'relation': self.ttype == 'many2one' and
  154. self.many2one_model_id.model or False,
  155. }
  156. @api.multi
  157. def _prepare_tree_field(self):
  158. self.ensure_one()
  159. res = ''
  160. if self.field_description and self.tree_visibility != 'unavailable':
  161. res = """<field name="{}" {}/>""".format(
  162. self.name,
  163. self.tree_visibility == 'hidden' and 'invisible="1"' or '')
  164. return res
  165. @api.multi
  166. def _prepare_graph_field(self):
  167. self.ensure_one()
  168. res = ''
  169. if self.graph_type and self.field_description:
  170. res = """<field name="{}" type="{}" />""".format(
  171. self.name, self.graph_type)
  172. return res
  173. @api.multi
  174. def _prepare_pivot_field(self):
  175. self.ensure_one()
  176. res = ''
  177. if self.graph_type and self.field_description:
  178. res = """<field name="{}" type="{}" />""".format(
  179. self.name, self.graph_type)
  180. return res
  181. @api.multi
  182. def _prepare_search_field(self):
  183. self.ensure_one()
  184. res = ''
  185. if self.field_description:
  186. res = """<field name="{}"/>""".format(self.name)
  187. return res
  188. @api.multi
  189. def _prepare_search_filter_field(self):
  190. self.ensure_one()
  191. res = ''
  192. if self.field_description and self.is_group_by:
  193. res =\
  194. """<filter string="%s" context="{'group_by':'%s'}"/>""" % (
  195. self.field_description, self.name)
  196. return res