comparison piecrust/data/linker.py @ 977:84fc72a17f7a

sources: Changes in related contents management. - Remove `getParentGroup` method, use related contents instead. - Return only a single group when asked for the parent.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 17 Oct 2017 01:11:54 -0700
parents d9059257743c
children 45ad976712ec
comparison
equal deleted inserted replaced
976:b9374b3682f0 977:84fc72a17f7a
1 import logging 1 import logging
2 from piecrust.data.paginationdata import PaginationData 2 from piecrust.data.paginationdata import PaginationData
3 from piecrust.sources.base import ( 3 from piecrust.sources.base import (
4 REL_LOGICAL_PARENT_ITEM, REL_LOGICAl_CHILD_GROUP) 4 REL_PARENT_GROUP, REL_LOGICAL_PARENT_ITEM, REL_LOGICAl_CHILD_GROUP)
5 5
6 6
7 logger = logging.getLogger(__name__) 7 logger = logging.getLogger(__name__)
8 8
9 9
41 return None 41 return None
42 42
43 @property 43 @property
44 def ancestors(self): 44 def ancestors(self):
45 if self._ancestors is None: 45 if self._ancestors is None:
46 self._ensureParentGroup()
47
48 src = self._source 46 src = self._source
49 app = src.app 47 app = src.app
50 48
51 cur_group = self._parent_group
52 self._ancestors = [] 49 self._ancestors = []
50 cur_group = self._getParentGroup()
53 while cur_group: 51 while cur_group:
54 pi = src.getRelatedContents(cur_group, 52 pi = src.getRelatedContents(cur_group,
55 REL_LOGICAL_PARENT_ITEM) 53 REL_LOGICAL_PARENT_ITEM)
56 if pi is not None: 54 if pi is not None:
57 pipage = app.getPage(src, pi) 55 pipage = app.getPage(src, pi)
58 self._ancestors.append(PaginationData(pipage)) 56 self._ancestors.append(PaginationData(pipage))
59 cur_group = src.getParentGroup(pi) 57 cur_group = src.getRelatedContents(
58 pi, REL_PARENT_GROUP)
60 else: 59 else:
61 break 60 break
62 return self._ancestors 61 return self._ancestors
63 62
64 @property 63 @property
65 def siblings(self): 64 def siblings(self):
66 if self._siblings is None: 65 src = self._source
67 self._ensureParentGroup() 66 app = src.app
68 67 for i in self._getAllSiblings():
69 src = self._source 68 if not i.is_group and i.spec != self._content_item.spec:
70 app = src.app 69 ipage = app.getPage(src, i)
71 70 yield PaginationData(ipage)
72 self._siblings = []
73 for i in src.getContents(self._parent_group):
74 if not i.is_group:
75 ipage = app.getPage(src, i)
76 self._siblings.append(PaginationData(ipage))
77 return self._siblings
78 71
79 @property 72 @property
80 def children(self): 73 def children(self):
81 if self._children is None: 74 src = self._source
82 src = self._source 75 app = src.app
83 app = src.app 76 for i in self._getAllChildren():
84 77 if not i.is_group:
85 self._children = [] 78 ipage = app.getPage(src, i)
86 child_group = src.getRelatedContents(self._content_item, 79 yield PaginationData(ipage)
87 REL_LOGICAl_CHILD_GROUP)
88 if child_group:
89 for i in src.getContents(child_group):
90 ipage = app.getPage(src, i)
91 self._children.append(PaginationData(ipage))
92 return self._children
93 80
94 def forpath(self, path): 81 def forpath(self, path):
95 # TODO: generalize this for sources that aren't file-system based. 82 # TODO: generalize this for sources that aren't file-system based.
96 item = self._source.findContent({'slug': path}) 83 item = self._source.findContent({'slug': path})
97 return Linker(self._source, item) 84 return Linker(self._source, item)
106 if not i.is_group: 93 if not i.is_group:
107 ipage = app.getPage(src, i) 94 ipage = app.getPage(src, i)
108 yield PaginationData(ipage) 95 yield PaginationData(ipage)
109 return None 96 return None
110 97
111 def _ensureParentGroup(self): 98 def _getAllSiblings(self):
99 if self._siblings is None:
100 self._siblings = list(self._source.getContents(
101 self._getParentGroup()))
102 return self._siblings
103
104 def _getAllChildren(self):
105 if self._children is None:
106 child_group = self._source.getRelatedContents(
107 self._content_item, REL_LOGICAl_CHILD_GROUP)
108 if child_group is not None:
109 self._children = list(
110 self._source.getContents(child_group))
111 else:
112 self._children = []
113 return self._children
114
115 def _getParentGroup(self):
112 if self._parent_group is _unloaded: 116 if self._parent_group is _unloaded:
113 src = self._source 117 self._parent_group = self._source.getRelatedContents(
114 item = self._content_item 118 self._content_item, REL_PARENT_GROUP)
115 self._parent_group = src.getParentGroup(item) 119 return self._parent_group
116 120
117 def _debugRenderAncestors(self): 121 def _debugRenderAncestors(self):
118 return [i.title for i in self.ancestors] 122 return [i.title for i in self.ancestors]
119 123
120 def _debugRenderSiblings(self): 124 def _debugRenderSiblings(self):