Browse Source

[IMP] base_partner_sequence: black, isort

14.0
Kevin Khao 5 years ago
committed by mostafaala2
parent
commit
179268381e
  1. 9
      base_partner_sequence/__manifest__.py
  2. 33
      base_partner_sequence/models/partner.py
  3. 39
      base_partner_sequence/tests/test_base_partner_sequence.py
  4. 1
      base_partner_sequence/views/partner_view.xml

9
base_partner_sequence/__manifest__.py

@ -16,14 +16,9 @@
"development_status": "Production/Stable",
"category": "Generic Modules/Base",
"website": "https://github.com/OCA/partner-contact",
"depends": [
'base',
],
"depends": ["base"],
"summary": "Sets customer's code from a sequence",
"data": [
'data/partner_sequence.xml',
'views/partner_view.xml',
],
"data": ["data/partner_sequence.xml", "views/partner_view.xml"],
"installable": True,
"license": "AGPL-3",
}

33
base_partner_sequence/models/partner.py

@ -4,29 +4,29 @@
# Copyright 2016 Camptocamp - Akim Juillerat (<http://www.camptocamp.com>).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, models, exceptions, _
from odoo import _, api, exceptions, models
class ResPartner(models.Model):
"""Assigns 'ref' from a sequence on creation and copying"""
_inherit = 'res.partner'
_inherit = "res.partner"
@api.multi
def _get_next_ref(self, vals=None):
return self.env['ir.sequence'].next_by_code('res.partner')
return self.env["ir.sequence"].next_by_code("res.partner")
@api.model
def create(self, vals):
if not vals.get('ref') and self._needsRef(vals=vals):
vals['ref'] = self._get_next_ref(vals=vals)
if not vals.get("ref") and self._needsRef(vals=vals):
vals["ref"] = self._get_next_ref(vals=vals)
return super(ResPartner, self).create(vals)
@api.multi
def copy(self, default=None):
default = default or {}
if self._needsRef():
default['ref'] = self._get_next_ref()
default["ref"] = self._get_next_ref()
return super(ResPartner, self).copy(default)
@api.multi
@ -34,11 +34,11 @@ class ResPartner(models.Model):
for partner in self:
partner_vals = vals.copy()
if (
not partner_vals.get('ref')
and partner._needsRef(partner_vals)
and not partner.ref
not partner_vals.get("ref")
and partner._needsRef(partner_vals)
and not partner.ref
):
partner_vals['ref'] = partner._get_next_ref(vals=partner_vals)
partner_vals["ref"] = partner._get_next_ref(vals=partner_vals)
super(ResPartner, partner).write(partner_vals)
return True
@ -52,14 +52,15 @@ class ResPartner(models.Model):
partner's 'ref'
"""
if not vals and not self: # pragma: no cover
raise exceptions.UserError(_(
'Either field values or an id must be provided.'))
raise exceptions.UserError(
_("Either field values or an id must be provided.")
)
# only assign a 'ref' to commercial partners
if self:
vals = {}
vals['is_company'] = self.is_company
vals['parent_id'] = self.parent_id
return vals.get('is_company') or not vals.get('parent_id')
vals["is_company"] = self.is_company
vals["parent_id"] = self.parent_id
return vals.get("is_company") or not vals.get("parent_id")
@api.model
def _commercial_fields(self):
@ -67,4 +68,4 @@ class ResPartner(models.Model):
Make the partner reference a field that is propagated
to the partner's contacts
"""
return super(ResPartner, self)._commercial_fields() + ['ref']
return super(ResPartner, self)._commercial_fields() + ["ref"]

39
base_partner_sequence/tests/test_base_partner_sequence.py

@ -6,42 +6,47 @@ import odoo.tests.common as common
class TestBasePartnerSequence(common.TransactionCase):
def setUp(self):
super(TestBasePartnerSequence, self).setUp()
self.res_partner = self.env['res.partner']
self.partner = self.res_partner.create({
'name': "test1",
'email': "test@test.com"})
self.res_partner = self.env["res.partner"]
self.partner = self.res_partner.create(
{"name": "test1", "email": "test@test.com"}
)
def test_ref_sequence_on_partner(self):
# Test sequence on creating partner and copying it
self.assertTrue(self.partner.ref, "A partner has always a ref.")
copy = self.partner.copy()
self.assertTrue(copy.ref, "A partner with ref created by copy "
"has a ref by default.")
self.assertTrue(
copy.ref, "A partner with ref created by copy " "has a ref by default."
)
def test_ref_sequence_on_contact(self):
# Test if sequence doesn't increase on creating a contact child
contact = self.res_partner.create({
'name': "contact1",
'email': "contact@contact.com",
'parent_id': self.partner.id})
contact = self.res_partner.create(
{
"name": "contact1",
"email": "contact@contact.com",
"parent_id": self.partner.id,
}
)
self.assertEqual(
self.partner.ref, contact.ref, "All it's ok as sequence doesn't "
"increase.")
self.partner.ref,
contact.ref,
"All it's ok as sequence doesn't " "increase.",
)
def test_unique_ref_on_write(self):
"""Assert that create and write gives a different refs if we """
vals = [
{'name': "test1", 'email': "test@test.com"},
{'name': "test2", 'email': "test@test.com"},
{"name": "test1", "email": "test@test.com"},
{"name": "test2", "email": "test@test.com"},
]
partners = self.env['res.partner'].create(vals)
partners = self.env["res.partner"].create(vals)
self.assertFalse(partners[0].ref == partners[1].ref)
partners.write({'ref': False})
partners.write({"ref": False})
self.assertFalse(partners[0].ref)
partners.write({})
self.assertFalse(partners[0].ref == partners[1].ref)

1
base_partner_sequence/views/partner_view.xml

@ -13,4 +13,3 @@
</record>
</odoo>
Loading…
Cancel
Save