From 17feba6cefe89bd4d27b2bafbe04157f46375563 Mon Sep 17 00:00:00 2001 From: Benoit Date: Mon, 1 Oct 2018 18:19:58 +0200 Subject: [PATCH] [ADD] new module for partner address versioning --- partner_address_version/README.rst | 78 ++++ partner_address_version/__init__.py | 5 + partner_address_version/__manifest__.py | 16 + partner_address_version/models/__init__.py | 5 + partner_address_version/models/res_partner.py | 66 +++ .../readme/CONTRIBUTORS.rst | 1 + .../readme/DESCRIPTION.rst | 6 + .../static/description/index.html | 422 ++++++++++++++++++ partner_address_version/tests/__init__.py | 5 + .../tests/test_address_version.py | 63 +++ 10 files changed, 667 insertions(+) create mode 100644 partner_address_version/README.rst create mode 100644 partner_address_version/__init__.py create mode 100644 partner_address_version/__manifest__.py create mode 100644 partner_address_version/models/__init__.py create mode 100644 partner_address_version/models/res_partner.py create mode 100644 partner_address_version/readme/CONTRIBUTORS.rst create mode 100644 partner_address_version/readme/DESCRIPTION.rst create mode 100644 partner_address_version/static/description/index.html create mode 100644 partner_address_version/tests/__init__.py create mode 100644 partner_address_version/tests/test_address_version.py diff --git a/partner_address_version/README.rst b/partner_address_version/README.rst new file mode 100644 index 000000000..e56f21c0e --- /dev/null +++ b/partner_address_version/README.rst @@ -0,0 +1,78 @@ +======================= +Partner Address Version +======================= + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpartner--contact-lightgray.png?logo=github + :target: https://github.com/OCA/partner-contact/tree/10.0/partner_address_version + :alt: OCA/partner-contact +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/partner-contact-10-0/partner-contact-10-0-partner_address_version + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/134/10.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows to manage versions for partner addresses. + +You can define a list of versionned fields, for instance address, country, ... +Those fields cannot be changed once an address is versionned. + +This can be useful to know where a sale order has been delivered even after several changes on the partner addresses. + +**Table of contents** + +.. contents:: + :local: + +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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Akretion + +Contributors +~~~~~~~~~~~~ + +* Benoît Guillot + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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. + +This module is part of the `OCA/partner-contact `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/partner_address_version/__init__.py b/partner_address_version/__init__.py new file mode 100644 index 000000000..186ec6921 --- /dev/null +++ b/partner_address_version/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Akretion - Benoît Guillot +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models diff --git a/partner_address_version/__manifest__.py b/partner_address_version/__manifest__.py new file mode 100644 index 000000000..a7aa8553f --- /dev/null +++ b/partner_address_version/__manifest__.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Akretion - Benoît Guillot +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +{ + 'name': 'Partner Address Version', + 'version': '10.0.1.0.0', + 'author': "Akretion, " + "Odoo Community Association (OCA)", + 'website': 'https://github.com/OCA/partner-contact', + 'category': 'CRM', + 'license': 'AGPL-3', + 'installable': True, + 'depends': [ + 'base', + ], +} diff --git a/partner_address_version/models/__init__.py b/partner_address_version/models/__init__.py new file mode 100644 index 000000000..5c14864bb --- /dev/null +++ b/partner_address_version/models/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Akretion - Benoît Guillot +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import res_partner diff --git a/partner_address_version/models/res_partner.py b/partner_address_version/models/res_partner.py new file mode 100644 index 000000000..8757ebaa9 --- /dev/null +++ b/partner_address_version/models/res_partner.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Akretion - Benoît Guillot +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models, api, exceptions, _ +import hashlib +from collections import OrderedDict + + +class ResPartner(models.Model): + _inherit = "res.partner" + + version_hash = fields.Char(readonly=True, copy=False) + date_version = fields.Datetime(string='Date version', readonly=True) + + @api.multi + def get_address_version(self): + """ + Get a versioned partner corresponding to the partner fields. + If no versioned partner exists, create a new one. + """ + self.ensure_one() + version_hash = self.get_version_hash() + versioned_partner = self.with_context(active_test=False).search( + [('version_hash', '=', version_hash)]) + if versioned_partner: + return versioned_partner + default = { + 'active': False, + 'version_hash': version_hash, + 'parent_id': self.parent_id and self.parent_id.id or self.id, + 'date_version': fields.Datetime.now(), + } + versioned_partner = self.copy(default=default) + return versioned_partner + + def get_version_fields(self): + return [ + 'name', 'street', 'street2', 'zip', 'city', 'country_id', + 'parent_id' + ] + + def get_version_hash(self): + version_fields = self.get_version_fields() + version = OrderedDict() + for field in version_fields: + if field == 'parent_id': + parent_id = self.parent_id and self.parent_id.id or self.id + version[field] = parent_id + elif self[field]: + version[field] = self[field] + version_hash = hashlib.md5(str(version)).hexdigest() + return version_hash + + @api.multi + def write(self, vals): + version_fields = self.get_version_fields() + has_written_versioned_fields = any( + (f in version_fields) for f in vals.keys() + ) + for partner in self: + if partner.version_hash and has_written_versioned_fields: + raise exceptions.UserError(_( + "You can't modify a versioned field %s on the versioned " + "partner %s.") % (version_fields, partner.name)) + return super(ResPartner, self).write(vals) diff --git a/partner_address_version/readme/CONTRIBUTORS.rst b/partner_address_version/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..54ea6561e --- /dev/null +++ b/partner_address_version/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Benoît Guillot diff --git a/partner_address_version/readme/DESCRIPTION.rst b/partner_address_version/readme/DESCRIPTION.rst new file mode 100644 index 000000000..cf50fe441 --- /dev/null +++ b/partner_address_version/readme/DESCRIPTION.rst @@ -0,0 +1,6 @@ +This module allows to manage versions for partner addresses. + +You can define a list of versionned fields, for instance address, country, ... +Those fields cannot be changed once an address is versionned. + +This can be useful to know where a sale order has been delivered even after several changes on the partner addresses. diff --git a/partner_address_version/static/description/index.html b/partner_address_version/static/description/index.html new file mode 100644 index 000000000..4166480a0 --- /dev/null +++ b/partner_address_version/static/description/index.html @@ -0,0 +1,422 @@ + + + + + + +Partner Address Version + + + +
+

