From 4412dc74756364a23b25562ed867b00481852258 Mon Sep 17 00:00:00 2001 From: SodexisTeam Date: Thu, 27 Oct 2016 14:16:09 +0530 Subject: [PATCH] [9.0][FIX] added post and uninstall hook for partner_address_street3 (#316) * [FIX] added hooks,modified version,MVC structure and simple headers * hooks docstrings & test for post_init_hook from PR#318 * [FIX] remove street3 with 2 different pattern * [FIX] added test for uninstall_hook --- partner_address_street3/README.rst | 1 + partner_address_street3/__init__.py | 27 ++--- partner_address_street3/__openerp__.py | 31 ++---- partner_address_street3/hooks.py | 42 ++++++++ partner_address_street3/model/__init__.py | 21 ---- partner_address_street3/model/partner.py | 48 --------- partner_address_street3/models/__init__.py | 7 ++ partner_address_street3/models/partner.py | 19 ++++ partner_address_street3/models/res_country.py | 20 ++++ partner_address_street3/tests/__init__.py | 23 +---- .../tests/test_street_3.py | 99 ++++++------------- partner_address_street3/view/partner_view.xml | 6 +- 12 files changed, 138 insertions(+), 206 deletions(-) create mode 100644 partner_address_street3/hooks.py delete mode 100644 partner_address_street3/model/__init__.py delete mode 100644 partner_address_street3/model/partner.py create mode 100644 partner_address_street3/models/__init__.py create mode 100644 partner_address_street3/models/partner.py create mode 100644 partner_address_street3/models/res_country.py diff --git a/partner_address_street3/README.rst b/partner_address_street3/README.rst index 529101270..6382b2a67 100644 --- a/partner_address_street3/README.rst +++ b/partner_address_street3/README.rst @@ -41,6 +41,7 @@ Contributors * Nicolas Bessi * Alexandre Fayolle +* Sodexis Team Maintainer ---------- diff --git a/partner_address_street3/__init__.py b/partner_address_street3/__init__.py index 6ac610f01..c04b6d5e1 100644 --- a/partner_address_street3/__init__.py +++ b/partner_address_street3/__init__.py @@ -1,21 +1,8 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# Author: Nicolas Bessi -# Copyright 2014 Camptocamp SA -# -# 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 . import model +# Copyright 2014 Nicolas Bessi, Alexandre Fayolle, Camptocamp SA +# Copyright 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models +from .hooks import post_init_hook +from .hooks import uninstall_hook diff --git a/partner_address_street3/__openerp__.py b/partner_address_street3/__openerp__.py index 61be56df1..d953eafcc 100644 --- a/partner_address_street3/__openerp__.py +++ b/partner_address_street3/__openerp__.py @@ -1,35 +1,20 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# Author: Nicolas Bessi -# Copyright 2014 Camptocamp SA -# -# 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 . -# -############################################################################## +# Copyright 2014 Nicolas Bessi, Alexandre Fayolle, Camptocamp SA +# Copyright 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + { 'name': 'Street3 in addresses', - 'version': '9.0.0.1.0', - 'author': "Camptocamp,Odoo Community Association (OCA)", + 'version': '9.0.1.1.0', + 'author': "Camptocamp,Sodexis,Odoo Community Association (OCA)", 'maintainer': 'Camptocamp', 'category': 'Sales', 'complexity': 'easy', 'depends': ['base'], 'website': 'http://www.camptocamp.com', 'data': ['view/partner_view.xml'], - 'demo': [], - 'test': [], + 'post_init_hook': 'post_init_hook', + 'uninstall_hook': 'uninstall_hook', 'installable': True, 'auto_install': False, 'license': 'AGPL-3', diff --git a/partner_address_street3/hooks.py b/partner_address_street3/hooks.py new file mode 100644 index 000000000..f256e4ca2 --- /dev/null +++ b/partner_address_street3/hooks.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +# Copyright 2014 Nicolas Bessi, Alexandre Fayolle, Camptocamp SA +# Copyright 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +def post_init_hook(cr, registry): + """ Add street3 to address format """ + query = """ + UPDATE res_country + SET address_format = replace( + address_format, + E'%(street2)s\n', + E'%(street2)s\n%(street3)s\n' + ) + """ + cr.execute(query) + + +def uninstall_hook(cr, registry): + """ Remove street3 from address format """ + # Remove %(street3)s\n from address_format + query = """ + UPDATE res_country + SET address_format = replace( + address_format, + E'%(street3)s\n', + '' + ) + """ + cr.execute(query) + + # Remove %(street3)s from address_format + query = """ + UPDATE res_country + SET address_format = replace( + address_format, + E'%(street3)s', + '' + ) + """ + cr.execute(query) diff --git a/partner_address_street3/model/__init__.py b/partner_address_street3/model/__init__.py deleted file mode 100644 index b4349f625..000000000 --- a/partner_address_street3/model/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# Author: Nicolas Bessi -# Copyright 2014 Camptocamp SA -# -# 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 . import partner diff --git a/partner_address_street3/model/partner.py b/partner_address_street3/model/partner.py deleted file mode 100644 index 71e78ef54..000000000 --- a/partner_address_street3/model/partner.py +++ /dev/null @@ -1,48 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# Author: Nicolas Bessi -# Copyright 2014-2015 Camptocamp SA -# -# 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, api - - -class ResPartner(models.Model): - """Add third field in address""" - - _inherit = "res.partner" - street3 = fields.Char('Street 3') - - @api.model - def _address_fields(self): - fields = super(ResPartner, self)._address_fields() - fields.append('street3') - return fields - - -class res_country(models.Model): - """Override default adresses formatting of countries""" - - _inherit = 'res.country' - - address_format = fields.Text( - default=( - "%(street)s\n%(street2)s\n%(street3)s\n" - "%(city)s %(state_code)s %(zip)s\n" - "%(country_name)s" - ) - ) diff --git a/partner_address_street3/models/__init__.py b/partner_address_street3/models/__init__.py new file mode 100644 index 000000000..541c51242 --- /dev/null +++ b/partner_address_street3/models/__init__.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +# Copyright 2014 Nicolas Bessi, Alexandre Fayolle, Camptocamp SA +# Copyright 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import partner +from . import res_country diff --git a/partner_address_street3/models/partner.py b/partner_address_street3/models/partner.py new file mode 100644 index 000000000..5b7d9e10c --- /dev/null +++ b/partner_address_street3/models/partner.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Copyright 2014 Nicolas Bessi, Alexandre Fayolle, Camptocamp SA +# Copyright 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import models, fields, api + + +class ResPartner(models.Model): + """Add third field in address""" + + _inherit = "res.partner" + street3 = fields.Char('Street 3') + + @api.model + def _address_fields(self): + fields = super(ResPartner, self)._address_fields() + fields.append('street3') + return fields diff --git a/partner_address_street3/models/res_country.py b/partner_address_street3/models/res_country.py new file mode 100644 index 000000000..1a5abf1e3 --- /dev/null +++ b/partner_address_street3/models/res_country.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Copyright 2014 Nicolas Bessi, Alexandre Fayolle, Camptocamp SA +# Copyright 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import models, fields + + +class ResCountry(models.Model): + """Override default adresses formatting of countries""" + + _inherit = 'res.country' + + address_format = fields.Text( + default=( + "%(street)s\n%(street2)s\n%(street3)s\n" + "%(city)s %(state_code)s %(zip)s\n" + "%(country_name)s" + ) + ) diff --git a/partner_address_street3/tests/__init__.py b/partner_address_street3/tests/__init__.py index 3e6e82972..fe03ad116 100644 --- a/partner_address_street3/tests/__init__.py +++ b/partner_address_street3/tests/__init__.py @@ -1,21 +1,6 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# Author: Nicolas Bessi -# Copyright 2014 Camptocamp SA -# -# 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 . -# -############################################################################## +# Copyright 2014 Nicolas Bessi, Alexandre Fayolle, Camptocamp SA +# Copyright 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + from . import test_street_3 diff --git a/partner_address_street3/tests/test_street_3.py b/partner_address_street3/tests/test_street_3.py index ee329bd55..13dde9d0f 100644 --- a/partner_address_street3/tests/test_street_3.py +++ b/partner_address_street3/tests/test_street_3.py @@ -1,88 +1,45 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# Author: Nicolas Bessi -# Copyright 2014 Camptocamp SA -# -# 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 . -# -############################################################################## +# Copyright 2014 Nicolas Bessi, Alexandre Fayolle, Camptocamp SA +# Copyright 2016 Sodexis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + import openerp.tests.common as test_common +from ..hooks import uninstall_hook class TestStreet3(test_common.TransactionCase): def test_partner(self): - part_model = self.registry('res.partner') - country_model = self.registry('res.country') - country_id = country_model.create( - self.cr, - self.uid, - { - 'name': 'Donut Land', - 'code': 'DNL', - } - ) + """"Test address_format has been updated on existing countries""" + us_country = self.env.ref('base.us') - self.assertTrue(country_id) + self.assertTrue('%(street3)s' in us_country.address_format) - create_data = { + homer = self.env['res.partner'].create({ 'name': 'Homer Simpson', 'city': 'Springfield', 'street': '742 Evergreen Terrace', 'street2': 'Donut Lane', 'street3': 'Tho', - 'country_id': country_id, - 'is_company': True - } - - homer_id = part_model.create( - self.cr, - self.uid, - create_data - ) - - homer = part_model.browse( - self.cr, - self.uid, - homer_id, - ) - - self.assertEqual( - homer.country_id.address_format, - ("%(street)s\n%(street2)s\n%(street3)s\n" - "%(city)s %(state_code)s %(zip)s\n" - "%(country_name)s") - ) + 'country_id': us_country.id, + }) - create_data = { + # test synchro of street3 on create + bart = self.env['res.partner'].create({ 'name': 'Bart Simpson', - 'is_company': False, 'parent_id': homer.id, - 'use_parent_address': True - } - - bart_id = part_model.create( - self.cr, - self.uid, - create_data - ) - - bart = part_model.browse( - self.cr, - self.uid, - bart_id, - ) - - self.assertTrue(bart.street3, 'Tho') + 'type': 'contact', + }) + self.assertEquals(bart.street3, 'Tho') + + # test synchro of street3 on write + homer.write({'street3': 'in OCA we trust'}) + self.assertEquals(bart.street3, 'in OCA we trust') + + def test_uninstall_hook(self): + """"Test uninstall_hook""" + us_country = self.env.ref('base.us') + self.assertTrue('%(street3)s' in us_country.address_format) + uninstall_hook(self.cr, self.registry) + us_country.invalidate_cache() + self.assertTrue('%(street3)s' not in us_country.address_format) diff --git a/partner_address_street3/view/partner_view.xml b/partner_address_street3/view/partner_view.xml index 79071c851..b85c99ecd 100644 --- a/partner_address_street3/view/partner_view.xml +++ b/partner_address_street3/view/partner_view.xml @@ -1,6 +1,5 @@ - - + add stree3 in form view res.partner @@ -57,5 +56,4 @@ - - +