Skip to content

Commit

Permalink
Merge branch 'master' into background-image-behavior
Browse files Browse the repository at this point in the history
Conflicts:
	docs/end-user.rst
	src/collective/cover/behaviors/configure.zcml
	src/collective/cover/behaviors/interfaces.py
	src/collective/cover/browser/templates/view.pt
	src/collective/cover/content.py
  • Loading branch information
hvelarde committed Oct 20, 2014
2 parents 44144c8 + b4f8f09 commit 29c52fe
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 6 deletions.
18 changes: 16 additions & 2 deletions docs/end-user.rst
Expand Up @@ -249,8 +249,22 @@ 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.

Background image behavior
^^^^^^^^^^^^^^^^^^^^^^^^^
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

Background image
++++++++++++++++

The package includes a behavior that adds the possibility of having a background image on the default view of the item.

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.
8 changes: 8 additions & 0 deletions src/collective/cover/behaviors/configure.zcml
Expand Up @@ -6,6 +6,14 @@

<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"
/>

<plone:behavior
title="Background image"
description="Adds the possibility of having a background image on the default view of the item."
Expand Down
29 changes: 29 additions & 0 deletions src/collective/cover/behaviors/interfaces.py
Expand Up @@ -3,7 +3,36 @@
from plone.directives import form
from plone.namedfile.field import NamedBlobImage
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.'))


class IBackgroundImage(model.Schema):
Expand Down
13 changes: 9 additions & 4 deletions src/collective/cover/browser/templates/view.pt
Expand Up @@ -6,13 +6,18 @@
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>

<metal:styles fill-slot="style_slot">
<style type="text/css"
tal:condition="context/background"
tal:content="context/background" />
<style type="text/css"
tal:condition="context/background"
tal:content="context/background" />
</metal:styles>
</head>

<body>
<metal:main fill-slot="main">
<div tal:define="layout nocall:context/@@layout"
Expand Down
11 changes: 11 additions & 0 deletions src/collective/cover/content.py
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from collective.cover.behaviors.interfaces import IBackgroundImage
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 @@ -31,6 +32,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

@property
def background(self):
"""Return the style to be used on the item, if the IBackgroundImage
Expand Down
57 changes: 57 additions & 0 deletions src/collective/cover/tests/test_refresh_behavior.py
@@ -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 29c52fe

Please sign in to comment.