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.

71 lines
2.4 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 random import randint
  10. from odoo import _, fields, models
  11. from odoo.exceptions import UserError, ValidationError
  12. from odoo.tools.safe_eval import safe_eval
  13. class ResPartnerIdCategory(models.Model):
  14. _name = "res.partner.id_category"
  15. _description = "Partner ID Category"
  16. _order = "name"
  17. def _get_default_color(self):
  18. return randint(1, 11)
  19. color = fields.Integer(string="Color Index", default=_get_default_color)
  20. code = fields.Char(
  21. string="Code",
  22. size=16,
  23. required=True,
  24. help="Abbreviation or acronym of this ID type. For example, "
  25. "'driver_license'",
  26. )
  27. name = fields.Char(
  28. string="ID name",
  29. required=True,
  30. translate=True,
  31. help="Name of this ID type. For example, 'Driver License'",
  32. )
  33. active = fields.Boolean(string="Active", default=True)
  34. validation_code = fields.Text(
  35. "Python validation code", help="Python code called to validate an id number."
  36. )
  37. def _validation_eval_context(self, id_number):
  38. self.ensure_one()
  39. return {"self": self, "id_number": id_number}
  40. def validate_id_number(self, id_number):
  41. """Validate the given ID number
  42. The method raises an odoo.exceptions.ValidationError if the eval of
  43. python validation code fails
  44. """
  45. self.ensure_one()
  46. if self.env.context.get("id_no_validate") or not self.validation_code:
  47. return
  48. eval_context = self._validation_eval_context(id_number)
  49. try:
  50. safe_eval(self.validation_code, eval_context, mode="exec", nocopy=True)
  51. except Exception as e:
  52. raise UserError(
  53. _(
  54. "Error when evaluating the id_category validation code:"
  55. ":\n %s \n(%s)"
  56. )
  57. % (self.name, e)
  58. )
  59. if eval_context.get("failed", False):
  60. raise ValidationError(
  61. _("%s is not a valid %s identifier") % (id_number.name, self.name)
  62. )