Skip to content

Commit

Permalink
implement read-only check
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel committed Jul 19, 2017
1 parent fbb4950 commit cee94e5
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
/share/
/src/plone.api.egg-info/
/var/
pip-selfcheck.json
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion buildout.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions docs/env.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions plone-5.1.x.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[buildout]
test-eggs +=
plone.app.contenttypes

[versions]
plone.api =
10 changes: 7 additions & 3 deletions src/plone/api/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from plone.api.validation import required_parameters
from zope.globalrequest import getRequest

import Globals
import traceback


Expand Down Expand Up @@ -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


Expand Down
6 changes: 6 additions & 0 deletions src/plone/api/tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

1 comment on commit cee94e5

@jenkins-plone-org
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loechel Jenkins CI reporting about code analysis
See the full report here: http://jenkins.plone.org/job/package-plone.api/19/violations

src/plone/api/tests/test_portal.py:4:1: I001 isort found an import in the wrong position

Follow these instructions to reproduce it locally.

Please sign in to comment.