Skip to content

Commit

Permalink
Merge pull request #457 from collective/refresh-behavior
Browse files Browse the repository at this point in the history
Add behavior to enable reloading the current page after a certain amount of time
  • Loading branch information
hvelarde committed Oct 20, 2014
2 parents 0b7587a + 9ae1bee commit b4f8f09
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 3 deletions.
14 changes: 14 additions & 0 deletions docs/end-user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ The changes will be applied to your cover immediately.
As mentioned before, the changes will be applied only to the cover tile,
not to the original content.

Behaviors
^^^^^^^^^

Refresh
+++++++

The Refresh behavior adds a couple of fields that enable reloading the current page after a certain amount of time.

.. figure:: https://raw.github.com/collective/collective.cover/master/docs/refresh-behavior.png
:align: center
:height: 400px
:width: 400px
:alt: A cover object with the Refresh behavior enabled

Advanced Actions
----------------

Expand Down
Binary file added docs/refresh-behavior.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
'plone.app.uuid',
'plone.app.vocabularies',
'plone.autoform',
'plone.behavior',
'plone.dexterity',
'plone.directives.form >=1.1',
'plone.i18n',
Expand Down
Empty file.
17 changes: 17 additions & 0 deletions src/collective/cover/behaviors/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:i18n="http://namespaces.zope.org/i18n"
xmlns:plone="http://namespaces.plone.org/plone"
i18n_domain="collective.cover">

<include package="plone.behavior" file="meta.zcml" />

<plone:behavior
title="Page refresh"
description="Reload the current page after a certain amount of time."
provides="collective.cover.behaviors.interfaces.IRefresh"
for="collective.cover.interfaces.ICover"
i18n:attributes="title; description"
/>

</configure>
34 changes: 34 additions & 0 deletions src/collective/cover/behaviors/interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# coding: utf-8
from collective.cover import _
from plone.directives import form
from plone.supermodel import model
from zope import schema
from zope.interface import alsoProvides
from zope.interface import Invalid


class IRefresh(model.Schema):

"""Reload the current page after a certain amount of time."""

model.fieldset('settings', fields=['enable_refresh', 'ttl'])

enable_refresh = schema.Bool(
title=_(u'Enable refresh'),
description=_(u'Enable refresh of the current page.'),
default=False,
)

ttl = schema.Int(
title=_(u'Time to live'),
description=_(u'Number of seconds after which to reload the current page.',),
default=300,
)

alsoProvides(IRefresh, form.IFormFieldProvider)


@form.validator(field=IRefresh['ttl'])
def validate_ttl(value):
if value <= 0:
raise Invalid(_(u'Value must be greater than zero.'))
10 changes: 7 additions & 3 deletions src/collective/cover/browser/templates/view.pt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
lang="en"
metal:use-macro="context/main_template/macros/master"
i18n:domain="collective.cover">

<head>
<metal:headslot fill-slot="head_slot">
<meta http-equiv="refresh" content="300"
tal:condition="context/refresh"
tal:attributes="content context/ttl|default" />
</metal:headslot>
</head>
<body>

<metal:main fill-slot="main">
<div tal:define="layout nocall:context/@@layout"
tal:replace="structure layout/render_view" />
</metal:main>

</body>
</html>
1 change: 1 addition & 0 deletions src/collective/cover/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<include file="permissions.zcml" />
<grok:grok package="." />

<include package=".behaviors" />
<include package=".tiles" />
<include file="profiles.zcml" />
<include file="subscribers.zcml" />
Expand Down
11 changes: 11 additions & 0 deletions src/collective/cover/content.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from collective.cover.behaviors.interfaces import IRefresh
from collective.cover.config import PROJECTNAME
from collective.cover.controlpanel import ICoverSettings
from collective.cover.interfaces import ICover
Expand Down Expand Up @@ -30,6 +31,16 @@ class Cover(Item):
# ref: http://thread.gmane.org/gmane.comp.web.zope.plone.devel/31799
implements(IDAVAware)

@property
def refresh(self):
"""Return the value of the enable_refresh field if the IRefresh
behavior is applied to the object, or False if not.
:returns: True if refresh of the current page is enabled
:rtype: bool
"""
return self.enable_refresh if IRefresh.providedBy(self) else False

def get_tiles(self, types=None, layout=None):
"""Traverse the layout tree and return a list of tiles on it.
Expand Down
57 changes: 57 additions & 0 deletions src/collective/cover/tests/test_refresh_behavior.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
from collective.cover.behaviors.interfaces import IRefresh
from collective.cover.interfaces import ICoverLayer
from collective.cover.testing import INTEGRATION_TESTING
from plone import api
from plone.behavior.interfaces import IBehavior
from plone.dexterity.interfaces import IDexterityFTI
from plone.dexterity.schema import SchemaInvalidatedEvent
from zope.component import queryUtility
from zope.event import notify
from zope.interface import alsoProvides

import unittest


class RefreshBehaviorTestCase(unittest.TestCase):

layer = INTEGRATION_TESTING

def _enable_refresh_behavior(self):
fti = queryUtility(IDexterityFTI, name='collective.cover.content')
behaviors = list(fti.behaviors)
behaviors.append(IRefresh.__identifier__)
fti.behaviors = tuple(behaviors)
# invalidate schema cache
notify(SchemaInvalidatedEvent('collective.cover.content'))

def _disable_refresh_behavior(self):
fti = queryUtility(IDexterityFTI, name='collective.cover.content')
behaviors = list(fti.behaviors)
behaviors.remove(IRefresh.__identifier__)
fti.behaviors = tuple(behaviors)
# invalidate schema cache
notify(SchemaInvalidatedEvent('collective.cover.content'))

def setUp(self):
self.portal = self.layer['portal']
self.request = self.layer['request']
alsoProvides(self.request, ICoverLayer)
with api.env.adopt_roles(['Manager']):
self.cover = api.content.create(
self.portal, 'collective.cover.content', 'c1')

def test_refresh_registration(self):
registration = queryUtility(IBehavior, name=IRefresh.__identifier__)
self.assertIsNotNone(registration)

def test_refresh_behavior(self):
view = api.content.get_view(u'view', self.cover, self.request)
self.assertNotIn('<meta http-equiv="refresh" content="300" />', view())
self._enable_refresh_behavior()
self.cover.enable_refresh = True
self.assertIn('<meta http-equiv="refresh" content="300" />', view())
self.cover.ttl = 5
self.assertIn('<meta http-equiv="refresh" content="5" />', view())
self._disable_refresh_behavior()
self.assertNotIn('<meta http-equiv="refresh" content="5" />', view())

0 comments on commit b4f8f09

Please sign in to comment.