[Checkins] SVN: z3c.zalchemy/sandbox/src/z3c/zalchemy/ added keyreference implementation for classes with a single mapper object

Bernd Dorn bernd.dorn at fhv.at
Wed May 3 11:37:45 EDT 2006


Log message for revision 67935:
  added keyreference implementation for classes with a single mapper object

Changed:
  U   z3c.zalchemy/sandbox/src/z3c/zalchemy/configure.zcml
  U   z3c.zalchemy/sandbox/src/z3c/zalchemy/interfaces.py
  A   z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/
  A   z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/__init__.py
  A   z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/configure.zcml
  A   z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/keyreference.py
  A   z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/keyreference.txt
  A   z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/tests.py

-=-
Modified: z3c.zalchemy/sandbox/src/z3c/zalchemy/configure.zcml
===================================================================
--- z3c.zalchemy/sandbox/src/z3c/zalchemy/configure.zcml	2006-05-03 15:29:25 UTC (rev 67934)
+++ z3c.zalchemy/sandbox/src/z3c/zalchemy/configure.zcml	2006-05-03 15:37:44 UTC (rev 67935)
@@ -36,6 +36,7 @@
      trusted="True"
      />
 
+  <include package=".intid" />
   <include package=".browser" />
 
 </configure>

Modified: z3c.zalchemy/sandbox/src/z3c/zalchemy/interfaces.py
===================================================================
--- z3c.zalchemy/sandbox/src/z3c/zalchemy/interfaces.py	2006-05-03 15:29:25 UTC (rev 67934)
+++ z3c.zalchemy/sandbox/src/z3c/zalchemy/interfaces.py	2006-05-03 15:37:44 UTC (rev 67935)
@@ -21,7 +21,13 @@
     """Marker interface for mapped sqlalchemy objects.
     """
 
+class IMappedSQLAlchemyObject(ISQLAlchemyObject):
 
+    """an object mapped to a single mapper which ist stored as a class
+    attribute"""
+
+    mapper = interface.Attribute("The sqlalchemy.mapper object")
+
 class ISQLAlchemyContainer(IContainer):
     """A zope container containing sqlalchemy objects.
     """

Added: z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/__init__.py
===================================================================
--- z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/__init__.py	2006-05-03 15:29:25 UTC (rev 67934)
+++ z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/__init__.py	2006-05-03 15:37:44 UTC (rev 67935)
@@ -0,0 +1 @@
+#


Property changes on: z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/configure.zcml
===================================================================
--- z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/configure.zcml	2006-05-03 15:29:25 UTC (rev 67934)
+++ z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/configure.zcml	2006-05-03 15:37:44 UTC (rev 67935)
@@ -0,0 +1,6 @@
+<configure xmlns="http://namespaces.zope.org/zope"
+           i18n_domain="zalchemy">
+
+<adapter factory=".keyreference.RefToMappedSQLAlchemyObject"/>
+
+</configure>


Property changes on: z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/keyreference.py
===================================================================
--- z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/keyreference.py	2006-05-03 15:29:25 UTC (rev 67934)
+++ z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/keyreference.py	2006-05-03 15:37:44 UTC (rev 67935)
@@ -0,0 +1,52 @@
+##############################################################################
+#
+# Copyright (c) 2005 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""IKeyreference adapter for zalchemy objects
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+from zope import interface, component
+import zope.app.keyreference.interfaces
+from zope.security.proxy import removeSecurityProxy
+from z3c.zalchemy.interfaces import IMappedSQLAlchemyObject
+class RefToMappedSQLAlchemyObject(object):
+    
+    """An IKeyReference for objects stored in an sql database by
+    zalchemy whith a mapper attached to the class"""
+    
+    interface.implements(zope.app.keyreference.interfaces.IKeyReference)
+    component.adapts(IMappedSQLAlchemyObject)
+
+    key_type_id = 'z3c.zalchemy.intid.keyreference'
+
+    def __init__(self, object):
+        object =  removeSecurityProxy(object)
+        self.ident = object.mapper.instance_key(object)[:2]
+        self._class,self.pk = self.ident
+        
+    def __call__(self):
+        return self._class.mapper.get(*self.pk)
+
+    def __hash__(self):
+        return hash(self.ident)
+    
+    def __cmp__(self, other):
+        if self.key_type_id == other.key_type_id:
+            return cmp(self.ident,other.ident)
+        return cmp(self.key_type_id, other.key_type_id)
+
+
+
+


