diff --git a/field_char_transformed/README.rst b/field_char_transformed/README.rst new file mode 100644 index 000000000..4cabddec3 --- /dev/null +++ b/field_char_transformed/README.rst @@ -0,0 +1,61 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +================================== +Transform data on character fields +================================== + +This module was written to give programmers a convenient way to transform text input before it is written into the database or before it is read from it. + +As a practical example, FieldCharStripped is provided, which strips whitespace from the value before it is committed. + +Configuration +============= + +This module is a dependency to be used by programmers, not end users. There's no configuration to be done via the UI. + +Usage +===== + +Depend on this module and import the field classes you need:: + + from openerp.addons.field_char_transformed import FieldCharTransformed, FieldCharStripped + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed feedback +`here `_. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Holger Brunn + +Do not contact contributors directly about help with questions or problems concerning this addon, but use the `community mailing list `_ or the `appropriate specialized mailinglist `_ for help, and the bug tracker linked in `Bug Tracker`_ above for technical issues. + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/field_char_transformed/__init__.py b/field_char_transformed/__init__.py new file mode 100644 index 000000000..2d859400d --- /dev/null +++ b/field_char_transformed/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from .field_char_transformed import FieldCharTransformed +from .field_char_stripped import FieldCharStripped diff --git a/field_char_transformed/__openerp__.py b/field_char_transformed/__openerp__.py new file mode 100644 index 000000000..3a0db57d6 --- /dev/null +++ b/field_char_transformed/__openerp__.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + "name": "Transform data on character fields", + "version": "8.0.1.0.0", + "author": "Therp BV,Odoo Community Association (OCA)", + "license": "AGPL-3", + "category": "Hidden/Dependency", + "summary": "Allows to transform input in character fields before writing" + " or reading it to/from the database", + "depends": [ + 'base', + ], + "data": [ + ], +} diff --git a/field_char_transformed/field_char_stripped.py b/field_char_transformed/field_char_stripped.py new file mode 100644 index 000000000..b611fbff7 --- /dev/null +++ b/field_char_transformed/field_char_stripped.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from .field_char_transformed import FieldCharTransformed + + +class FieldCharStripped(FieldCharTransformed): + def __init__(self, string=None, **kwargs): + kwargs['transform'] = lambda x: x and x.strip() or x + super(FieldCharStripped, self).__init__(string=string, **kwargs) diff --git a/field_char_transformed/field_char_transformed.py b/field_char_transformed/field_char_transformed.py new file mode 100644 index 000000000..7bf438c42 --- /dev/null +++ b/field_char_transformed/field_char_transformed.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openerp import fields +from openerp.osv import fields as low_level_fields + + +class ColumnCharTransformed(low_level_fields.char): + def _transform(self, value): + return low_level_fields._symbol_set_char(self, self.transform(value)) + + def __init__(self, string="unknown", size=None, **args): + super(ColumnCharTransformed, self).__init__( + string=string, size=size, **args + ) + if self.transform: + self._symbol_f = self._symbol_set_char = self._transform + self._symbol_set = (self._symbol_c, self._symbol_f) + + +class FieldCharTransformed(fields.Char): + _slots = { + # a callable receiving a value and returning its result + 'transform': None, + } + + def convert_to_read(self, value, use_name_get=True): + result = super(FieldCharTransformed, self).convert_to_read( + value, use_name_get=use_name_get) + if self.transform: + return self.transform(result) + return result + + def convert_to_write(self, value, target=None, fnames=None): + result = super(FieldCharTransformed, self).convert_to_write( + value, target=target, fnames=fnames) + if self.transform: + return self.transform(result) + return result + + def to_column(self): + result = super(FieldCharTransformed, self).to_column() + if result and isinstance(result, low_level_fields.char): + result = ColumnCharTransformed( + transform=self.transform, **result._args) + return result diff --git a/field_char_transformed/static/description/icon.png b/field_char_transformed/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/field_char_transformed/static/description/icon.png differ diff --git a/field_char_transformed/tests/__init__.py b/field_char_transformed/tests/__init__.py new file mode 100644 index 000000000..e44dd2884 --- /dev/null +++ b/field_char_transformed/tests/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from . import test_field_char_transformed diff --git a/field_char_transformed/tests/test_field_char_transformed.py b/field_char_transformed/tests/test_field_char_transformed.py new file mode 100644 index 000000000..5dec35cb6 --- /dev/null +++ b/field_char_transformed/tests/test_field_char_transformed.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# © 2016 Therp BV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openerp import SUPERUSER_ID, models +from openerp.tests.common import TransactionCase +from openerp.addons.field_char_transformed import FieldCharStripped + + +class StrippedTest(models.TransientModel): + _name = 'test.field.char.stripped' + + name = FieldCharStripped() + + +class TestFieldCharTransformed(TransactionCase): + def test_field_char_transformed(self): + model = StrippedTest._build_model(self.registry, self.cr) + model._prepare_setup(self.cr, SUPERUSER_ID, False) + model._setup_base(self.cr, SUPERUSER_ID, False) + model._setup_fields(self.cr, SUPERUSER_ID) + model._auto_init(self.cr) + record_id = model.create( + self.cr, + SUPERUSER_ID, + { + 'name': ' hello world ', + }) + self.cr.execute( + 'select name from %s where id=%%s' % model._table, (record_id,)) + self.assertEqual(self.cr.fetchall()[0][0], 'hello world') + record = model.browse(self.cr, SUPERUSER_ID, record_id) + self.assertEqual(record.name, 'hello world')