plone.api/master: implement read-only check on ZODB

Alexander Loechel <jenkins-z4DKO/[email protected]>
Newsgroups gmane.comp.web.zope.plone.cvs
Message-ID <[email protected]>
Repository: plone.api
Branch: refs/heads/master
Date: 2017-07-20T15:11:30+02:00
Author: Alexander Loechel (loechel) <[email protected]>
Commit: https://github.com/plone/plone.api/commit/9d45f619471160376735b0ae0175364b9e2a016d

implement read-only check on ZODB

Files changed:
M CHANGES.rst
M docs/env.rst
M src/plone/api/env.py
M src/plone/api/tests/test_env.py

diff --git a/CHANGES.rst b/CHANGES.rst
index 85dcdcf..a3d178e 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -10,7 +10,8 @@ Breaking changes:
 
 New features:
 
-- *add item here*
+- Add method to check if ZODB is in read-only mode.
+  [loechel]
 
 Bug fixes:
 
diff --git a/docs/env.rst b/docs/env.rst
index aadebe4..fb50b69 100644
--- a/docs/env.rst
+++ b/docs/env.rst
@@ -102,6 +102,27 @@ To know if your Plone instance is running in a test runner, use :meth:`api.env.t
         pass  # do something
 
 
+.. _env_read_only_mode_example:
+
+Read-Only mode
+==============
+
+To know if your Zope / Plone instance is running on a read-only ZODB connection use :meth:`api.env.read_only_mode`.
+
+**Use-Case:**
+If you run a ZRS or RelStorage cluster with active replication where all replicas are read-only be default.
+You could check if your instance is connected to a read only ZODB or a writeable ZODB.
+Therefore you could adjust the UI to prevent create, delete or update pages are shown.
+
+.. code-block:: python
+
+    from plone import api
+
+    is_read_only = api.env.read_only_mode()
+        if is_read_only:
+                  pass  # do something
+
+
 .. _env_plone_version_example:
 
 Plone version
diff --git a/src/plone/api/env.py b/src/plone/api/env.py
index 96545e2..32d81fb 100644
--- a/src/plone/api/env.py
+++ b/src/plone/api/env.py
@@ -13,6 +13,7 @@
 from plone.api.validation import required_parameters
 from zope.globalrequest import getRequest
 
+import Globals
 import traceback
 
 
@@ -201,6 +202,20 @@ def test_mode():
     return IS_TEST
 
 
+def read_only_mode():
+    """Returns True if you are running the zope instance on an read only ZODB.
+
+    :Example: :ref:`env_read_only_mode_example`
+    """
+    isReadOnly = True
+    try:
+        conn = Globals.DB.open()
+        isReadOnly = conn.isReadOnly()
+    finally:
+        conn.close()
+    return isReadOnly
+
+
 def plone_version():
     """Return Plone version number.
 
diff --git a/src/plone/api/tests/test_env.py b/src/plone/api/tests/test_env.py
index 1e16351..7b27fb5 100644
--- a/src/plone/api/tests/test_env.py
+++ b/src/plone/api/tests/test_env.py
@@ -402,6 +402,12 @@ def test_test_mode(self):
         from plone.api.env import test_mode
         self.assertEqual(test_mode(), True)
 
+    def test_read_only_mode(self):
+        """Test that read_only_mode() returns False
+        as we have a write enabled ZODB."""
+        from plone.api.env import read_only_mode
+        self.assertFalse(read_only_mode())
+
     def test_plone_version(self):
         """Tests that plone_version() returns Plone version."""
         from plone.api.env import plone_version



------------------------------------------------------------------------------
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.