plone.restapi/locking: Implement endpoints for locking

Thomas Buchberger <jenkins-z4DKO/[email protected]>
Newsgroups gmane.comp.web.zope.plone.cvs
Message-ID <[email protected]>
Repository: plone.restapi
Branch: refs/heads/locking
Date: 2017-07-25T18:14:09+02:00
Author: Thomas Buchberger (buchi) <t.buchberger-/8aE7x569Db/[email protected]>
Commit: https://github.com/plone/plone.restapi/commit/c609e0d4bb8cb47e015550576a4347b0e9d1dc2b

Implement endpoints for locking

Files changed:
A src/plone/restapi/services/locking/__init__.py
A src/plone/restapi/services/locking/configure.zcml
A src/plone/restapi/services/locking/locking.py
M src/plone/restapi/services/configure.zcml

diff --git a/src/plone/restapi/services/configure.zcml b/src/plone/restapi/services/configure.zcml
index bc18fdfd..cf31ebb5 100644
--- a/src/plone/restapi/services/configure.zcml
+++ b/src/plone/restapi/services/configure.zcml
@@ -10,6 +10,7 @@
   <include package=".groups"/>
   <!--<include package=".navigation"/>-->
   <include package=".history"/>
+  <include package=".locking" />
   <include package=".principals"/>
   <include package=".registry"/>
   <include package=".roles"/>
diff --git a/src/plone/restapi/services/locking/__init__.py b/src/plone/restapi/services/locking/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/plone/restapi/services/locking/configure.zcml b/src/plone/restapi/services/locking/configure.zcml
new file mode 100644
index 00000000..d3eb4c94
--- /dev/null
+++ b/src/plone/restapi/services/locking/configure.zcml
@@ -0,0 +1,38 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope"
+    xmlns:plone="http://namespaces.plone.org/plone"
+    xmlns:zcml="http://namespaces.zope.org/zcml">
+
+  <plone:service
+    method="POST"
+    name="@lock"
+    for="Products.CMFCore.interfaces.IContentish"
+    factory=".locking.Lock"
+    permission="cmf.ModifyPortalContent"
+    />
+
+  <plone:service
+    method="POST"
+    name="@unlock"
+    for="Products.CMFCore.interfaces.IContentish"
+    factory=".locking.Unlock"
+    permission="cmf.ModifyPortalContent"
+    />
+
+  <plone:service
+    method="POST"
+    name="@refresh-lock"
+    for="Products.CMFCore.interfaces.IContentish"
+    factory=".locking.RefreshLock"
+    permission="cmf.ModifyPortalContent"
+    />
+
+  <plone:service
+    method="GET"
+    name="@lock"
+    for="Products.CMFCore.interfaces.IContentish"
+    factory=".locking.LockInfo"
+    permission="zope2.View"
+    />
+
+</configure>
diff --git a/src/plone/restapi/services/locking/locking.py b/src/plone/restapi/services/locking/locking.py
new file mode 100644
index 00000000..bf9d34a9
--- /dev/null
+++ b/src/plone/restapi/services/locking/locking.py
@@ -0,0 +1,56 @@
+# -*- coding: utf-8 -*-
+from plone.restapi.services import Service
+from plone.locking.interfaces import ILockable
+from plone.locking.interfaces import IRefreshableLockable
+
+
+class Lock(Service):
+    """Lock an object"""
+
+    def reply(self):
+        lockable = IRefreshableLockable(self.context, None)
+        if lockable is not None:
+            lockable.lock()
+        self.request.response.setStatus(204)
+        return super(Lock, self).reply()
+
+
+class Unlock(Service):
+    """Unlock an object"""
+
+    def reply(self):
+        lockable = ILockable(self.context)
+        if lockable.can_safely_unlock():
+            lockable.unlock()
+        self.request.response.setStatus(204)
+        return super(Unlock, self).reply()
+
+
+class RefreshLock(Service):
+    """Refresh the lock of an object"""
+
+    def reply(self):
+        lockable = IRefreshableLockable(self.context, None)
+        if lockable is not None:
+            lockable.refresh_lock()
+        self.request.response.setStatus(204)
+        return super(RefreshLock, self).reply()
+
+
+class LockInfo(Service):
+    """Returns lock information about an object"""
+
+    def reply(self):
+        lockable = ILockable(self.context)
+        if lockable is not None:
+            info = {
+                'locked': lockable.locked(),
+                'stealable': lockable.stealable(),
+            }
+            lock_info = lockable.lock_info()
+            if len(lock_info) > 0:
+                info['creator'] = lock_info[0]['creator']
+                info['time'] = lock_info[0]['time']
+                info['token'] = lock_info[0]['token']
+                info['timeout'] = lock_info[0]['type'].timeout
+            return info



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.