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.

77 lines
2.7 KiB

  1. # Copyright 2004-2010 Tiny SPRL http://tiny.be
  2. # Copyright 2010-2012 ChriCar Beteiligungs- und Beratungs- GmbH
  3. # http://www.camptocamp.at
  4. # Copyright 2015 Antiun Ingenieria, SL (Madrid, Spain)
  5. # http://www.antiun.com
  6. # Antonio Espinosa <antonioea@antiun.com>
  7. # Copyright 2016 ACSONE SA/NV (<http://acsone.eu>)
  8. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  9. from odoo import _, fields, models
  10. from odoo.exceptions import UserError, ValidationError
  11. from odoo.tools.safe_eval import safe_eval
  12. class ResPartnerIdCategory(models.Model):
  13. _name = "res.partner.id_category"
  14. _description = "Partner ID Category"
  15. _order = "name"
  16. code = fields.Char(
  17. string="Code",
  18. size=16,
  19. required=True,
  20. help="Abbreviation or acronym of this ID type. For example, "
  21. "'driver_license'",
  22. )
  23. name = fields.Char(
  24. string="ID name",
  25. required=True,
  26. translate=True,
  27. help="Name of this ID type. For example, 'Driver License'",
  28. )
  29. active = fields.Boolean(string="Active", default=True)
  30. validation_code = fields.Text(
  31. "Python validation code",
  32. help="Python code called to validate an id number.",
  33. default=lambda self: self._default_validation_code(),
  34. )
  35. def _default_validation_code(self):
  36. return _(
  37. "\n# Python code. Use failed = True to specify that the id "
  38. "number is not valid.\n"
  39. "# You can use the following variables :\n"
  40. "# - self: browse_record of the current ID Category "
  41. "browse_record\n"
  42. "# - id_number: browse_record of ID number to validate"
  43. )
  44. def _validation_eval_context(self, id_number):
  45. self.ensure_one()
  46. return {"self": self, "id_number": id_number}
  47. def validate_id_number(self, id_number):
  48. """Validate the given ID number
  49. The method raises an odoo.exceptions.ValidationError if the eval of
  50. python validation code fails
  51. """
  52. self.ensure_one()
  53. if self.env.context.get("id_no_validate"):
  54. return
  55. eval_context = self._validation_eval_context(id_number)
  56. try:
  57. safe_eval(self.validation_code, eval_context, mode="exec", nocopy=True)
  58. except Exception as e:
  59. raise UserError(
  60. _(
  61. "Error when evaluating the id_category validation code:"
  62. ":\n %s \n(%s)"
  63. )
  64. % (self.name, e)
  65. )
  66. if eval_context.get("failed", False):
  67. raise ValidationError(
  68. _("%s is not a valid %s identifier") % (id_number.name, self.name)
  69. )