def __call__(self, context=None): request = getRequest() if not isThemeEnabled(request): return SimpleVocabulary([]) currentTheme = getCurrentTheme() if currentTheme is None: return SimpleVocabulary([]) themeDirectory = queryResourceDirectory(THEME_RESOURCE_NAME, currentTheme) # noqa if themeDirectory is None: return SimpleVocabulary([]) if not themeDirectory.isDirectory(FRAGMENTS_DIRECTORY): return SimpleVocabulary([]) # Get settings to map titles titles = getFragmentsSettings(themeDirectory, 'themefragments:tiles') tiles = [ splitext(filename)[0] for filename in themeDirectory[FRAGMENTS_DIRECTORY].listDirectory() if splitext(filename)[-1] == '.pt' and themeDirectory[FRAGMENTS_DIRECTORY].isFile(filename) ] terms = [SimpleTerm(None, '', _(u'-- select fragment --'))] for tile in tiles: title = titles.get(tile, None) title = title is None and tile or title.strip().split('#')[0] if title and not title.startswith("Hidden"): terms.append(SimpleTerm(tile, tile, title)) return SimpleVocabulary(terms)
def __call__(self, context=None): request = getRequest() if not isThemeEnabled(request): return SimpleVocabulary([]) currentTheme = getCurrentTheme() if currentTheme is None: return SimpleVocabulary([]) themeDirectory = queryResourceDirectory(THEME_RESOURCE_NAME, currentTheme) # noqa if themeDirectory is None: return SimpleVocabulary([]) if not themeDirectory.isDirectory(FRAGMENTS_DIRECTORY): return SimpleVocabulary([]) # Get settings to map titles titles = getFragmentsSettings(themeDirectory, 'themefragments:tiles') tiles = [splitext(filename)[0] for filename in themeDirectory[FRAGMENTS_DIRECTORY].listDirectory() if splitext(filename)[-1] == '.pt' and themeDirectory[FRAGMENTS_DIRECTORY].isFile(filename)] terms = [SimpleTerm(None, '', _(u'-- select fragment --'))] for tile in tiles: title = titles.get(tile, None) title = title is None and tile or title.strip().split('#')[0] if title: terms.append(SimpleTerm(tile, tile, title)) return SimpleVocabulary(terms)
def __getitem__(self, name): # Make sure a theme is enabled if not isThemeEnabled(self.request): raise KeyError(name) # Check if there is views/<name>.pt in the theme, if not raise currentTheme = getCurrentTheme() if currentTheme is None: raise KeyError(name) themeDirectory = queryResourceDirectory(THEME_RESOURCE_NAME, currentTheme) # noqa if themeDirectory is None: raise KeyError(name) templatePath = "%s/%s.pt" % (FRAGMENTS_DIRECTORY, name,) if not themeDirectory.isFile(templatePath): raise KeyError(name) try: owner = themeDirectory[templatePath].getOwner() except AttributeError: owner = getSite().getOwner() template = themeDirectory.readFile(templatePath).decode('utf-8', 'replace') # noqa # Now disable the theme so we don't double-transform if self._disable_theming: self.request.response.setHeader('X-Theme-Disabled', '1') # Get settings to map fragment permissions permission = getFragmentsSettings( themeDirectory, 'themefragments:permissions').get(name) or 'zope.Public' # noqa return FragmentView(self.context, self.request, name, permission, template, owner) # noqa
def __getitem__(self, name): # Make sure a theme is enabled if not isThemeEnabled(self.request): raise KeyError(name) # Check if there is views/<name>.pt in the theme, if not raise currentTheme = getCurrentTheme() if currentTheme is None: raise KeyError(name) themeDirectory = queryResourceDirectory(THEME_RESOURCE_NAME, currentTheme) # noqa if themeDirectory is None: raise KeyError(name) templatePath = "%s/%s.pt" % ( FRAGMENTS_DIRECTORY, name, ) if not themeDirectory.isFile(templatePath): raise KeyError(name) try: owner = themeDirectory[templatePath].getOwner() except AttributeError: owner = getSite().getOwner() template = themeDirectory.readFile(templatePath).decode( 'utf-8', 'replace') # noqa # Now disable the theme so we don't double-transform if self._disable_theming: self.request.response.setHeader('X-Theme-Disabled', '1') # Get settings to map fragment permissions permission = getFragmentsSettings( themeDirectory, 'themefragments:permissions').get(name) or 'zope.Public' # noqa return FragmentView(self.context, self.request, name, permission, template, owner) # noqa
def FragmentTileCacheRuleFactory(obj): from z3c.caching.registry import ICacheRule from z3c.caching.registry import CacheRule noLongerProvides(obj, IFragmentTileCacheRuleLookup) try: default = ICacheRule(obj, None) except TypeError: try: default = ICacheRule(obj.context, None) except (TypeError, AttributeError): default = None try: fragment = obj.context.data['fragment'] except (AttributeError, KeyError): fragment = getFragmentName(getRequest()) if not fragment: return default currentTheme = getCurrentTheme() if not currentTheme: return default themeDirectory = queryResourceDirectory(THEME_RESOURCE_NAME, currentTheme) if not themeDirectory: return default rulesets = getFragmentsSettings(themeDirectory, 'themefragments:caching') if not rulesets: return default ruleset = rulesets.get(fragment) if not ruleset: return default return CacheRule(ruleset)
def __call__(self, context=None): request = getRequest() if not isThemeEnabled(request): return SimpleVocabulary([]) currentTheme = getCurrentTheme() if currentTheme is None: return SimpleVocabulary([]) themeDirectory = queryResourceDirectory(THEME_RESOURCE_NAME, currentTheme) # noqa if themeDirectory is None: return SimpleVocabulary([]) if not themeDirectory.isDirectory(FRAGMENTS_DIRECTORY): return SimpleVocabulary([]) # Get settings to map titles titles = getFragmentsSettings(themeDirectory, 'themefragments:tiles') tiles = [splitext(filename)[0] for filename in themeDirectory[FRAGMENTS_DIRECTORY].listDirectory() if splitext(filename)[-1] == '.pt' and themeDirectory[FRAGMENTS_DIRECTORY].isFile(filename)] fragment_tiles = [] for tile in tiles: title = titles.get(tile, None) title = title is None and tile or title.strip().split('#')[0] if title and not title.startswith("Hidden"): fragment_tiles.append([title, tile]) fragment_tiles.append([_(u'-- select fragment --'), None ]) fragment_tiles.sort() terms = [ SimpleTerm(value=pair[1], token=pair[1], title=pair[0]) for pair in fragment_tiles ] return SimpleVocabulary(terms)