Skip to content

Commit

Permalink
provide plone.app.caching etag "editmode".
Browse files Browse the repository at this point in the history
The "editmode" etag can be used when plone.app.caching is enabled for
editors in order to make the cache flush when switching the editmode.
  • Loading branch information
jone committed May 12, 2016
1 parent e316f72 commit 1492a5e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.rst
Expand Up @@ -41,6 +41,15 @@ You may want to place an action somewhere for toggling the edit mode - this
integration is not done by ``collective.editmodeswitcher``.


Caching
-------

``collective.editmodeswitcher`` provides a ``plone.app.caching`` etag adapter
named ``editmode``.
This etag can be used in the caching configuration in order to make the cache
flush when switching the edit mode.



Links
-----
Expand Down
18 changes: 18 additions & 0 deletions collective/editmodeswitcher/caching.py
@@ -0,0 +1,18 @@
from plone.app.caching.interfaces import IETagValue
from zope.component import adapts
from zope.component.hooks import getSite
from zope.interface import implements
from zope.interface import Interface


class EditModeEtag(object):
implements(IETagValue)
adapts(Interface, Interface)

def __init__(self, published, request):
self.published = published
self.request = request

def __call__(self):
switcher = getSite().restrictedTraverse('@@switch-editmode')
return switcher.get_state()
2 changes: 2 additions & 0 deletions collective/editmodeswitcher/configure.zcml
Expand Up @@ -19,4 +19,6 @@
allowed_attributes="get_state"
/>

<adapter factory=".caching.EditModeEtag" name="editmode" />

</configure>
1 change: 1 addition & 0 deletions collective/editmodeswitcher/tests/__init__.py
Expand Up @@ -10,6 +10,7 @@ class FunctionalTestCase(TestCase):

def setUp(self):
self.portal = self.layer['portal']
self.request = self.layer['request']

def grant(self, *roles):
setRoles(self.portal, TEST_USER_ID, list(roles))
Expand Down
17 changes: 17 additions & 0 deletions collective/editmodeswitcher/tests/test_caching.py
@@ -0,0 +1,17 @@
from collective.editmodeswitcher.config import COOKIE_NAME
from collective.editmodeswitcher.tests import FunctionalTestCase
from plone.app.caching.interfaces import IETagValue
from zope.component import getMultiAdapter


class TestCaching(FunctionalTestCase):

def test_editmode_etag(self):
view = self.portal.restrictedTraverse('@@view')
adapter = getMultiAdapter((view, self.request),
IETagValue,
name='editmode')

self.assertEquals('enabled', adapter())
self.request.cookies[COOKIE_NAME] = '1'
self.assertEquals('disabled', adapter())
2 changes: 1 addition & 1 deletion docs/HISTORY.txt
Expand Up @@ -5,7 +5,7 @@ Changelog
1.0.3 (unreleased)
------------------

- Nothing changed yet.
- Provide plone.app.caching etag "editmode". [jone]


1.0.2 (2016-02-12)
Expand Down

0 comments on commit 1492a5e

Please sign in to comment.