Partner Address Version

+ + +

Beta License: AGPL-3 OCA/partner-contact Translate me on Weblate Try me on Runbot

+

This module allows to manage versions for partner addresses.

+

You can define a list of versionned fields, for instance address, country, … +Those fields cannot be changed once an address is versionned.

+

This can be useful to know where a sale order has been delivered even after several changes on the partner addresses.

+

Table of contents

+ +
+

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.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Akretion
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

This module is part of the OCA/partner-contact project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/partner_address_version/tests/__init__.py b/partner_address_version/tests/__init__.py new file mode 100644 index 000000000..6af5be9a1 --- /dev/null +++ b/partner_address_version/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Akretion - Benoît Guillot +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_address_version diff --git a/partner_address_version/tests/test_address_version.py b/partner_address_version/tests/test_address_version.py new file mode 100644 index 000000000..0bf3f728c --- /dev/null +++ b/partner_address_version/tests/test_address_version.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Akretion - Benoît Guillot +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.tests import SavepointCase +from collections import OrderedDict +import hashlib +from odoo.exceptions import UserError + + +class TestAddressVersion(SavepointCase): + + @classmethod + def setUpClass(cls): + super(TestAddressVersion, cls).setUpClass() + cls.partner_vals = OrderedDict([ + ('name', u'Name'), + ('street', u'Street'), + ('street2', u'Street2'), + ('zip', u'Zip'), + ('city', u'City'), + ('country_id', cls.env.ref('base.fr')) + ]) + create_vals = cls.partner_vals.copy() + create_vals['country_id'] = cls.env.ref('base.fr').id + create_vals_2 = create_vals.copy() + cls.partner = cls.env['res.partner'].create(create_vals) + cls.partner_2 = cls.env['res.partner'].create(create_vals_2) + cls.partner_vals.update({'parent_id': cls.partner.id}) + + def test_hash(self): + test_hash = hashlib.md5(str(self.partner_vals)).hexdigest() + self.assertEqual(test_hash, self.partner.get_version_hash()) + + def test_create_version_partner(self): + new_partner = self.partner.get_address_version() + self.assertEqual(new_partner.active, False) + self.assertNotEqual(new_partner.id, self.partner.id) + self.assertEqual(new_partner.parent_id.id, self.partner.id) + + def test_get_version_hash(self): + self.partner.version_hash = self.partner.get_version_hash() + self.partner.active = False + version_partner = self.partner.get_address_version() + self.assertEqual(version_partner.id, self.partner.id) + + def test_write_versioned_partner(self): + new_partner = self.partner.get_address_version() + with self.assertRaises(UserError): + new_partner.street = 'New street' + + def test_same_address_different_parent(self): + new_partner = self.partner.get_address_version() + new_partner_2 = self.partner_2.get_address_version() + for field in self.partner.get_version_fields(): + if field == 'parent_id': + continue + self.assertEqual(new_partner[field], new_partner_2[field]) + self.assertNotEqual(new_partner.id, new_partner_2.id) + self.assertNotEqual( + new_partner.version_hash, + new_partner_2.version_hash + )