Browse Source

[IMP] partner_address_version: black

14.0
kevinkhao 5 years ago
committed by Kevin Khao
parent
commit
1f24c916e0
  1. 19
      partner_address_version/__manifest__.py
  2. 34
      partner_address_version/models/res_partner.py
  3. 34
      partner_address_version/tests/test_address_version.py

19
partner_address_version/__manifest__.py

@ -2,15 +2,12 @@
# 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',
],
"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"],
}

34
partner_address_version/models/res_partner.py

@ -11,7 +11,7 @@ class ResPartner(models.Model):
_inherit = "res.partner"
version_hash = fields.Char(readonly=True, copy=False)
date_version = fields.Datetime(string='Date version', readonly=True)
date_version = fields.Datetime(string="Date version", readonly=True)
@api.multi
def get_address_version(self):
@ -22,29 +22,35 @@ class ResPartner(models.Model):
self.ensure_one()
version_hash = self.get_version_hash()
versioned_partner = self.with_context(active_test=False).search(
[('version_hash', '=', version_hash)])
[("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(),
"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'
"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':
if field == "parent_id":
parent_id = self.parent_id and self.parent_id.id or self.id
version[field] = parent_id
elif self[field]:
@ -60,7 +66,11 @@ class ResPartner(models.Model):
)
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))
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)

34
partner_address_version/tests/test_address_version.py

@ -9,24 +9,25 @@ 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'))
])
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["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})
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()
@ -47,17 +48,16 @@ class TestAddressVersion(SavepointCase):
def test_write_versioned_partner(self):
new_partner = self.partner.get_address_version()
with self.assertRaises(UserError):
new_partner.street = 'New street'
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':
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
new_partner.version_hash, new_partner_2.version_hash
)
Loading…
Cancel
Save