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.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 api, models, fields, _
  10. from odoo.exceptions import ValidationError, UserError
  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", size=16, required=True,
  18. help="Abbreviation or acronym of this ID type. For example, "
  19. "'driver_license'")
  20. name = fields.Char(
  21. string="ID name", required=True, translate=True,
  22. help="Name of this ID type. For example, 'Driver License'")
  23. active = fields.Boolean(string="Active", default=True)
  24. validation_code = fields.Text(
  25. 'Python validation code',
  26. help="Python code called to validate an id number.",
  27. default=lambda self: self._default_validation_code())
  28. def _default_validation_code(self):
  29. return _("\n# Python code. Use failed = True to specify that the id "
  30. "number is not valid.\n"
  31. "# You can use the following variables :\n"
  32. "# - self: browse_record of the current ID Category "
  33. "browse_record\n"
  34. "# - id_number: browse_record of ID number to validate")
  35. @api.multi
  36. def _validation_eval_context(self, id_number):
  37. self.ensure_one()
  38. return {'self': self,
  39. 'id_number': id_number,
  40. }
  41. @api.multi
  42. def validate_id_number(self, id_number):
  43. """Validate the given ID number
  44. The method raises an odoo.exceptions.ValidationError if the eval of
  45. python validation code fails
  46. """
  47. self.ensure_one()
  48. if self.env.context.get('id_no_validate'):
  49. return
  50. eval_context = self._validation_eval_context(id_number)
  51. try:
  52. safe_eval(self.validation_code,
  53. eval_context,
  54. mode='exec',
  55. nocopy=True)
  56. except Exception as e:
  57. raise UserError(
  58. _('Error when evaluating the id_category validation code:'
  59. ':\n %s \n(%s)') % (self.name, e))
  60. if eval_context.get('failed', False):
  61. raise ValidationError(
  62. _("%s is not a valid %s identifier") % (
  63. id_number.name, self.name))