[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/locking/ add a TALES
path adapter to make it easier to get information about lock
Fred L. Drake, Jr.
fdrake at gmail.com
Fri May 20 11:56:59 EDT 2005
Log message for revision 30466:
add a TALES path adapter to make it easier to get information about lock
status; this is useful in both templates and filter expressions
Changed:
U Zope3/trunk/src/zope/app/locking/README.txt
U Zope3/trunk/src/zope/app/locking/adapter.py
U Zope3/trunk/src/zope/app/locking/configure.zcml
U Zope3/trunk/src/zope/app/locking/tests.py
-=-
Modified: Zope3/trunk/src/zope/app/locking/README.txt
===================================================================
--- Zope3/trunk/src/zope/app/locking/README.txt 2005-05-20 15:55:43 UTC (rev 30465)
+++ Zope3/trunk/src/zope/app/locking/README.txt 2005-05-20 15:56:59 UTC (rev 30466)
@@ -349,3 +349,68 @@
>>> obj.breaklock()
BreakLockEvent ...
+
+
+TALES conditions based on locking
+---------------------------------
+
+TALES expressions can use a named path adapter to get information
+about the lock status for an object, including whether or not the
+object can be locked. The default registration for this adapter uses
+the name "locking", so a condition might be expressed like
+"context/locking:ownLock", for example.
+
+For objects that aren't lockable, the adapter provides information
+that makes sense::
+
+ >>> from zope.component import getAdapter
+
+ >>> from zope.app.traversing.interfaces import IPathAdapter
+
+ >>> ns = getAdapter(42, IPathAdapter, "locking")
+ >>> ns.lockable
+ False
+
+ >>> ns.locked
+ False
+
+ >>> ns.lockedOut
+ False
+
+ >>> ns.ownLock
+ False
+
+Using an object that's lockable, but unlocked, also gives the expected
+results::
+
+ >>> ns = getAdapter(item1, IPathAdapter, "locking")
+ >>> ns.lockable
+ True
+
+ >>> ns.locked
+ False
+
+ >>> ns.lockedOut
+ False
+
+ >>> ns.ownLock
+ False
+
+If we lock the object, the adapter indicates that the object is locked
+and that we own it::
+
+ >>> ob = ILockable(item1)
+ >>> ob.lock()
+ LockedEvent for ...
+
+ >>> ns.lockable
+ True
+
+ >>> ns.locked
+ True
+
+ >>> ns.lockedOut
+ False
+
+ >>> ns.ownLock
+ True
Modified: Zope3/trunk/src/zope/app/locking/adapter.py
===================================================================
--- Zope3/trunk/src/zope/app/locking/adapter.py 2005-05-20 15:55:43 UTC (rev 30465)
+++ Zope3/trunk/src/zope/app/locking/adapter.py 2005-05-20 15:56:59 UTC (rev 30466)
@@ -149,3 +149,25 @@
class BreakLockEvent(UnlockedEvent):
zope.interface.implements(IBreakLockEvent)
+
+
+class LockingPathAdapter(object):
+
+ zope.interface.implements(
+ zope.app.traversing.interfaces.IPathAdapter)
+
+ def __init__(self, target):
+ self._locking = LockingAdapterFactory(target)
+ self.lockable = self._locking is not None
+
+ def lockedOut(self):
+ return (self._locking is not None) and self._locking.isLockedOut()
+ lockedOut = property(lockedOut)
+
+ def locked(self):
+ return (self._locking is not None) and self._locking.locked()
+ locked = property(locked)
+
+ def ownLock(self):
+ return (self._locking is not None) and self._locking.ownLock()
+ ownLock = property(ownLock)
Modified: Zope3/trunk/src/zope/app/locking/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/locking/configure.zcml 2005-05-20 15:55:43 UTC (rev 30465)
+++ Zope3/trunk/src/zope/app/locking/configure.zcml 2005-05-20 15:56:59 UTC (rev 30466)
@@ -17,4 +17,18 @@
</configure>
+ <adapter
+ for="*"
+ factory=".adapter.LockingPathAdapter"
+ name="locking"
+ permission="zope.Public"
+ />
+
+ <class class=".adapter.LockingPathAdapter">
+ <require
+ permission="zope.Public"
+ attributes="lockable locked lockedOut ownLock"
+ />
+ </class>
+
</configure>
Modified: Zope3/trunk/src/zope/app/locking/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/locking/tests.py 2005-05-20 15:55:43 UTC (rev 30465)
+++ Zope3/trunk/src/zope/app/locking/tests.py 2005-05-20 15:56:59 UTC (rev 30466)
@@ -67,10 +67,14 @@
from zope.interface import Interface
from zope.app.locking.interfaces import ILockable, ILockTracker
from zope.app.locking.adapter import LockingAdapterFactory
+ from zope.app.locking.adapter import LockingPathAdapter
from zope.app.locking.storage import ILockStorage, LockStorage
-
+ from zope.app.traversing.interfaces import IPathAdapter
+
ztapi.provideAdapter(Interface, IKeyReference, FakeKeyReference)
ztapi.provideAdapter(Interface, ILockable, LockingAdapterFactory)
+ ztapi.provideAdapter(None, IPathAdapter, LockingPathAdapter,
+ "locking")
storage = LockStorage()
ztapi.provideUtility(ILockStorage, storage)
ztapi.provideUtility(ILockTracker, storage)
More information about the Zope3-Checkins
mailing list