Created
December 12, 2013 16:56
-
-
Save hvelarde/7931259 to your computer and use it in GitHub Desktop.
collective.cover API draft methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import json | |
def get_tiles_from_layout(layout): | |
"""Traverse a layout tree and return a list of tiles on it. | |
:param layout: a JSON object describing the layout | |
:type layout: list | |
:returns: a list of tiles; each tile is described as {id, type} | |
""" | |
assert isinstance(layout, list) | |
tiles = [] | |
for element in layout: | |
if element['type'] == 'tile': | |
tile = dict(id=element['id'], type=element['tile-type']) | |
tiles.append(tile) | |
if 'children' in element: | |
tiles.extend(get_tiles_from_layout(element['children'])) | |
return tiles | |
def get_tiles(cover): | |
"""Return a list of tiles on a cover. | |
:param cover: the cover object | |
:type cover: collective.cover.content Dexterity-based content type | |
:returns: a list of tiles as {id, type} | |
""" | |
assert cover.portal_type == 'collective.cover.content' | |
layout = json.loads(cover.cover_layout) | |
return get_tiles_from_layout(layout) | |
def list_tiles(cover): | |
"""Return a list of tile id on a cover. | |
:param cover: the cover object | |
:type cover: collective.cover.content Dexterity-based content type | |
:returns: a list of tile ids | |
""" | |
return [t['id'] for t in get_tiles(cover)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment