Skip to content

Instantly share code, notes, and snippets.

@thomasmassmann
Created July 8, 2020 07:56
Show Gist options
  • Save thomasmassmann/1432ca4b6d3e12dd74d328c73122d1ce to your computer and use it in GitHub Desktop.
Save thomasmassmann/1432ca4b6d3e12dd74d328c73122d1ce to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""Customizations for plone.app.mosaic."""
from operator import itemgetter
from plone.app.mosaic.interfaces import IMosaicRegistryAdapter
from plone.app.mosaic.registry import getCategoryIndex
from plone.app.mosaic.registry import MosaicRegistry
from plone.registry.interfaces import IRegistry
from Products.CMFCore.interfaces._content import IFolderish
from zope.component import adapter
from zope.component import ComponentLookupError
from zope.component import getMultiAdapter
from zope.globalrequest import getRequest
from zope.i18n import translate
from zope.interface import implementer
@implementer(IMosaicRegistryAdapter)
@adapter(IRegistry)
class SlwfMosaicRegistry(MosaicRegistry):
"""Adapts a registry object to parse the mosaic settings data."""
def mapTiles(self, settings, config, tile_category, kwargs): # noqa: N802
"""Only show tiles available for the current context."""
request = getRequest()
context = kwargs.get('context', None)
if context:
try:
context = context._DraftProxy__target
except AttributeError:
pass
tiles = settings.get(
'{0:s}.{1:s}'.format(self.prefix, tile_category), {})
for key, tile in tiles.items():
if 'category' not in tile:
continue
# Here we check if the tile is available and allowed
if context:
try:
getMultiAdapter(
(context, request),
name=tile.get('name'),
)
except ComponentLookupError:
# Tile is not available anymore or not available for
# the current context.
continue
index = getCategoryIndex(config['tiles'], tile['category'])
label = tile.get('label', None)
if label:
label = translate(label, context=request)
tile['label'] = label
if index is not None:
config['tiles'][index]['tiles'].append(tile)
for tile in config['tiles']:
tile['tiles'].sort(key=itemgetter('weight'))
return config
def __call__(self, **kwargs):
settings = self.parseRegistry()
config = {}
config = self.mapFormatCategories(settings, config)
config = self.mapFormats(settings, config)
config = self.mapTinyMCEActionCategories(settings, config)
config = self.mapTinyMCEToolbarFormats(settings, config)
config = self.mapTinyMCEContextMenuFormats(settings, config)
config = self.mapActions(settings, config)
config = self.mapTilesCategories(settings, config)
for tile_category in ['structure_tiles', 'app_tiles']:
config = self.mapTiles(settings, config, tile_category, kwargs)
config = self.mapFieldTiles(settings, config, kwargs)
args = {
'type': None,
'context': None,
'request': None,
}
args.update(kwargs)
if IFolderish.providedBy(args['context']):
config['parent'] = args['context'].absolute_url() + '/'
elif args['context']:
config['parent'] = getattr(
args['context'].aq_inner,
'aq_parent',
None,
).absolute_url() + '/'
else:
# context can be None, at least in tests. Do nothing
# then. See test_config in test_mosaicregistry.py
pass
return config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment