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.

46 lines
1.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Therp BV <http://therp.nl>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import fields
  5. from openerp.osv import fields as low_level_fields
  6. class ColumnCharTransformed(low_level_fields.char):
  7. def _transform(self, value):
  8. return low_level_fields._symbol_set_char(self, self.transform(value))
  9. def __init__(self, string="unknown", size=None, **args):
  10. super(ColumnCharTransformed, self).__init__(
  11. string=string, size=size, **args
  12. )
  13. if self.transform:
  14. self._symbol_f = self._symbol_set_char = self._transform
  15. self._symbol_set = (self._symbol_c, self._symbol_f)
  16. class FieldCharTransformed(fields.Char):
  17. _slots = {
  18. # a callable receiving a value and returning its result
  19. 'transform': None,
  20. }
  21. def convert_to_read(self, value, use_name_get=True):
  22. result = super(FieldCharTransformed, self).convert_to_read(
  23. value, use_name_get=use_name_get)
  24. if self.transform:
  25. return self.transform(result)
  26. return result
  27. def convert_to_write(self, value, target=None, fnames=None):
  28. result = super(FieldCharTransformed, self).convert_to_write(
  29. value, target=target, fnames=fnames)
  30. if self.transform:
  31. return self.transform(result)
  32. return result
  33. def to_column(self):
  34. result = super(FieldCharTransformed, self).to_column()
  35. if result and isinstance(result, low_level_fields.char):
  36. result = ColumnCharTransformed(
  37. transform=self.transform, **result._args)
  38. return result