Skip to content

Commit

Permalink
add option in configlet to select tiles available for cover layout cr…
Browse files Browse the repository at this point in the history
…eation (closes #191)

an upgrade step is provided to update the registry with the new record, but it's not working yet
see: http://stackoverflow.com/questions/16268938/how-to-add-a-plone-registry-record-programatically
  • Loading branch information
hvelarde committed Apr 29, 2013
1 parent 0b0d0f4 commit a240a74
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 22 deletions.
6 changes: 6 additions & 0 deletions docs/HISTORY.rst
Expand Up @@ -6,6 +6,11 @@ Because you have to know where your towel is.
1.0a3 (unreleased)
^^^^^^^^^^^^^^^^^^

- An option in the control panel configlet was added in order to select the
tiles that will be available for cover layout creation; an upgrade step
is provided to update the registry with the new record (closes `#191`_).
[hvelarde]

- Apply default configuration to tiles at initialization (closes `#100`_).
[hvelarde]

Expand Down Expand Up @@ -107,3 +112,4 @@ Because you have to know where your towel is.
.. _`#157`: https://github.com/collective/collective.cover/issues/157
.. _`#158`: https://github.com/collective/collective.cover/issues/158
.. _`#179`: https://github.com/collective/collective.cover/issues/179
.. _`#191`: https://github.com/collective/collective.cover/issues/191
15 changes: 15 additions & 0 deletions src/collective/cover/config.py
Expand Up @@ -2,6 +2,21 @@

PROJECTNAME = 'collective.cover'

# by default, all cover tiles will be available on layouts
DEFAULT_AVAILABLE_TILES = [
'collective.cover.basic',
'collective.cover.carousel',
'collective.cover.collection',
'collective.cover.contentbody',
'collective.cover.embed',
'collective.cover.file',
'collective.cover.image',
'collective.cover.link',
'collective.cover.list',
'collective.cover.pfg',
'collective.cover.richtext',
]

# by default, all standard content types will be searchable
DEFAULT_SEARCHABLE_CONTENT_TYPES = [
'Collection',
Expand Down
25 changes: 18 additions & 7 deletions src/collective/cover/controlpanel.py
@@ -1,12 +1,11 @@
# -*- coding: utf-8 -*-

from zope import schema
from zope.interface import Interface

from plone.app.registry.browser import controlpanel

from collective.cover import _
from collective.cover.config import DEFAULT_AVAILABLE_TILES
from collective.cover.config import DEFAULT_SEARCHABLE_CONTENT_TYPES
from plone.app.registry.browser import controlpanel
from zope import schema
from zope.interface import Interface


class ICoverSettings(Interface):
Expand All @@ -18,7 +17,17 @@ class ICoverSettings(Interface):
required=True,
key_type=schema.TextLine(title=_(u'Name')),
value_type=schema.TextLine(title=_(u'Layout')),
readonly=True,) # FIXME: we have no widget for this field yet
readonly=True, # FIXME: we have no widget for this field yet
)

available_tiles = schema.List(
title=_(u"Available tiles"),
description=_(u"This tiles will be available for layout creation."),
required=True,
default=DEFAULT_AVAILABLE_TILES,
value_type=schema.Choice(
vocabulary=u'collective.cover.EnabledTiles'),
)

searchable_content_types = schema.List(
title=_(u"Searchable Content Types"),
Expand All @@ -28,7 +37,8 @@ class ICoverSettings(Interface):
default=DEFAULT_SEARCHABLE_CONTENT_TYPES,
# we are going to list only the main content types in the widget
value_type=schema.Choice(
vocabulary=u'collective.cover.AvailableContentTypes'),)
vocabulary=u'collective.cover.AvailableContentTypes'),
)


class CoverSettingsEditForm(controlpanel.RegistryEditForm):
Expand All @@ -42,6 +52,7 @@ class CoverSettingsEditForm(controlpanel.RegistryEditForm):

def updateWidgets(self):
super(CoverSettingsEditForm, self).updateWidgets()
self.widgets['available_tiles'].style = u'min-width: 200px;'
self.widgets['searchable_content_types'].style = u'min-width: 200px;'


Expand Down
33 changes: 29 additions & 4 deletions src/collective/cover/profiles.zcml
Expand Up @@ -9,28 +9,53 @@
title="collective.cover"
directory="profiles/default"
description="An easy-to-use package to create complex cover pages for Plone sites."
provides="Products.GenericSetup.interfaces.EXTENSION" />
provides="Products.GenericSetup.interfaces.EXTENSION"
/>

<genericsetup:registerProfile
name="uninstall"
title="collective.cover uninstall"
directory="profiles/uninstall"
description="Uninstall profile for the collective.cover package."
provides="Products.GenericSetup.interfaces.EXTENSION" />
provides="Products.GenericSetup.interfaces.EXTENSION"
/>

