Mercurial > piecrust2
comparison piecrust/data/linker.py @ 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 | 7e4742a60d14 |
children | 00a0a65d08e6 |
comparison
equal
deleted
inserted
replaced
1057:fd95fef51705 | 1058:f6b975db2545 |
---|---|
74 | 74 |
75 @property | 75 @property |
76 def siblings(self): | 76 def siblings(self): |
77 src = self._source | 77 src = self._source |
78 app = src.app | 78 app = src.app |
79 sibs = [] | |
79 for i in self._getAllSiblings(): | 80 for i in self._getAllSiblings(): |
80 if not i.is_group: | 81 if not i.is_group: |
81 ipage = app.getPage(src, i) | 82 ipage = app.getPage(src, i) |
82 ipage_data = self._makePageData(ipage) | 83 ipage_data = self._makePageData(ipage) |
83 yield ipage_data | 84 sibs.append(ipage_data) |
84 else: | 85 else: |
85 yield self._makeGroupData(i) | 86 sibs.append(self._makeGroupData(i)) |
87 return sibs | |
86 | 88 |
87 @property | 89 @property |
88 def has_children(self): | 90 def has_children(self): |
89 return bool(self.children) | 91 return bool(self.children) |
90 | 92 |
91 @property | 93 @property |
92 def children(self): | 94 def children(self): |
93 src = self._source | 95 src = self._source |
94 app = src.app | 96 app = src.app |
97 childs = [] | |
95 for i in self._getAllChildren(): | 98 for i in self._getAllChildren(): |
96 if not i.is_group: | 99 if not i.is_group: |
97 ipage = app.getPage(src, i) | 100 ipage = app.getPage(src, i) |
98 yield self._makePageData(ipage) | 101 childs.append(self._makePageData(ipage)) |
102 return childs | |
103 | |
104 @property | |
105 def children_and_groups(self): | |
106 src = self._source | |
107 app = src.app | |
108 childs = [] | |
109 for i in self._getAllChildren(): | |
110 if not i.is_group: | |
111 ipage = app.getPage(src, i) | |
112 childs.append(self._makePageData(ipage)) | |
99 else: | 113 else: |
100 yield self._makeGroupData(i) | 114 childs.append(self._makeGroupData(i)) |
115 return childs | |
101 | 116 |
102 def forpath(self, path): | 117 def forpath(self, path): |
103 # TODO: generalize this for sources that aren't file-system based. | 118 # TODO: generalize this for sources that aren't file-system based. |
104 item = self._source.findContentFromSpec({'slug': path}) | 119 item = self._source.findContentFromRoute({'slug': path}) |
105 return Linker(self._source, item) | 120 return Linker(self._source, item) |
106 | 121 |
107 def childrenof(self, path): | 122 def childrenof(self, path, with_groups=False): |
108 # TODO: generalize this for sources that aren't file-system based. | 123 # TODO: generalize this for sources that aren't file-system based. |
109 src = self._source | 124 src = self._source |
110 app = src.app | 125 app = src.app |
111 group = src.findContentFromSpec(path) | 126 item = src.findContentFromRoute({'slug': path}) |
127 if item is None: | |
128 raise ValueError("No such content: %s" % path) | |
129 | |
130 group = self._source.getRelatedContents(item, | |
131 REL_LOGICAL_CHILD_GROUP) | |
112 if group is not None: | 132 if group is not None: |
113 if not group.is_group: | 133 childs = [] |
114 raise Exception("'%s' is not a folder/group." % path) | |
115 for i in src.getContents(group): | 134 for i in src.getContents(group): |
116 if not i.is_group: | 135 if not i.is_group: |
117 ipage = app.getPage(src, i) | 136 ipage = app.getPage(src, i) |
118 yield self._makePageData(ipage) | 137 childs.append(self._makePageData(ipage)) |
119 else: | 138 elif with_groups: |
120 yield self._makeGroupData(i) | 139 childs.append(self._makeGroupData(i)) |
140 return childs | |
121 return None | 141 return None |
122 | 142 |
123 def _getAllSiblings(self): | 143 def _getAllSiblings(self): |
124 if self._siblings is None: | 144 if self._siblings is None: |
125 self._siblings = list(self._source.getContents( | 145 self._siblings = list(self._source.getContents( |