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.

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