From: Mike Bayer Date: Tue, 8 Dec 2009 23:09:48 +0000 (+0000) Subject: - The signature of the proxy_factory callable passed to X-Git-Tag: rel_0_6beta1~133 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=dc1fc3a8972ce79d3c63289018335fcb92dd45d8;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - The signature of the proxy_factory callable passed to association_proxy is now (lazy_collection, creator, value_attr, association_proxy), adding a fourth argument that is the parent AssociationProxy argument. Allows serializability and subclassing of the built in collections. [ticket:1259] --- diff --git a/CHANGES b/CHANGES index e3040479b1..a70a017f67 100644 --- a/CHANGES +++ b/CHANGES @@ -745,6 +745,13 @@ CHANGES the Session methods of those names, ensuring that the bind is in terms of the SqlSoup object's bind. + - The signature of the proxy_factory callable passed to + association_proxy is now (lazy_collection, creator, + value_attr, association_proxy), adding a fourth argument + that is the parent AssociationProxy argument. Allows + serializability and subclassing of the built in collections. + [ticket:1259] + 0.5.7 ===== - orm diff --git a/lib/sqlalchemy/ext/associationproxy.py b/lib/sqlalchemy/ext/associationproxy.py index e126fe638d..4353558f82 100644 --- a/lib/sqlalchemy/ext/associationproxy.py +++ b/lib/sqlalchemy/ext/associationproxy.py @@ -220,7 +220,7 @@ class AssociationProxy(object): self.collection_class = util.duck_type_collection(lazy_collection()) if self.proxy_factory: - return self.proxy_factory(lazy_collection, creator, self.value_attr) + return self.proxy_factory(lazy_collection, creator, self.value_attr, self) if self.getset_factory: getter, setter = self.getset_factory(self.collection_class, self) diff --git a/test/ext/test_associationproxy.py b/test/ext/test_associationproxy.py index 4a5775218d..e7a6de5e19 100644 --- a/test/ext/test_associationproxy.py +++ b/test/ext/test_associationproxy.py @@ -6,6 +6,7 @@ from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.orm.collections import collection from sqlalchemy.ext.associationproxy import * +from sqlalchemy.ext.associationproxy import _AssociationList from sqlalchemy.test import * from sqlalchemy.test.util import gc_collect @@ -615,6 +616,56 @@ class CustomObjectTest(_CollectionOperations): except TypeError: pass +class ProxyFactoryTest(ListTest): + def setup(self): + metadata = MetaData(testing.db) + + parents_table = Table('Parent', metadata, + Column('id', Integer, primary_key=True, + test_needs_autoincrement=True), + Column('name', String(128))) + children_table = Table('Children', metadata, + Column('id', Integer, primary_key=True, + test_needs_autoincrement=True), + Column('parent_id', Integer, + ForeignKey('Parent.id')), + Column('foo', String(128)), + Column('name', String(128))) + + class CustomProxy(_AssociationList): + def __init__(self, lazy_collection, creator, value_attr, parent): + getter, setter = parent._default_getset(lazy_collection) + _AssociationList.__init__(self, lazy_collection, creator, getter, setter, parent) + + + class Parent(object): + children = association_proxy('_children', 'name', + proxy_factory=CustomProxy, + proxy_bulk_set=CustomProxy.extend + ) + + def __init__(self, name): + self.name = name + + class Child(object): + def __init__(self, name): + self.name = name + + mapper(Parent, parents_table, properties={ + '_children': relation(Child, lazy=False, + collection_class=list)}) + mapper(Child, children_table) + + metadata.create_all() + + self.metadata = metadata + self.session = create_session() + self.Parent, self.Child = Parent, Child + + def test_sequence_ops(self): + self._test_sequence_ops() + + class ScalarTest(TestBase): def test_scalar_proxy(self): metadata = MetaData(testing.db)