change_key_helper.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding: utf-8 -*-
  2. from odoo.exceptions import ValidationError
  3. class ChangeKeyHelper(object):
  4. def __init__(self):
  5. self.invalid_keys = []
  6. self.index = 0
  7. self.change_key_dic = {}
  8. return
  9. def add_invalid_keys(self, keys):
  10. if not keys:
  11. return
  12. self.invalid_keys += keys
  13. return
  14. def add_key(self, key):
  15. if key in self.invalid_keys:
  16. raise ValidationError(u'使用了非法名称:' + key)
  17. new_key = self._get_new_key()
  18. self.change_key_dic[key] = new_key
  19. self.invalid_keys.append(new_key)
  20. return
  21. def get_new_key(self, key):
  22. if key in self.invalid_keys:
  23. raise ValidationError(u'使用了非法名称:' + key)
  24. new_key = self._get_new_key()
  25. self.change_key_dic[key] = new_key
  26. self.invalid_keys.append(new_key)
  27. return new_key
  28. def safe_get_key(self, key):
  29. if key in self.change_key_dic:
  30. return self.change_key_dic[key]
  31. return ''
  32. def _get_new_key(self):
  33. self.index += 1
  34. return 'param_' + str(self.index)