Vo Minh Thu (OpenERP) has proposed merging lp:~openerp-dev/openobject-server/trunk-factored-create-instance-vmt into lp:openobject-server.
Requested reviews: OpenERP Core Team (openerp) For more details, see: https://code.launchpad.net/~openerp-dev/openobject-server/trunk-factored-create-instance-vmt/+merge/62473 -- https://code.launchpad.net/~openerp-dev/openobject-server/trunk-factored-create-instance-vmt/+merge/62473 Your team OpenERP R&D Team is subscribed to branch lp:~openerp-dev/openobject-server/trunk-factored-create-instance-vmt.
=== modified file 'openerp/osv/osv.py' --- openerp/osv/osv.py 2011-05-25 16:06:07 +0000 +++ openerp/osv/osv.py 2011-05-26 12:14:28 +0000 @@ -304,99 +304,75 @@ # method won't be called. return None + # + # Goal: try to apply inheritance at the instanciation level and + # put objects in the pool var + # + @classmethod + def makeInstance(cls, pool, module, cr, attributes): + parent_names = getattr(cls, '_inherit', None) + if parent_names: + if isinstance(parent_names, (str, unicode)): + name = cls._name or parent_names + parent_names = [parent_names] + else: + name = cls._name + + if not name: + raise TypeError('_name is mandatory in case of multiple inheritance') + + for parent_name in ((type(parent_names)==list) and parent_names or [parent_names]): + parent_class = pool.get(parent_name).__class__ + assert pool.get(parent_name), "parent class %s does not exist in module %s !" % (parent_name, module) + nattr = {} + for s in attributes: + new = copy.copy(getattr(pool.get(parent_name), s)) + if s == '_columns': + # Don't _inherit custom fields. + for c in new.keys(): + if new[c].manual: + del new[c] + if hasattr(new, 'update'): + new.update(cls.__dict__.get(s, {})) + elif s=='_constraints': + for c in cls.__dict__.get(s, []): + 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] \ + 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 + if not exist: + new.append(c) + else: + new.extend(cls.__dict__.get(s, [])) + nattr[s] = new + cls = type(name, (cls, parent_class), nattr) + obj = object.__new__(cls) + obj.__init__(pool, cr) + return obj + + class osv_memory(osv_base, orm.orm_memory): - # - # Goal: try to apply inheritancy at the instanciation level and - # put objects in the pool var - # + + @classmethod def createInstance(cls, pool, module, cr): - parent_names = getattr(cls, '_inherit', None) - if parent_names: - if isinstance(parent_names, (str, unicode)): - name = cls._name or parent_names - parent_names = [parent_names] - else: - name = cls._name - if not name: - raise TypeError('_name is mandatory in case of multiple inheritance') - - for parent_name in ((type(parent_names)==list) and parent_names or [parent_names]): - parent_class = pool.get(parent_name).__class__ - assert pool.get(parent_name), "parent class %s does not exist in module %s !" % (parent_name, module) - nattr = {} - for s in ('_columns', '_defaults'): - new = copy.copy(getattr(pool.get(parent_name), s)) - if s == '_columns': - # Don't _inherit custom fields. - for c in new.keys(): - if new[c].manual: - del new[c] - if hasattr(new, 'update'): - new.update(cls.__dict__.get(s, {})) - else: - new.extend(cls.__dict__.get(s, [])) - nattr[s] = new - cls = type(name, (cls, parent_class), nattr) - - obj = object.__new__(cls) - obj.__init__(pool, cr) - return obj - createInstance = classmethod(createInstance) + return cls.makeInstance(pool, module, cr, ['_columns', '_defaults']) + class osv(osv_base, orm.orm): - # - # Goal: try to apply inheritancy at the instanciation level and - # put objects in the pool var - # + + @classmethod def createInstance(cls, pool, module, cr): - parent_names = getattr(cls, '_inherit', None) - if parent_names: - if isinstance(parent_names, (str, unicode)): - name = cls._name or parent_names - parent_names = [parent_names] - else: - name = cls._name - if not name: - raise TypeError('_name is mandatory in case of multiple inheritance') + return cls.makeInstance(pool, module, cr, ['_columns', '_defaults', + '_inherits', '_constraints', '_sql_constraints']) - for parent_name in ((type(parent_names)==list) and parent_names or [parent_names]): - parent_class = pool.get(parent_name).__class__ - assert pool.get(parent_name), "parent class %s does not exist in module %s !" % (parent_name, module) - nattr = {} - for s in ('_columns', '_defaults', '_inherits', '_constraints', '_sql_constraints'): - new = copy.copy(getattr(pool.get(parent_name), s)) - if s == '_columns': - # Don't _inherit custom fields. - for c in new.keys(): - if new[c].manual: - del new[c] - if hasattr(new, 'update'): - new.update(cls.__dict__.get(s, {})) - else: - if s=='_constraints': - for c in cls.__dict__.get(s, []): - 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] \ - 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 - if not exist: - new.append(c) - else: - new.extend(cls.__dict__.get(s, [])) - nattr[s] = new - cls = type(name, (cls, parent_class), nattr) - obj = object.__new__(cls) - obj.__init__(pool, cr) - return obj - createInstance = classmethod(createInstance) def start_object_proxy(): object_proxy()
_______________________________________________ Mailing list: https://launchpad.net/~openerp-dev-web Post to : openerp-dev-web@lists.launchpad.net Unsubscribe : https://launchpad.net/~openerp-dev-web More help : https://help.launchpad.net/ListHelp