diff --git a/base_field_serialized/__init__.py b/base_field_serialized/__init__.py new file mode 100644 index 000000000..3a4c4411e --- /dev/null +++ b/base_field_serialized/__init__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2014 Therp BV (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +import fields +import base_field_serialized diff --git a/base_field_serialized/__openerp__.py b/base_field_serialized/__openerp__.py new file mode 100644 index 000000000..13e6f48f4 --- /dev/null +++ b/base_field_serialized/__openerp__.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2014 Therp BV (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +{ + "name": "Serialized fields", + "version": "1.0", + "author": "Therp BV", + "license": "AGPL-3", + "complexity": "normal", + "description": """This addon restores serialized fields for Odoo 8.0 + """, + "category": "Hidden/Dependency", + "depends": [ + 'base', + ], + "auto_install": False, + "installable": True, + "application": False, +} diff --git a/base_field_serialized/base_field_serialized.py b/base_field_serialized/base_field_serialized.py new file mode 100644 index 000000000..10190e965 --- /dev/null +++ b/base_field_serialized/base_field_serialized.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2014 Therp BV (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from openerp import fields + + +class Serialized(fields.Field): + type = 'serialized' + + def convert_to_cache(self, value, env, validate=True): + if value: + return value + else: + return {} + +fields.Serialized = Serialized diff --git a/base_field_serialized/fields.py b/base_field_serialized/fields.py new file mode 100644 index 000000000..bac5aa21c --- /dev/null +++ b/base_field_serialized/fields.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2009 Tiny SPRL (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +import simplejson +import openerp +from openerp.models import FIELDS_TO_PGTYPES +from openerp.osv.fields import _column + +# --------------------------------------------------------- +# Serialized fields +# --------------------------------------------------------- + + +class serialized(_column): + """ A field able to store an arbitrary python data structure. + + Note: only plain components allowed. + """ + + def _symbol_set_struct(val): + return simplejson.dumps(val) + + def _symbol_get_struct(self, val): + return simplejson.loads(val or '{}') + + _prefetch = False + _type = 'serialized' + + _symbol_c = '%s' + _symbol_f = _symbol_set_struct + _symbol_set = (_symbol_c, _symbol_f) + _symbol_get = _symbol_get_struct + +openerp.osv.fields.serialized = serialized +FIELDS_TO_PGTYPES[openerp.osv.fields.serialized] = 'text' diff --git a/base_field_serialized/static/description/icon.png b/base_field_serialized/static/description/icon.png new file mode 100644 index 000000000..62e4d4be4 Binary files /dev/null and b/base_field_serialized/static/description/icon.png differ diff --git a/base_field_serialized/tests/__init__.py b/base_field_serialized/tests/__init__.py new file mode 100644 index 000000000..dde5974db --- /dev/null +++ b/base_field_serialized/tests/__init__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2014 Therp BV (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +import test_serialized +from test_serialized import TestBaseFieldSerialized diff --git a/base_field_serialized/tests/test_serialized.py b/base_field_serialized/tests/test_serialized.py new file mode 100644 index 000000000..f2956ea96 --- /dev/null +++ b/base_field_serialized/tests/test_serialized.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2014 Therp BV (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from openerp import models, fields +from openerp.tests.common import TransactionCase + + +class BaseFieldSerializedTestModel(models.Model): + _name = 'base.field.serialized.test.model' + + serialized = fields.Serialized('Serialized') + + +class TestBaseFieldSerialized(TransactionCase): + def test_ReadWrite(self): + BaseFieldSerializedTestModel._build_model(self.registry, self.cr) + self.env['base.field.serialized.test.model']._auto_init() + record = self.env['base.field.serialized.test.model'].create( + {'serialized': ['hello world']}) + self.assertEqual(record.serialized, ['hello world']) + self.env.invalidate_all() + self.assertEqual(record.serialized, ['hello world']) + record.write({'serialized': {'hello': 'world'}}) + self.env.invalidate_all() + self.assertEqual(record.serialized, {'hello': 'world'}) + record.write({'serialized': None}) + self.assertEqual( + self.registry['base.field.serialized.test.model'].browse( + self.cr, self.uid, record.id).serialized, + {})