From 1427047a134153c968bee1c2bc120985e72fa585 Mon Sep 17 00:00:00 2001 From: Asko Soukka Date: Tue, 11 Aug 2015 12:29:34 +0300 Subject: [PATCH] Add fragment tile for plone.app.mosaic --- src/collective/themefragments/configure.zcml | 27 ++++++++ .../profiles/default/metadata.xml | 7 ++ .../profiles/default/registry.xml | 37 +++++++++++ src/collective/themefragments/tiles.py | 64 +++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 src/collective/themefragments/profiles/default/metadata.xml create mode 100644 src/collective/themefragments/profiles/default/registry.xml create mode 100644 src/collective/themefragments/tiles.py diff --git a/src/collective/themefragments/configure.zcml b/src/collective/themefragments/configure.zcml index 5291cdf..1ae69ac 100644 --- a/src/collective/themefragments/configure.zcml +++ b/src/collective/themefragments/configure.zcml @@ -1,6 +1,10 @@ @@ -12,4 +16,27 @@ permission="zope.Public" /> + + + + + diff --git a/src/collective/themefragments/profiles/default/metadata.xml b/src/collective/themefragments/profiles/default/metadata.xml new file mode 100644 index 0000000..f56f6a1 --- /dev/null +++ b/src/collective/themefragments/profiles/default/metadata.xml @@ -0,0 +1,7 @@ + + + 1 + + profile-plone.app.tiles:default + + diff --git a/src/collective/themefragments/profiles/default/registry.xml b/src/collective/themefragments/profiles/default/registry.xml new file mode 100644 index 0000000..7e1906b --- /dev/null +++ b/src/collective/themefragments/profiles/default/registry.xml @@ -0,0 +1,37 @@ + + + + + + Tiles + + + + collective.themefragment.fragment + + + + + + collective.themefragments.fragment + Theme fragment + advanced + app + + false + true + false + false + 100 + + + + + Available actions for the theme fragment tile + + + + + + diff --git a/src/collective/themefragments/tiles.py b/src/collective/themefragments/tiles.py new file mode 100644 index 0000000..c9332a6 --- /dev/null +++ b/src/collective/themefragments/tiles.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- +from collective.themefragments.interfaces import FRAGMENTS_DIRECTORY +from collective.themefragments.traversal import ThemeFragment +from os.path import splitext +from plone.app.theming.interfaces import THEME_RESOURCE_NAME +from plone.app.theming.utils import getCurrentTheme +from plone.app.theming.utils import isThemeEnabled +from plone.resource.utils import queryResourceDirectory +from plone.supermodel import model +from plone.tiles import Tile +from zope import schema +from zope.globalrequest import getRequest +from zope.interface import provider +from zope.schema.interfaces import IContextSourceBinder +from zope.schema.vocabulary import SimpleTerm +from zope.schema.vocabulary import SimpleVocabulary + +from zope.i18nmessageid import MessageFactory +_ = MessageFactory('collective.themefragments') + + +@provider(IContextSourceBinder) +def themeFragments(context): + request = getRequest() + + if not isThemeEnabled(request): + return SimpleVocabulary([]) + + currentTheme = getCurrentTheme() + if currentTheme is None: + return SimpleVocabulary([]) + + themeDirectory = queryResourceDirectory(THEME_RESOURCE_NAME, currentTheme) + if themeDirectory is None: + return SimpleVocabulary([]) + + 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)] + + return SimpleVocabulary(map(SimpleTerm, fragments)) + + +class IFragmentTile(model.Schema): + fragment = schema.Choice( + title=_(u'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')] + + def __call__(self): + self.update() + return u'{0:s}'.format(self.index())