plone.api/master: Merge pull request #376 from plone/read_only_second_approach

GitHub <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-26T07:32:14+02:00
Author: Jens W. Klein (jensens) <jk-jUMamYRpa/IasaSY4/nUZ7NldLUNz+W/@public.gmane.org>
Commit: https://github.com/plone/plone.api/commit/ed8e8834663f3d3c1e9b198a745c6b42535c0735

Merge pull request #376 from plone/read_only_second_approach

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 f7184b9..d4d09c7 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -10,6 +10,9 @@ Breaking changes:
 
 New features:
 
+- Add method to check if ZODB is in read-only mode.
+  [loechel]
+
 - added tox.ini and code convention definitions in setup.py and .editorconfig so that they could be enforced
   [loechel]
 
diff --git a/docs/env.rst b/docs/env.rst
index aadebe4..1b8f846 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..8ac15ec 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,21 @@ def test_mode():
     return IS_TEST
 
 
+def read_only_mode():
+    """Check if the Zope instance is running on a read-only ZODB.
+
+    :returns: bool isReadOnly True if ZODB is read-only
+    :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.