Mercurial > piecrust2
changeset 1058:f6b975db2545
data: Make `family` properties return lists instead of generators.
data: Add ability to return pages and groups.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 13 Feb 2018 11:10:55 -0800 |
parents | fd95fef51705 |
children | 292e3a1316d8 |
files | piecrust/data/linker.py |
diffstat | 1 files changed, 32 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/piecrust/data/linker.py Thu Feb 01 22:03:33 2018 -0800 +++ b/piecrust/data/linker.py Tue Feb 13 11:10:55 2018 -0800 @@ -76,13 +76,15 @@ def siblings(self): src = self._source app = src.app + sibs = [] for i in self._getAllSiblings(): if not i.is_group: ipage = app.getPage(src, i) ipage_data = self._makePageData(ipage) - yield ipage_data + sibs.append(ipage_data) else: - yield self._makeGroupData(i) + sibs.append(self._makeGroupData(i)) + return sibs @property def has_children(self): @@ -92,32 +94,50 @@ def children(self): src = self._source app = src.app + childs = [] for i in self._getAllChildren(): if not i.is_group: ipage = app.getPage(src, i) - yield self._makePageData(ipage) + childs.append(self._makePageData(ipage)) + return childs + + @property + def children_and_groups(self): + src = self._source + app = src.app + childs = [] + for i in self._getAllChildren(): + if not i.is_group: + ipage = app.getPage(src, i) + childs.append(self._makePageData(ipage)) else: - yield self._makeGroupData(i) + childs.append(self._makeGroupData(i)) + return childs def forpath(self, path): # TODO: generalize this for sources that aren't file-system based. - item = self._source.findContentFromSpec({'slug': path}) + item = self._source.findContentFromRoute({'slug': path}) return Linker(self._source, item) - def childrenof(self, path): + def childrenof(self, path, with_groups=False): # TODO: generalize this for sources that aren't file-system based. src = self._source app = src.app - group = src.findContentFromSpec(path) + item = src.findContentFromRoute({'slug': path}) + if item is None: + raise ValueError("No such content: %s" % path) + + group = self._source.getRelatedContents(item, + REL_LOGICAL_CHILD_GROUP) if group is not None: - if not group.is_group: - raise Exception("'%s' is not a folder/group." % path) + childs = [] for i in src.getContents(group): if not i.is_group: ipage = app.getPage(src, i) - yield self._makePageData(ipage) - else: - yield self._makeGroupData(i) + childs.append(self._makePageData(ipage)) + elif with_groups: + childs.append(self._makeGroupData(i)) + return childs return None def _getAllSiblings(self):