[6.0 RC2] constraints can not be altered

Bug #700451 reported by Ferdinand
34
This bug affects 6 people
Affects Status Importance Assigned to Milestone
Odoo Server (MOVED TO GITHUB)
Fix Released
Medium
OpenERP's Framework R&D

Bug Description

pls see/try extra-trunk/chricar_product_gtin
and see discussion here
https://bugs.launchpad.net/openobject-addons/+bug/697969
may be because of product_product inheritance ?

Related branches

Revision history for this message
xrg (xrg) wrote : [PATCH] OSV: override inherited constraints, when fn name equals [Bug 700451]

Reported by Dr. Ferdinand Gassauer:
 "constraints can not be altered ..."

Indeed, when we define an expression like

class bar(osv.osv):
  _inherit = 'bar.bar'

  def _check_foo(self, cr, uid, ids, context):
      return True

  _constraints = [ (_check_foo, "Foo failed!", ['foo']) ]

... it means that _check_foo will be passed as an *object* to the model's
structure of _constraints. Therefore, it would be unequal and just append
the list of any existing constraints. So, an older (_check_foo, , ['foo'])
would always remain active using the previous code. This has to do with
the _check_foo being an unbound (ie. not inheritable) function.

Now, we check the /string name/ of the function, too. We say that if the
inherited class's constraint function has the same name "_check_foo", the
old ones shall be replaced.

Note: this MAY introduce unpredictable results, if several modules try
to override the same inherited constraint. There is no guaranteed order
of inheritance. Please avoid using this feature unless necessary.
---
 bin/osv/osv.py | 7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)

diff --git a/bin/osv/osv.py b/bin/osv/osv.py
index 411dc0f..21a7e05 100644
--- a/bin/osv/osv.py
+++ b/bin/osv/osv.py
@@ -372,7 +372,12 @@ class osv(osv_base, orm.orm):
                                 exist = False
                                 for c2 in range(len(new)):
                                      #For _constraints, we should check field and methods as well
- if new[c2][2]==c[2] and new[c2][0]==c[0]:
+ if new[c2][2]==c[2] and (new[c2][0] == c[0] \
+ or getattr(new[c2][0],'__name__', True) == \
+ getattr(c[0],'__name__', False)):
+ # If new class defines a constraint with
+ # same function name, we let it override
+ # the old one.
                                         new[c2] = c
                                         exist = True
                                         break
--
1.7.1

Changed in openobject-server:
assignee: nobody → OpenERP's Framework R&D (openerp-dev-framework)
importance: Undecided → Medium
status: New → Confirmed
Revision history for this message
xrg (xrg) wrote :

On Monday 10 January 2011, Ferdinand Gassauer wrote:
> Am Montag 10 Januar 2011, 12:11:33 schrieb P. Christeas:
>
> well isn't it the same for all inheritance - the last one wins ?

No. In members like _columns and _defaults (which are a dict), the named
items are updated. In _constraints, which is a list, they are just appended,
with that special fragment of code.

--
Say NO to spam and viruses. Stop using Microsoft Windows!

Revision history for this message
Ferdinand (office-chricar) wrote :

well IMHO it's not logical (and to be expected) that constraints follow another rule (append instead of replace) - only because its a list and not a dict.
just my 2c

Revision history for this message
Ferdinand (office-chricar) wrote :

Applying the patch solves the problem - will it make it into v6?

Revision history for this message
Olivier Dony (Odoo) (odo-openerp) wrote :

xrg's patch was merged in server revision 3277 <email address hidden>, in time for v6.0.

Thanks for reporting!

Changed in openobject-server:
milestone: none → 6.0
status: Confirmed → Fix Released
Revision history for this message
Satinderjit Singh (grewal-satinder) wrote :

So, does it work with OpenERP 7 for anyone? We tried installing it on OpenERP 7, and it does not work.

Revision history for this message
Martin Collins (mkc-steadfast) wrote :

It doesn't work in 6.1, even though it has the patch (around line 900 in orm.py).
Apparently there is never anything in cls.__dict__.get('_constraints', []).

Revision history for this message
Martin Collins (mkc-steadfast) wrote :

It does work in 6.1. You have to override the _constraints list as well as the function, even if you don't want to change it.
Only then will it show up in cls.__dict__.get('_constraints', []).

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.