Skip to content

Commit

Permalink
Merge b19dda4 into 8d24981
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel committed Apr 18, 2017
2 parents 8d24981 + b19dda4 commit 58b02ee
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 2 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
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

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
21 changes: 21 additions & 0 deletions docs/env.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 =
15 changes: 15 additions & 0 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 @@ -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.
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

0 comments on commit 58b02ee

Please sign in to comment.