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.

113 lines
4.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
  3. # Copyright 2017 Pedro M. Baeza <pedro.baeza@tecnativa.com>
  4. # License LGPL-3 - See http://www.gnu.org/licenses/lgpl-3.0.html
  5. from openerp import _, api, fields, models
  6. from openerp.exceptions import UserError, ValidationError
  7. class CustomInfoProperty(models.Model):
  8. """Name of the custom information property."""
  9. _description = "Custom information property"
  10. _name = "custom.info.property"
  11. _order = "template_id, category_sequence, category_id, sequence, id"
  12. _sql_constraints = [
  13. ("name_template",
  14. "UNIQUE (name, template_id)",
  15. "Another property with that name exists for that template."),
  16. ]
  17. name = fields.Char(required=True, translate=True)
  18. sequence = fields.Integer(index=True)
  19. category_id = fields.Many2one(
  20. comodel_name="custom.info.category",
  21. string="Category",
  22. )
  23. category_sequence = fields.Integer(
  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(CustomInfoProperty, self).check_access_rule(operation)
  80. @api.one
  81. @api.constrains("default_value", "field_type")
  82. def _check_default_value(self):
  83. """Ensure the default value is valid."""
  84. if self.default_value:
  85. try:
  86. self.env["custom.info.value"]._transform_value(
  87. self.default_value, self.field_type, self)
  88. except ValueError:
  89. selection = dict(
  90. self._fields["field_type"].get_description(self.env)
  91. ["selection"])
  92. raise ValidationError(
  93. _("Default value %s cannot be converted to type %s.") %
  94. (self.default_value, selection[self.field_type]))
  95. @api.multi
  96. @api.onchange("required", "field_type")
  97. def _onchange_required_warn(self):
  98. """Warn if the required flag implies a possible weird behavior."""
  99. if self.required:
  100. if self.field_type == "bool":
  101. raise UserError(
  102. _("If you require a Yes/No field, you can only set Yes."))
  103. if self.field_type in {"int", "float"}:
  104. raise UserError(
  105. _("If you require a numeric field, you cannot set it to "
  106. "zero."))