def onLayoutEdited(obj, event): """ need to get the layout because you need to know what are acceptible storage values """ lookup = ILayoutAware(obj) layout = lookup.content_layout() if not layout: return tree = fromstring(layout) tile_keys = [] for el in utils.bodyTileXPath(tree): tile_url = el.attrib.get('data-tile', '') if 'plone.app.standardtiles.field' in tile_url: continue tile_keys.append( ANNOTATIONS_KEY_PREFIX + '.' + tile_url.split('?')[0].split('/')[-1] ) annotations = IAnnotations(obj) for key in list(annotations.keys()): if key.startswith(ANNOTATIONS_KEY_PREFIX) and key not in tile_keys: del annotations[key]
def test_content_layout(self): self.behavior.contentLayout = \ '/++contentlayout++testlayout1/content.html' rendered = ContentLayoutView(self.portal['f1']['d1'], self.request)() tree = html.fromstring(rendered) tiles = [node.attrib[tileAttrib] for node in bodyTileXPath(tree)] self.assertIn( './@@test.tile1/tile2?magicNumber:int=2&X-Tile-Persistent=yes', tiles) self.assertIn( './@@test.tile1/tile3?X-Tile-Persistent=yes', tiles)
def renderTiles(request, tree): """Find all tiles in the given response, contained in the lxml element tree `tree`, and insert them into the ouput. Assumes panel merging has already happened. """ # Optionally enable ESI rendering in tiles that support this if not request.getHeader(ESI_HEADER): registry = queryUtility(IRegistry) if registry is not None: if registry.forInterface(IBlocksSettings).esi: request.environ[ESI_HEADER_KEY] = 'true' root = tree.getroot() headNode = root.find('head') baseURL = request.getURL() if request.getVirtualRoot(): # plone.subrequest deals with VHM requests baseURL = '' for tileNode in utils.headTileXPath(tree): tileHref = tileNode.attrib[utils.tileAttrib] if not tileHref.startswith('/'): tileHref = urljoin(baseURL, tileHref) try: tileTree = utils.resolve(tileHref) except NotFound: continue if tileTree is not None: tileRoot = tileTree.getroot() utils.replace_content(tileNode, tileRoot.find('head')) for tileNode in utils.bodyTileXPath(tree): tileHref = tileNode.attrib[utils.tileAttrib] if not tileHref.startswith('/'): tileHref = urljoin(baseURL, tileHref) try: tileTree = utils.resolve(tileHref) except NotFound: continue if tileTree is not None: tileRoot = tileTree.getroot() tileHead = tileRoot.find('head') if tileHead is not None: for tileHeadChild in tileHead: headNode.append(tileHeadChild) utils.replace_with_children(tileNode, tileRoot.find('body')) return tree
def renderTiles(request, tree): """Find all tiles in the given response, contained in the lxml element tree `tree`, and insert them into the ouput. Assumes panel merging has already happened. """ # Optionally enable ESI rendering in tiles that support this if not request.getHeader(ESI_HEADER): registry = queryUtility(IRegistry) if registry is not None: if registry.forInterface(IBlocksSettings).esi: request.environ[ESI_HEADER_KEY] = 'true' root = tree.getroot() headNode = root.find('head') baseURL = request.getURL() if request.getVirtualRoot(): # plone.subrequest deals with VHM requests baseURL = '' for tileNode in utils.headTileXPath(tree): tileHref = tileNode.attrib[utils.tileAttrib] if not tileHref.startswith('/'): tileHref = urljoin(baseURL, tileHref) try: tileTree = utils.resolve(tileHref) except NotFound: continue if tileTree is not None: tileRoot = tileTree.getroot() utils.replace_content(tileNode, tileRoot.find('head')) for tileNode in utils.bodyTileXPath(tree): tileHref = tileNode.attrib[utils.tileAttrib] if not tileHref.startswith('/'): tileHref = urljoin(baseURL, tileHref) try: tileTree = utils.resolve(tileHref) except NotFound: continue if tileTree is not None: tileRoot = tileTree.getroot() tileHead = tileRoot.find('head') if tileHead is not None: for tileHeadChild in tileHead: headNode.append(tileHeadChild) utils.replace_content(tileNode, tileRoot.find('body')) return tree
def applyTilePersistent(path, resolved): """Append X-Tile-Persistent into resolved layout's tile URLs to allow context specific tile configuration overrides. (Path is required for proper error message when lxml parser fails.) """ from plone.app.blocks.utils import tileAttrib from plone.app.blocks.utils import bodyTileXPath from plone.app.blocks.utils import resolve tree = resolve(path, resolved=resolved) for node in bodyTileXPath(tree): url = node.attrib[tileAttrib] if 'X-Tile-Persistent' not in url: if '?' in url: url += '&X-Tile-Persistent=yes' else: url += '?X-Tile-Persistent=yes' node.attrib[tileAttrib] = url return html.tostring(tree)
def tiles_instances(self): tiles = {} baseURL = self.request.getURL() tree = lxml.html.fromstring(self.content) for panelNode in utils.panelXPath(tree): panelName = panelNode.attrib['data-panel'] for tileNode in utils.bodyTileXPath(panelNode): tileName = tileNode.attrib['data-tile'] tileTree = utils.resolve(urljoin(baseURL, tileName)) tile = tileTree.find('body') if panelName not in tiles.keys(): tiles[panelName] = {} tiles[panelName][tileName] = (tile.text or '') + \ ''.join([lxml.html.tostring(child) for child in tile]) return json.dumps(tiles)
def renderTiles(request, tree): """Find all tiles in the given response, contained in the lxml element tree `tree`, and insert them into the output. Assumes panel merging has already happened. """ # Optionally enable ESI rendering in tiles that support this if not request.getHeader(ESI_HEADER): registry = queryUtility(IRegistry) if registry is not None: if registry.forInterface(IBlocksSettings, check=False).esi: request.environ[ESI_HEADER_KEY] = 'true' root = tree.getroot() headNode = root.find('head') baseURL = request.getURL() if request.getVirtualRoot(): # plone.subrequest deals with VHM requests baseURL = '' for tileNode in utils.headTileXPath(tree): tileHref = tileNode.attrib[utils.tileAttrib] if not tileHref.startswith('/'): tileHref = urljoin(baseURL, tileHref) try: tileTree = utils.resolve(tileHref) except RuntimeError: tileTree = None except NotFound: logger.warn( 'NotFound while trying to render tile: {0}'.format( tileHref ) ) continue if tileTree is not None: tileRoot = tileTree.getroot() utils.replace_with_children(tileNode, tileRoot.find('head')) for tileNode in utils.bodyTileXPath(tree): tileHref = tileNode.attrib[utils.tileAttrib] tileRulesHref = tileNode.attrib.get(utils.tileRulesAttrib) if not tileHref.startswith('/'): tileHref = urljoin(baseURL, tileHref) try: tileTree = utils.resolve(tileHref) except RuntimeError: tileTree = errorTile(request) except NotFound: continue if tileRulesHref: if not tileRulesHref.startswith('/'): tileRulesHref = urljoin(baseURL, tileRulesHref) try: tileTransform = resolve_transform(tileRulesHref, tileNode) except NotFound: tileTransform = None del tileNode.attrib[utils.tileRulesAttrib] else: tileTransform = None if tileTree is not None: tileRoot = tileTree.getroot() tileHead = tileRoot.find('head') tileBody = tileRoot.find('body') if tileHead is None and tileBody is None: tileBody = tileRoot if tileTransform is not None: result = tileTransform(tileBody).getroot() del tileBody[:] tileBody.append(result) if tileHead is not None: for tileHeadChild in tileHead: headNode.append(tileHeadChild) utils.replace_with_children(tileNode, tileBody) return tree