]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- The signature of the proxy_factory callable passed to
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 8 Dec 2009 23:09:48 +0000 (23:09 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 8 Dec 2009 23:09:48 +0000 (23:09 +0000)
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]

CHANGES
lib/sqlalchemy/ext/associationproxy.py
test/ext/test_associationproxy.py

diff --git a/CHANGES b/CHANGES
index e3040479b144f467c653441c04fd78f15576ec71..a70a017f679c801edd778dff2f998d7bdf1ed710 100644 (file)
--- 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
index e126fe638d772bfad177a12b303caae9e6876ca6..4353558f82afe6513ae7efcf9fec0d982fc2d81b 100644 (file)
@@ -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)
index 4a5775218dcaa5f1e67a88aaf61e821e9df54e3e..e7a6de5e19c78044ba1cdb7f676333019d26a3aa 100644 (file)
@@ -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)