<genericsetup:registerProfile
name="testfixture"
title="collective.cover: testfixture"
directory="profiles/testfixture"
description="This fixture creates generic content to be used in tests."
provides="Products.GenericSetup.interfaces.EXTENSION" />
provides="Products.GenericSetup.interfaces.EXTENSION"
/>

<genericsetup:upgradeStep
title="Upgrade cover to Plone 4.3"
source="1"
destination="2"
description="Install plone.app.relationfield"
handler="collective.cover.setuphandlers.to_plone43"
profile="collective.cover:default" />
profile="collective.cover:default"
/>

<genericsetup:registerProfile
name="upgrade_2_to_3"
title="collective.cover: upgrade_2_to_3"
directory="profiles/upgrade_2_to_3"
description="Upgrade steps from 2 to 3."
provides="Products.GenericSetup.interfaces.EXTENSION"
/>

<genericsetup:upgradeSteps
source="2"
destination="3"
profile="collective.cover:default">

<genericsetup:upgradeStep
title="Update registry"
description="Update the registry to register the available_tiles record"
handler=".upgrades.register_available_tiles_record"
/>

</genericsetup:upgradeSteps>

</configure>
2 changes: 1 addition & 1 deletion src/collective/cover/profiles/default/metadata.xml
@@ -1,5 +1,5 @@
<metadata>
<version>2</version>
<version>3</version>
<dependencies>
<dependency>profile-plone.app.tiles:default</dependency>
<dependency>profile-plone.app.blocks:default</dependency>
Expand Down
2 changes: 2 additions & 0 deletions src/collective/cover/profiles/uninstall/registry.xml
@@ -1,9 +1,11 @@
<?xml version="1.0"?>
<registry>
<record name="collective.cover.controlpanel.ICoverSettings.layouts" remove="true" />
<record name="collective.cover.controlpanel.ICoverSettings.available_tiles" remove="true" />
<record name="collective.cover.controlpanel.ICoverSettings.searchable_content_types" remove="true"/>
<!-- BBB: needed for compatibility with plone.app.registry < 1.2
right now Plone 4.2.x uses plone.app.registry = 1.1 -->
<record name="collective.cover.controlpanel.ICoverSettings.layouts" delete="true" />
<record name="collective.cover.controlpanel.ICoverSettings.available_tiles" delete="true"/>
<record name="collective.cover.controlpanel.ICoverSettings.searchable_content_types" delete="true"/>
</registry>
6 changes: 6 additions & 0 deletions src/collective/cover/tests/test_controlpanel.py
Expand Up @@ -62,6 +62,11 @@ def test_sections_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'layouts'))
self.assertNotEqual(self.settings.layouts, None)

def test_available_tiles_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'available_tiles'))
self.assertListEqual(
self.settings.available_tiles, DEFAULT_AVAILABLE_TILES)

