plone.api/read_only: implement read-only check

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

implement read-only check

Files changed:
A plone-5.1.x.cfg
M .gitignore
M .travis.yml
M buildout.cfg
M docs/env.rst
M src/plone/api/env.py
M src/plone/api/tests/test_env.py

diff --git a/.gitignore b/.gitignore
index 0a7685c..aee378d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,3 +21,4 @@
 /share/
 /src/plone.api.egg-info/
 /var/
+pip-selfcheck.json
diff --git a/.travis.yml b/.travis.yml
index 717a83e..3cc8395 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -8,6 +8,7 @@ cache:
 env:
 - PLONE_VERSION=4.3
 - PLONE_VERSION=5.0
+- PLONE_VERSION=5.1
 global:
   - secure: "nTXMNU2aYbCtvKD74PndF8Xm2h6IvsxPjr6vj45AxjXmaB7Wf5oGi/4b6ObV\nexBtUQkFnc+M0ThGyUznOCPi3YxcbTzsyhQYhHUtmtw/6QRyYgo4E0GLDItU\n37Ff6wpxl3NMMJTvpi6SOVUvIJUl3+cs+4bkMkS48RSFGg2rGmo="  # Coveralls.io token
 matrix:
diff --git a/buildout.cfg b/buildout.cfg
index 02bdfa7..caaf609 100644
--- a/buildout.cfg
+++ b/buildout.cfg
@@ -3,7 +3,7 @@ extends =
     https://raw.github.com/collective/buildout.plonetest/master/test-4.3.x.cfg
     plone-4.3.x.cfg
 package-name = plone.api
-package-extras = [test]
+package-extras = [develop, test]
 test-eggs = Pillow
 parts +=
     omelette
diff --git a/docs/env.rst b/docs/env.rst
index 5ec6b05..1b8f846 100644
--- a/docs/env.rst
+++ b/docs/env.rst
@@ -109,12 +109,17 @@ 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
 
-    in_test_mode = api.env.read_only_mode()
-    if in_test_mode:
+    is_read_only = api.env.read_only_mode()
+    if is_read_only:
         pass  # do something
 
 
diff --git a/plone-5.1.x.cfg b/plone-5.1.x.cfg
new file mode 100644
index 0000000..d32ca7a
--- /dev/null
+++ b/plone-5.1.x.cfg
@@ -0,0 +1,6 @@
+[buildout]
+test-eggs +=
+    plone.app.contenttypes
+
+[versions]
+plone.api =
diff --git a/src/plone/api/env.py b/src/plone/api/env.py
index 1acf4a9..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
 
 
@@ -206,9 +207,12 @@ def read_only_mode():
 
     :Example: :ref:`env_read_only_mode_example`
     """
-    conn = Globals.DB.open()
-    isReadOnly = conn.isReadOnly()
-    conn.close()
+    isReadOnly = True
+    try:
+        conn = Globals.DB.open()
+        isReadOnly = conn.isReadOnly()
+    finally:
+        conn.close()
     return isReadOnly
 
 
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.