Skip to content

Commit

Permalink
Add support for reading fragment tile titles from theme manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
datakurre committed Aug 11, 2015
1 parent 1427047 commit 726eab8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
41 changes: 31 additions & 10 deletions src/collective/themefragments/tiles.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from collective.themefragments.interfaces import FRAGMENTS_DIRECTORY
from collective.themefragments.traversal import ThemeFragment
from collective.themefragments.utils import getPluginSettings
from os.path import splitext
from plone.app.theming.interfaces import THEME_RESOURCE_NAME
from plone.app.theming.utils import getCurrentTheme
Expand All @@ -10,14 +11,21 @@
from plone.tiles import Tile
from zope import schema
from zope.globalrequest import getRequest
from zope.i18nmessageid import MessageFactory
from zope.interface import provider
from zope.schema.interfaces import IContextSourceBinder
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary
import logging

from zope.i18nmessageid import MessageFactory
_ = MessageFactory('collective.themefragments')

logger = logging.getLogger('collective.themefragments')

#
# [theme:themefragments:tiles]
# basename = Display title
#

@provider(IContextSourceBinder)
def themeFragments(context):
Expand All @@ -37,27 +45,40 @@ def themeFragments(context):
if not themeDirectory.isDirectory(FRAGMENTS_DIRECTORY):
return SimpleVocabulary([])

fragments = [splitext(filename)[0] for filename
in themeDirectory[FRAGMENTS_DIRECTORY].listDirectory()
if splitext(filename)[-1] == '.pt'
and themeDirectory[FRAGMENTS_DIRECTORY].isFile(filename)]
settings = getPluginSettings(
themeDirectory, plugins=[('themefragments:tiles', None)]
).get('themefragments:tiles', {})

return SimpleVocabulary(map(SimpleTerm, fragments))
tiles = [splitext(filename)[0] for filename
in themeDirectory[FRAGMENTS_DIRECTORY].listDirectory()
if splitext(filename)[-1] == '.pt'
and themeDirectory[FRAGMENTS_DIRECTORY].isFile(filename)]

return SimpleVocabulary(
[SimpleTerm(None, '', _(u'-- select fragment --'))] +
[SimpleTerm(tile, tile, settings.get(tile, tile)) for tile in tiles]
)


class IFragmentTile(model.Schema):
fragment = schema.Choice(
title=_(u'Fragment'),
source=themeFragments
title=_(u'Theme fragment'),
source=themeFragments,
)


class FragmentTile(Tile):
"""A tile that displays a theme fragment"""

def update(self):
self.index = ThemeFragment(
self.context, self.request)[self.data['fragment'].encode('utf-8')]
try:
self.index = ThemeFragment(self.context, self.request)[
self.data['fragment'].encode('utf-8')]
except KeyError:
logger.error(u"Theme fragment '{0:s}' was not found.".format(
self.data['fragment']
))
self.index = lambda: u''

def __call__(self):
self.update()
Expand Down
48 changes: 48 additions & 0 deletions src/collective/themefragments/utils.py
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
from ConfigParser import SafeConfigParser

from plone.app.theming.interfaces import THEME_RESOURCE_NAME
from plone.app.theming.plugins.utils import getPlugins
from plone.resource.manifest import MANIFEST_FILENAME


# This is copied from p.a.theming.plugins.utils to get uncached data
# noinspection PyPep8Naming
def getPluginSettings(themeDirectory, plugins=None):
"""Given an IResourceDirectory for a theme, return the settings for the
given list of plugins (or all plugins, if not given) provided as a list
of (name, plugin) pairs.
Returns a dict of dicts, with the outer dict having plugin names as keys
and containing plugins settings (key/value pairs) as values.
"""

if plugins is None:
plugins = getPlugins()

# noinspection PyPep8Naming
manifestContents = {}

if themeDirectory.isFile(MANIFEST_FILENAME):
parser = SafeConfigParser()
fp = themeDirectory.openFile(MANIFEST_FILENAME)

try:
parser.readfp(fp)
for section in parser.sections():
manifestContents[section] = {}

for name, value in parser.items(section):
manifestContents[section][name] = value

finally:
try:
fp.close()
except AttributeError:
pass

pluginSettings = {}
for name, plugin in plugins:
pluginSettings[name] = manifestContents.get("%s:%s" % (THEME_RESOURCE_NAME, name), {}) # noqa

return pluginSettings

0 comments on commit 726eab8

Please sign in to comment.