[Checkins] SVN: z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/ added keyreference implementation

Bernd Dorn bernd.dorn at fhv.at
Wed May 10 11:19:23 EDT 2006


Log message for revision 68074:
  added keyreference implementation

Changed:
  A   z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/
  A   z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/__init__.py
  A   z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/configure.zcml
  A   z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/keyreference.py
  A   z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/keyreference.txt
  A   z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/tests.py

-=-
Added: z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/__init__.py
===================================================================
--- z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/__init__.py	2006-05-10 12:30:36 UTC (rev 68073)
+++ z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/__init__.py	2006-05-10 15:19:22 UTC (rev 68074)
@@ -0,0 +1 @@
+#


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

Added: z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/configure.zcml
===================================================================
--- z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/configure.zcml	2006-05-10 12:30:36 UTC (rev 68073)
+++ z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/configure.zcml	2006-05-10 15:19:22 UTC (rev 68074)
@@ -0,0 +1,6 @@
+<configure xmlns="http://namespaces.zope.org/zope"
+           i18n_domain="zalchemy">
+
+<adapter factory=".keyreference.RefToSQLAlchemyObject"/>
+
+</configure>


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

Added: z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/keyreference.py
===================================================================
--- z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/keyreference.py	2006-05-10 12:30:36 UTC (rev 68073)
+++ z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/keyreference.py	2006-05-10 15:19:22 UTC (rev 68074)
@@ -0,0 +1,57 @@
+##############################################################################
+#
+# 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 ISQLAlchemyObject
+import z3c.zalchemy
+
+class RefToSQLAlchemyObject(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(ISQLAlchemyObject)
+
+    key_type_id = 'z3c.zalchemy.intid.keyreference'
+
+    def __init__(self, object):
+        object =  removeSecurityProxy(object)
+        session = z3c.zalchemy.getSession()
+        mapper = session.mapper(object.__class__)
+        self.ident = mapper.instance_key(object)[:2]
+        self._class,self.pk = self.ident
+        
+    def __call__(self):
+        session = z3c.zalchemy.getSession()
+        return session.get(self._class,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/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/keyreference.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/keyreference.txt
===================================================================
--- z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/keyreference.txt	2006-05-10 12:30:36 UTC (rev 68073)
+++ z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/keyreference.txt	2006-05-10 15:19:22 UTC (rev 68074)
@@ -0,0 +1,80 @@
+==================================
+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 RefToSQLAlchemyObject
+  >>> component.provideAdapter(RefToSQLAlchemyObject)
+
+
+Let's create a simple mapper and an according class.
+
+  >>> import sqlalchemy
+  >>> import z3c.zalchemy
+  >>> aTable = sqlalchemy.Table(
+  ...     'aTable',z3c.zalchemy.metadata,
+  ...     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.
+
+  >>> from z3c.zalchemy.interfaces import ISQLAlchemyObject
+  >>> class A(object):
+  ...     interface.implements(ISQLAlchemyObject)
+  ...     pass
+  >>> sqlalchemy.mapper(A, aTable) is not None
+  True
+  
+  >>> z3c.zalchemy.createTable('aTable')
+
+Start a transaction.
+
+  >>> import transaction
+  >>> txn = transaction.begin()
+  >>> session = z3c.zalchemy.getSession()
+
+
+  >>> a = A()
+  >>> session.save(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()
+
+  >>> a2 = ref1()
+  >>> a2
+  <A object at ...>
+
+  >>> a2.value
+  1
+  
+  >>> ref3 = IKeyReference(a2)
+  >>> ref3 == ref2 == ref1
+  True


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

Added: z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/tests.py
===================================================================
--- z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/tests.py	2006-05-10 12:30:36 UTC (rev 68073)
+++ z3c.zalchemy/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/tests.py	2006-05-10 15:19:22 UTC (rev 68074)
@@ -0,0 +1,39 @@
+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
+from threading import local
+
+def setUp(test):
+    setup.placefulSetUp()
+    test.tmpDir = tempfile.mkdtemp()
+    dbFile = os.path.join(test.tmpDir,'z3c.zalchemy.test.db')
+    
+    engineUtil = AlchemyEngineUtility(
+        'database','sqlite:///%s' % dbFile)
+    component.provideUtility(engineUtil)
+    test.globs['engineUtil'] = engineUtil
+    
+
+def tearDown(test):
+    setup.placefulTearDown()
+    shutil.rmtree(test.tmpDir)
+
+
+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/branches/jukart-SA-0.2/src/z3c/zalchemy/intid/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Checkins mailing list