Browse Source

[ADD] field_char_transformed (#466)

pull/484/head
Holger Brunn 8 years ago
committed by Moises Lopez - https://www.vauxoo.com/
parent
commit
f8f95bc9b4
  1. 61
      field_char_transformed/README.rst
  2. 5
      field_char_transformed/__init__.py
  3. 17
      field_char_transformed/__openerp__.py
  4. 10
      field_char_transformed/field_char_stripped.py
  5. 46
      field_char_transformed/field_char_transformed.py
  6. BIN
      field_char_transformed/static/description/icon.png
  7. 4
      field_char_transformed/tests/__init__.py
  8. 32
      field_char_transformed/tests/test_field_char_transformed.py

61
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 <https://github.com/OCA/server-tools/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 <https://github.com/OCA/server-tools/issues/new?body=module:%20field_char_transformed%0Aversion:%208.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
Credits
=======
Images
------
* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.
Contributors
------------
* Holger Brunn <hbrunn@therp.nl>
Do not contact contributors directly about help with questions or problems concerning this addon, but use the `community mailing list <mailto:community@mail.odoo.com>`_ or the `appropriate specialized mailinglist <https://odoo-community.org/groups>`_ 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.

5
field_char_transformed/__init__.py

@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# 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

17
field_char_transformed/__openerp__.py

@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# 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": [
],
}

10
field_char_transformed/field_char_stripped.py

@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# 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)

46
field_char_transformed/field_char_transformed.py

@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# 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

BIN
field_char_transformed/static/description/icon.png

After

Width: 128  |  Height: 128  |  Size: 9.2 KiB

4
field_char_transformed/tests/__init__.py

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import test_field_char_transformed

32
field_char_transformed/tests/test_field_char_transformed.py

@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
# © 2016 Therp BV <http://therp.nl>
# 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')
Loading…
Cancel
Save