def test_searchable_content_types_record_in_registry(self):
self.assertTrue(hasattr(self.settings, 'searchable_content_types'))
self.assertListEqual(self.settings.searchable_content_types,
Expand All @@ -75,6 +80,7 @@ def test_records_removed_on_uninstall(self):
BASE_REGISTRY = 'collective.cover.controlpanel.ICoverSettings.%s'
records = [
BASE_REGISTRY % 'layouts',
BASE_REGISTRY % 'available_tiles',
BASE_REGISTRY % 'searchable_content_types',
]

Expand Down
39 changes: 39 additions & 0 deletions src/collective/cover/tests/test_upgrades.py
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-

from collective.cover.testing import INTEGRATION_TESTING
from collective.cover.upgrades import register_available_tiles_record
from plone.registry.interfaces import IRecordAddedEvent
from plone.registry.interfaces import IRegistry
from zope.component import eventtesting
from zope.component import getUtility

import unittest


class UpgradeStepsTestCase(unittest.TestCase):

layer = INTEGRATION_TESTING

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

def test_from_2_to_3(self):
registry = getUtility(IRegistry)
record = 'collective.cover.controlpanel.ICoverSettings.available_tiles'

eventtesting.setUp()
# calling the handler here should have no effect as we are running the
# latest profile version
register_available_tiles_record(self.portal)
events = eventtesting.getEvents(IRecordAddedEvent)
self.assertEqual(len(events), 0)
eventtesting.clearEvents()

# now we delete the record and rerun the handler to verify the record
# was added
del registry.records[record]
register_available_tiles_record(self.portal)
events = eventtesting.getEvents(IRecordAddedEvent)
self.assertEqual(len(events), 1)
self.assertIn(record, registry.records)
eventtesting.clearEvents()
22 changes: 22 additions & 0 deletions src/collective/cover/tests/test_vocabularies.py
Expand Up @@ -46,6 +46,28 @@ def test_available_tiles_vocabulary(self):
#self.assertIn(u'plone.app.imagetile', tiles)
#self.assertIn(u'plone.app.texttile', tiles)

def test_enabled_tiles_vocabulary(self):
name = u'collective.cover.EnabledTiles'
vocabulary = queryUtility(IVocabularyFactory, name)
self.assertIsNotNone(vocabulary)
tiles = vocabulary(self.portal)
self.assertEqual(len(tiles), 11)
self.assertIn(u'collective.cover.basic', tiles)
self.assertIn(u'collective.cover.carousel', tiles)
self.assertIn(u'collective.cover.collection', tiles)
self.assertIn(u'collective.cover.contentbody', tiles)
self.assertIn(u'collective.cover.embed', tiles)
self.assertIn(u'collective.cover.file', tiles)
self.assertIn(u'collective.cover.image', tiles)
self.assertIn(u'collective.cover.link', tiles)
self.assertIn(u'collective.cover.list', tiles)
self.assertIn(u'collective.cover.pfg', tiles)
self.assertIn(u'collective.cover.richtext', tiles)

# standard tiles are not enabled... yet
self.assertNotIn(u'plone.app.imagetile', tiles)
self.assertNotIn(u'plone.app.texttile', tiles)

def test_available_content_types_vocabulary(self):
name = u'collective.cover.AvailableContentTypes'
vocabulary = queryUtility(IVocabularyFactory, name)
Expand Down
37 changes: 37 additions & 0 deletions src/collective/cover/upgrades.py
@@ -0,0 +1,37 @@
# -*- coding:utf-8 -*-

from collective.cover import _
from collective.cover.config import DEFAULT_AVAILABLE_TILES
from collective.cover.config import PROJECTNAME
from plone.registry import field
from plone.registry import Record
from plone.registry.interfaces import IRegistry
from zope.component import getUtility

import logging


def register_available_tiles_record(context, logger=None):
"""Handler for upgrade step from 2 to 3; adds the 'available_tiles' record
to the registry.
"""
if logger is None:
logger = logging.getLogger(PROJECTNAME)

registry = getUtility(IRegistry)
record = 'collective.cover.controlpanel.ICoverSettings.available_tiles'

if record not in registry.records:
available_tiles = field.List(
title=_(u"Available tiles"),
description=_(u"This tiles will be available for layout creation."),
required=True,
default=DEFAULT_AVAILABLE_TILES,
value_type=field.Choice(
vocabulary=u'collective.cover.EnabledTiles'),
)

registry.records[record] = Record(available_tiles)
logger.info("'available_tiles' record was added to the registry")
else:
logger.info("'available_tiles' record already exists in the registry")
43 changes: 33 additions & 10 deletions src/collective/cover/vocabularies.py
@@ -1,16 +1,16 @@
# -*- coding: utf-8 -*-

from zope.component import getUtility

from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary

from collective.cover.controlpanel import ICoverSettings
from collective.cover.tiles.base import IPersistentCoverTile
from five import grok
from plone.registry.interfaces import IRegistry

from plone.app.vocabularies.types import ReallyUserFriendlyTypesVocabulary

from collective.cover.controlpanel import ICoverSettings
from plone.registry.interfaces import IRegistry
from zope.component import getMultiAdapter
from zope.component import getUtility
from zope.globalrequest import getRequest
from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary


class AvailableLayoutsVocabulary(object):
Expand All @@ -36,14 +36,37 @@ def __call__(self, context):
registry = getUtility(IRegistry)
tiles = registry['plone.app.tiles']

# TODO: verify the tile implements IPersistentCoverTile
items = [SimpleTerm(value=i, title=i) for i in tiles]
return SimpleVocabulary(items)

grok.global_utility(AvailableTilesVocabulary,
name=u'collective.cover.AvailableTiles')


class EnabledTilesVocabulary(object):
"""Return a list of tiles ready to work with collective.cover.
"""
grok.implements(IVocabularyFactory)

def _enabled(self, name):
tile = getMultiAdapter((self.context, self.request), name=name)
return IPersistentCoverTile.providedBy(tile)

def __call__(self, context):
self.context = context
self.request = getRequest() # context may be an adapter

registry = getUtility(IRegistry)
tiles = registry['plone.app.tiles']

tiles = filter(self._enabled, tiles) # only enabled tiles
items = [SimpleTerm(value=i, title=i) for i in tiles]
return SimpleVocabulary(items)

grok.global_utility(EnabledTilesVocabulary,
name=u'collective.cover.EnabledTiles')


class AvailableContentTypesVocabulary(ReallyUserFriendlyTypesVocabulary):
"""
Inherit from plone.app.vocabularies.ReallyUserFriendlyTypes; and filter
Expand Down

0 comments on commit a240a74

Please sign in to comment.