Property changes on: z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/keyreference.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/keyreference.txt
===================================================================
--- z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/keyreference.txt	2006-05-03 15:29:25 UTC (rev 67934)
+++ z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/keyreference.txt	2006-05-03 15:37:44 UTC (rev 67935)
@@ -0,0 +1,71 @@
+==================================
+Keyreferences for zalchemy objects
+==================================
+
+In order to index zalchemy objects with the standard zope catalog we
+have to provide an adapter to IKeyReference.
+
+  >>> from zope import component, interface
+  >>> from z3c.zalchemy.intid.keyreference import RefToMappedSQLAlchemyObject
+  >>> component.provideAdapter(RefToMappedSQLAlchemyObject)
+
+
+Let's create a simple mapper and an according class.
+
+  >>> import sqlalchemy
+  >>> aTable = sqlalchemy.Table(
+  ...     'aTable',engine,
+  ...     sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True),
+  ...     sqlalchemy.Column('value', sqlalchemy.Integer),
+  ...     )
+
+Add it to the engine utility (which is already created for this test)
+and attach our mapper to the class.
+
+  >>> engineUtil.addTable(aTable,create=True)
+  >>> from z3c.zalchemy.interfaces import IMappedSQLAlchemyObject
+  >>> class A(object):
+  ...     interface.implements(IMappedSQLAlchemyObject)
+  ...     pass
+  >>> A.mapper = sqlalchemy.mapper(A, aTable)
+
+Start a transaction.
+
+  >>> import transaction
+  >>> from z3c.zalchemy.datamanager import beforeTraversal
+  >>> txn = transaction.begin()
+  >>> beforeTraversal(None)
+  >>> a = A()
+  >>> a.id = 1
+  >>> a.value = 1
+
+Now let us create a reference to the object
+
+  >>> from zope.app.keyreference.interfaces import IKeyReference
+  >>> ref1 = IKeyReference(a)
+
+A keyreference must be hashable and comparable.
+
+  >>> type(hash(ref1)) is type(1)
+  True
+
+  >>> ref2 = IKeyReference(a)
+  >>> ref1 == ref2
+  True
+
+We have not commited, our transaction therefore the ref returns None
+because it is not in the database.
+
+  >>> ref1() is None
+  True
+
+  >>> transaction.get().commit()
+  >>> txn = transaction.begin()
+  >>> beforeTraversal(None)
+  >>> a2 = ref1()
+  >>> a2
+  <A object at ...>
+
+  >>> ref3 = IKeyReference(a2)
+  >>> ref3 == ref2 == ref1
+  True


Property changes on: z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/keyreference.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/tests.py
===================================================================
--- z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/tests.py	2006-05-03 15:29:25 UTC (rev 67934)
+++ z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/tests.py	2006-05-03 15:37:44 UTC (rev 67935)
@@ -0,0 +1,38 @@
+import unittest
+import doctest
+from zope.testing.doctestunit import DocFileSuite
+from zope.app.testing import setup
+import os, tempfile
+import shutil
+from z3c.zalchemy.datamanager import AlchemyEngineUtility
+from zope import component
+import sqlalchemy
+
+def setUp(test):
+    setup.placefulSetUp()
+    test.tmpDir = tempfile.mkdtemp()
+    dbFile = os.path.join(test.tmpDir,'z3c.alchemy.test.db')
+    
+    engineUtil = AlchemyEngineUtility(
+        'sqlite',dns='sqlite://filename=%s.1' % dbFile)
+    component.provideUtility(engineUtil, name='sqlite')
+    test.globs['engine'] = sqlalchemy.ext.proxy.ProxyEngine()
+    test.globs['engineUtil'] = engineUtil
+    
+
+def tearDown(test):
+    shutil.rmtree(test.tmpDir)
+    setup.placefulTearDown()
+
+def test_suite():
+    return unittest.TestSuite((
+        DocFileSuite('keyreference.txt',
+                     setUp=setUp, tearDown=tearDown,
+                     optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+                     ),
+        ))
+
+
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: z3c.zalchemy/sandbox/src/z3c/zalchemy/intid/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Checkins mailing list