Mercurial > piecrust2
comparison piecrust/data/linker.py @ 1055:7e4742a60d14
data: Add new properties and folder support to the `family` data endpoint.
* Add `root` and `myself`.
* Return groups as part of the children.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 31 Jan 2018 08:53:27 -0800 |
parents | 8adc27285d93 |
children | f6b975db2545 |
comparison
equal
deleted
inserted
replaced
1054:75f1b4460491 | 1055:7e4742a60d14 |
---|---|
39 if a: | 39 if a: |
40 return a[0] | 40 return a[0] |
41 return None | 41 return None |
42 | 42 |
43 @property | 43 @property |
44 def root(self): | |
45 a = self.ancestors | |
46 if a: | |
47 return a[-1] | |
48 return self.myself | |
49 | |
50 @property | |
51 def myself(self): | |
52 page = self._source.app.getPage(self._source, self._content_item) | |
53 return self._makePageData(page) | |
54 | |
55 @property | |
44 def ancestors(self): | 56 def ancestors(self): |
45 if self._ancestors is None: | 57 if self._ancestors is None: |
46 src = self._source | 58 src = self._source |
47 app = src.app | 59 app = src.app |
48 | 60 |
51 while cur_group: | 63 while cur_group: |
52 pi = src.getRelatedContents(cur_group, | 64 pi = src.getRelatedContents(cur_group, |
53 REL_LOGICAL_PARENT_ITEM) | 65 REL_LOGICAL_PARENT_ITEM) |
54 if pi is not None: | 66 if pi is not None: |
55 pipage = app.getPage(src, pi) | 67 pipage = app.getPage(src, pi) |
56 self._ancestors.append(PaginationData(pipage)) | 68 self._ancestors.append(self._makePageData(pipage)) |
57 cur_group = src.getRelatedContents( | 69 cur_group = src.getRelatedContents( |
58 pi, REL_PARENT_GROUP) | 70 pi, REL_PARENT_GROUP) |
59 else: | 71 else: |
60 break | 72 break |
61 return self._ancestors | 73 return self._ancestors |
65 src = self._source | 77 src = self._source |
66 app = src.app | 78 app = src.app |
67 for i in self._getAllSiblings(): | 79 for i in self._getAllSiblings(): |
68 if not i.is_group: | 80 if not i.is_group: |
69 ipage = app.getPage(src, i) | 81 ipage = app.getPage(src, i) |
70 ipage_data = PaginationData(ipage) | 82 ipage_data = self._makePageData(ipage) |
71 ipage_data._setValue('is_self', | |
72 i.spec == self._content_item.spec) | |
73 yield ipage_data | 83 yield ipage_data |
84 else: | |
85 yield self._makeGroupData(i) | |
86 | |
87 @property | |
88 def has_children(self): | |
89 return bool(self.children) | |
74 | 90 |
75 @property | 91 @property |
76 def children(self): | 92 def children(self): |
77 src = self._source | 93 src = self._source |
78 app = src.app | 94 app = src.app |
79 for i in self._getAllChildren(): | 95 for i in self._getAllChildren(): |
80 if not i.is_group: | 96 if not i.is_group: |
81 ipage = app.getPage(src, i) | 97 ipage = app.getPage(src, i) |
82 yield PaginationData(ipage) | 98 yield self._makePageData(ipage) |
99 else: | |
100 yield self._makeGroupData(i) | |
83 | 101 |
84 def forpath(self, path): | 102 def forpath(self, path): |
85 # TODO: generalize this for sources that aren't file-system based. | 103 # TODO: generalize this for sources that aren't file-system based. |
86 item = self._source.findContentFromSpec({'slug': path}) | 104 item = self._source.findContentFromSpec({'slug': path}) |
87 return Linker(self._source, item) | 105 return Linker(self._source, item) |
95 if not group.is_group: | 113 if not group.is_group: |
96 raise Exception("'%s' is not a folder/group." % path) | 114 raise Exception("'%s' is not a folder/group." % path) |
97 for i in src.getContents(group): | 115 for i in src.getContents(group): |
98 if not i.is_group: | 116 if not i.is_group: |
99 ipage = app.getPage(src, i) | 117 ipage = app.getPage(src, i) |
100 yield PaginationData(ipage) | 118 yield self._makePageData(ipage) |
119 else: | |
120 yield self._makeGroupData(i) | |
101 return None | 121 return None |
102 | 122 |
103 def _getAllSiblings(self): | 123 def _getAllSiblings(self): |
104 if self._siblings is None: | 124 if self._siblings is None: |
105 self._siblings = list(self._source.getContents( | 125 self._siblings = list(self._source.getContents( |
121 if self._parent_group is _unloaded: | 141 if self._parent_group is _unloaded: |
122 self._parent_group = self._source.getRelatedContents( | 142 self._parent_group = self._source.getRelatedContents( |
123 self._content_item, REL_PARENT_GROUP) | 143 self._content_item, REL_PARENT_GROUP) |
124 return self._parent_group | 144 return self._parent_group |
125 | 145 |
146 def _makePageData(self, page): | |
147 return _PageData(self, page) | |
148 | |
149 def _makeGroupData(self, group): | |
150 return _GroupData(self._source, group) | |
151 | |
126 def _debugRenderAncestors(self): | 152 def _debugRenderAncestors(self): |
127 return [i.title for i in self.ancestors] | 153 return [i.title for i in self.ancestors] |
128 | 154 |
129 def _debugRenderSiblings(self): | 155 def _debugRenderSiblings(self): |
130 return [i.title for i in self.siblings] | 156 return [i.title for i in self.siblings] |
131 | 157 |
132 def _debugRenderChildren(self): | 158 def _debugRenderChildren(self): |
133 return [i.title for i in self.children] | 159 return [i.title for i in self.children] |
134 | 160 |
161 | |
162 class _PageData(PaginationData): | |
163 def __init__(self, linker, page): | |
164 super().__init__(page) | |
165 self._linker = linker | |
166 | |
167 def _load(self): | |
168 super()._load() | |
169 self._mapValue('is_page', True) | |
170 self._mapValue( | |
171 'is_self', | |
172 self._page.content_spec == self._linker._content_item.spec) | |
173 | |
174 self._mapLoader('is_dir', lambda d, n: self._linker.has_children) | |
175 self._mapLoader('is_group', lambda d, n: self._linker.has_children) | |
176 | |
177 | |
178 class _GroupData: | |
179 def __init__(self, source, group_item): | |
180 self._source = source | |
181 self._group_item = group_item | |
182 self.is_page = False | |
183 self.is_dir = True | |
184 self.is_group = True | |
185 self.family = Linker(source, group_item) |