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.

65 lines
2.2 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", help="Python code called to validate an id number."
  32. )
  33. def _validation_eval_context(self, id_number):
  34. self.ensure_one()
  35. return {"self": self, "id_number": id_number}
  36. def validate_id_number(self, id_number):
  37. """Validate the given ID number
  38. The method raises an odoo.exceptions.ValidationError if the eval of
  39. python validation code fails
  40. """
  41. self.ensure_one()
  42. if self.env.context.get("id_no_validate") or not self.validation_code:
  43. return
  44. eval_context = self._validation_eval_context(id_number)
  45. try:
  46. safe_eval(self.validation_code, eval_context, mode="exec", nocopy=True)
  47. except Exception as e:
  48. raise UserError(
  49. _(
  50. "Error when evaluating the id_category validation code:"
  51. ":\n %s \n(%s)"
  52. )
  53. % (self.name, e)
  54. )
  55. if eval_context.get("failed", False):
  56. raise ValidationError(
  57. _("%s is not a valid %s identifier") % (id_number.name, self.name)
  58. )