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.2 KiB

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