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

  1. # -*- coding: utf-8 -*-
  2. #
  3. # © 2004-2010 Tiny SPRL http://tiny.be
  4. # © 2010-2012 ChriCar Beteiligungs- und Beratungs- GmbH
  5. # http://www.camptocamp.at
  6. # © 2015 Antiun Ingenieria, SL (Madrid, Spain)
  7. # http://www.antiun.com
  8. # Antonio Espinosa <antonioea@antiun.com>
  9. # © 2016 ACSONE SA/NV (<http://acsone.eu>)
  10. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  11. from openerp import api, models, fields
  12. from openerp.exceptions import ValidationError, UserError
  13. from openerp.tools.safe_eval import safe_eval
  14. from openerp.tools.translate import _
  15. class ResPartnerIdCategory(models.Model):
  16. _name = "res.partner.id_category"
  17. _order = "name"
  18. def _default_validation_code(self):
  19. return _("\n# Python code. Use failed = True to specify that the id "
  20. "number is not valid.\n"
  21. "# You can use the following variables :\n"
  22. "# - self: browse_record of the current ID Category "
  23. "browse_record\n"
  24. "# - id_number: browse_record of ID number to validate")
  25. code = fields.Char(
  26. string="Code", size=16, required=True,
  27. help="Abbreviation or acronym of this ID type. For example, "
  28. "'driver_license'")
  29. name = fields.Char(
  30. string="ID name", required=True, translate=True,
  31. help="Name of this ID type. For example, 'Driver License'")
  32. active = fields.Boolean(string="Active", default=True)
  33. validation_code = fields.Text(
  34. 'Python validation code',
  35. help="Python code called to validate an id number.",
  36. default=_default_validation_code)
  37. @api.multi
  38. def _validation_eval_context(self, id_number):
  39. self.ensure_one()
  40. return {'self': self,
  41. 'id_number': id_number,
  42. }
  43. @api.multi
  44. def validate_id_number(self, id_number):
  45. """Validate the given ID number
  46. The method raises an openerp.exceptions.ValidationError if the eval of
  47. python validation code fails
  48. """
  49. self.ensure_one()
  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))