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.

116 lines
4.4 KiB

  1. # Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
  2. # Copyright 2017 Pedro M. Baeza <pedro.baeza@tecnativa.com>
  3. # License LGPL-3 - See http://www.gnu.org/licenses/lgpl-3.0.html
  4. from odoo import _, api, fields, models
  5. from odoo.exceptions import UserError, ValidationError
  6. class CustomInfoProperty(models.Model):
  7. """Name of the custom information property."""
  8. _description = "Custom information property"
  9. _name = "custom.info.property"
  10. _order = "template_id, category_sequence, category_id, sequence, id"
  11. _sql_constraints = [
  12. ("name_template",
  13. "UNIQUE (name, template_id)",
  14. "Another property with that name exists for that template."),
  15. ]
  16. name = fields.Char(required=True, translate=True)
  17. sequence = fields.Integer(index=True)
  18. category_id = fields.Many2one(
  19. comodel_name="custom.info.category",
  20. string="Category",
  21. )
  22. category_sequence = fields.Integer(
  23. string="Category Sequence",
  24. related="category_id.sequence",
  25. store=True,
  26. readonly=True,
  27. )
  28. template_id = fields.Many2one(
  29. comodel_name='custom.info.template', string='Template',
  30. required=True, ondelete="cascade",
  31. )
  32. model = fields.Char(
  33. related="template_id.model", readonly=True, auto_join=True,
  34. )
  35. info_value_ids = fields.One2many(
  36. comodel_name="custom.info.value",
  37. inverse_name="property_id",
  38. string="Property Values")
  39. default_value = fields.Char(
  40. translate=True,
  41. help="Will be applied by default to all custom values of this "
  42. "property. This is a char field, so you have to enter some value "
  43. "that can be converted to the field type you choose.",
  44. )
  45. required = fields.Boolean()
  46. minimum = fields.Float(
  47. help="For numeric fields, it means the minimum possible value; "
  48. "for text fields, it means the minimum possible length. "
  49. "If it is bigger than the maximum, then this check is skipped",
  50. )
  51. maximum = fields.Float(
  52. default=-1,
  53. help="For numeric fields, it means the maximum possible value; "
  54. "for text fields, it means the maximum possible length. "
  55. "If it is smaller than the minimum, then this check is skipped",
  56. )
  57. field_type = fields.Selection(
  58. selection=[
  59. ("str", "Text"),
  60. ("int", "Whole number"),
  61. ("float", "Decimal number"),
  62. ("bool", "Yes/No"),
  63. ("id", "Selection"),
  64. ],
  65. default="str",
  66. required=True,
  67. help="Type of information that can be stored in the property.",
  68. )
  69. option_ids = fields.Many2many(
  70. comodel_name="custom.info.option",
  71. string="Options",
  72. help="When the field type is 'selection', choose the available "
  73. "options here.",
  74. )
  75. @api.multi
  76. def check_access_rule(self, operation):
  77. """You access a property if you access its template."""
  78. self.mapped("template_id").check_access_rule(operation)
  79. return super().check_access_rule(operation)
  80. def _check_default_value_one(self):
  81. if self.default_value:
  82. try:
  83. self.env["custom.info.value"]._transform_value(
  84. self.default_value, self.field_type, self)
  85. except ValueError:
  86. selection = dict(
  87. self._fields["field_type"].get_description(self.env)
  88. ["selection"])
  89. raise ValidationError(
  90. _("Default value %s cannot be converted to type %s.") %
  91. (self.default_value, selection[self.field_type]))
  92. @api.constrains("default_value", "field_type")
  93. def _check_default_value(self):
  94. """Ensure the default value is valid."""
  95. for rec in self:
  96. rec._check_default_value_one()
  97. @api.multi
  98. @api.onchange("required", "field_type")
  99. def _onchange_required_warn(self):
  100. """Warn if the required flag implies a possible weird behavior."""
  101. if self.required:
  102. if self.field_type == "bool":
  103. raise UserError(
  104. _("If you require a Yes/No field, you can only set Yes."))
  105. if self.field_type in {"int", "float"}:
  106. raise UserError(
  107. _("If you require a numeric field, you cannot set it to "
  108. "zero."))