Browse Source

fixup constraint

pull/238/head
Yannick Vaucher 9 years ago
parent
commit
854d6a3ee2
  1. 19
      partner_firstname/models/partner.py
  2. 10
      partner_firstname/tests/test_empty.py

19
partner_firstname/models/partner.py

@ -146,8 +146,8 @@ class ResPartner(models.Model):
@api.constrains("firstname", "lastname")
def _check_name(self):
"""Ensure at least one name is set."""
if (self.type == 'contact' or self.company_type == 'company') \
and not (self.firstname or self.lastname):
if ((self.type == 'contact' or self.is_company) and
not (self.firstname or self.lastname)):
raise exceptions.EmptyNamesError(self)
@api.one
@ -189,11 +189,10 @@ class ResPartner(models.Model):
records._inverse_name()
_logger.info("%d partners updated installing module.", len(records))
# As name is computed after sql_constraint check, we modify it to accept
# to write when at least one name field is filled
_sql_constraints = [
('check_name',
"CHECK( (type='contact' AND (name IS NOT NULL OR"
" firstname IS NOT NULL OR lastname IS NOT NULL)) or"
" (type!='contact') )", 'Contacts require a name.'),
]
# Disabling SQL constraint givint a more explicit error using a Python
# contstraint
_sql_constraints = [(
'check_name',
"CHECK( 1=1 )",
'Contacts require a name.'
)]

10
partner_firstname/tests/test_empty.py

@ -6,8 +6,6 @@
To have more accurate results, remove the ``mail`` module before testing.
"""
import psycopg2
from openerp.tests.common import TransactionCase
from .base import MailInstalled
from .. import exceptions as ex
@ -22,12 +20,8 @@ class CompanyCase(TransactionCase):
try:
data = {"name": self.name}
model = self.env[self.model].with_context(**self.context)
if self.name is False:
with self.assertRaises(psycopg2.IntegrityError):
model.create(data)
else:
with self.assertRaises(ex.EmptyNamesError):
model.create(data)
with self.assertRaises(ex.EmptyNamesError):
model.create(data)
finally:
super(CompanyCase, self).tearDown()

Loading…
Cancel
Save