annotate piecrust/dataproviders/blog.py @ 1188:a7c43131d871

bake: Fix file write flushing problem with Python 3.8+ Writing the cache files fails in Python 3.8 because it looks like flushing behaviour has changed. We need to explicitly flush. And even then, in very rare occurrences, it looks like it can still run into racing conditions, so we do a very hacky and ugly "retry" loop when fetching cached data :(
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 15 Jun 2021 22:36:23 -0700
parents c5f1936e9e89
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import time
1088
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
2 import collections.abc
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 from piecrust.dataproviders.base import DataProvider
854
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
4 from piecrust.dataproviders.pageiterator import PageIterator
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
5 from piecrust.sources.list import ListSource
854
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
6 from piecrust.sources.taxonomy import Taxonomy
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
1088
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
9 class BlogDataProvider(DataProvider, collections.abc.Mapping):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 PROVIDER_NAME = 'blog'
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 debug_render_doc = """Provides a list of blog posts and yearly/monthly
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 archives."""
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 debug_render_dynamic = (['_debugRenderTaxonomies'] +
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 DataProvider.debug_render_dynamic)
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16
854
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
17 def __init__(self, source, page):
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
18 super().__init__(source, page)
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
19 self._posts = None
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 self._yearly = None
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 self._monthly = None
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 self._taxonomies = {}
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
23 self._archives_built = False
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 self._ctx_set = False
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
26 def _addSource(self, source):
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
27 raise Exception("The blog data provider doesn't support "
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
28 "combining multiple sources.")
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
29
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 @property
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 def posts(self):
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
32 self._buildPosts()
1033
57283302b3ee data: Fix a bug when listing a blog's posts twice on a page.
Ludovic Chabant <ludovic@chabant.com>
parents: 982
diff changeset
33 # Reset each time on access in case a page is showing 2 different
57283302b3ee data: Fix a bug when listing a blog's posts twice on a page.
Ludovic Chabant <ludovic@chabant.com>
parents: 982
diff changeset
34 # lists of the same blog.
57283302b3ee data: Fix a bug when listing a blog's posts twice on a page.
Ludovic Chabant <ludovic@chabant.com>
parents: 982
diff changeset
35 self._posts.reset()
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
36 return self._posts
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 @property
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 def years(self):
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
40 self._buildArchives()
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
41 return self._yearly
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 @property
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 def months(self):
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
45 self._buildArchives()
1088
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
46 return self._monthly
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47
1080
d464c1b1d686 data: Fix debug rendering of the blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1033
diff changeset
48 @property
d464c1b1d686 data: Fix debug rendering of the blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1033
diff changeset
49 def taxonomies(self):
d464c1b1d686 data: Fix debug rendering of the blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1033
diff changeset
50 return list(self._app.config.get('site/taxonomies').keys())
d464c1b1d686 data: Fix debug rendering of the blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1033
diff changeset
51
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 def __getitem__(self, name):
1088
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
53 if name in ['posts', 'years', 'months', 'taxonomies']:
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
54 return getattr(self, name)
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
55
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
56 self._buildArchives()
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
57 return self._taxonomies[name]
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
59 def __getattr__(self, name):
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
60 self._buildArchives()
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
61 try:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
62 return self._taxonomies[name]
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
63 except KeyError:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
64 raise AttributeError("No such taxonomy: %s" % name)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65
1088
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
66 def __len__(self):
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
67 return 4 + len(self.taxonomies)
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
68
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
69 def __iter__(self):
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
70 yield 'posts'
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
71 yield 'years'
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
72 yield 'months'
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
73 yield 'taxonomies'
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
74 yield from self.taxonomies
c5f1936e9e89 data: Fix recently introduced bug with blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1080
diff changeset
75
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 def _debugRenderTaxonomies(self):
1080
d464c1b1d686 data: Fix debug rendering of the blog data provider.
Ludovic Chabant <ludovic@chabant.com>
parents: 1033
diff changeset
77 return self.taxonomies
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
78
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
79 def _buildPosts(self):
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
80 if self._posts is None:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
81 it = PageIterator(self._sources[0], current_page=self._page)
1033
57283302b3ee data: Fix a bug when listing a blog's posts twice on a page.
Ludovic Chabant <ludovic@chabant.com>
parents: 982
diff changeset
82 self._onIteration()
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
83 self._posts = it
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
85 def _buildArchives(self):
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
86 if self._archives_built:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
87 return
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
88
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
89 yearly_index = {}
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
90 monthly_index = {}
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
91 tax_index = {}
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
93 taxonomies = []
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
94 tax_names = list(self._app.config.get('site/taxonomies').keys())
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
95 for tn in tax_names:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
96 tax_cfg = self._app.config.get('site/taxonomies/' + tn)
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
97 taxonomies.append(Taxonomy(tn, tax_cfg))
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
98 tax_index[tn] = {}
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
100 page = self._page
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
101 source = self._sources[0]
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
102
905
1d0364614665 internal: Sources can cache their pages in addition to their items.
Ludovic Chabant <ludovic@chabant.com>
parents: 874
diff changeset
103 for post in source.getAllPages():
874
f4608e2e80ce data: Optimize page data creation.
Ludovic Chabant <ludovic@chabant.com>
parents: 857
diff changeset
104 post_dt = post.datetime
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
105
874
f4608e2e80ce data: Optimize page data creation.
Ludovic Chabant <ludovic@chabant.com>
parents: 857
diff changeset
106 year = post_dt.year
f4608e2e80ce data: Optimize page data creation.
Ludovic Chabant <ludovic@chabant.com>
parents: 857
diff changeset
107 month = (post_dt.month, post_dt.year)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 posts_this_year = yearly_index.get(year)
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 if posts_this_year is None:
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 timestamp = time.mktime(
874
f4608e2e80ce data: Optimize page data creation.
Ludovic Chabant <ludovic@chabant.com>
parents: 857
diff changeset
112 (post_dt.year, 1, 1, 0, 0, 0, 0, 0, -1))
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
113 posts_this_year = BlogArchiveEntry(
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
114 source, page, year, timestamp)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 yearly_index[year] = posts_this_year
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
116 posts_this_year._items.append(post.content_item)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
118 posts_this_month = monthly_index.get(month)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119 if posts_this_month is None:
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 timestamp = time.mktime(
874
f4608e2e80ce data: Optimize page data creation.
Ludovic Chabant <ludovic@chabant.com>
parents: 857
diff changeset
121 (post_dt.year, post_dt.month, 1,
854
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
122 0, 0, 0, 0, 0, -1))
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
123 posts_this_month = BlogArchiveEntry(
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 905
diff changeset
124 source, page, month[0], timestamp)
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
125 monthly_index[month] = posts_this_month
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
126 posts_this_month._items.append(post.content_item)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
128 for tax in taxonomies:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
129 post_term = post.config.get(tax.setting_name)
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
130 if post_term is None:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
131 continue
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
133 posts_this_tax = tax_index[tax.name]
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
134 if tax.is_multiple:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
135 for val in post_term:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
136 entry = posts_this_tax.get(val)
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
137 if entry is None:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
138 entry = BlogTaxonomyEntry(source, page, val)
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
139 posts_this_tax[val] = entry
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
140 entry._items.append(post.content_item)
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
141 else:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
142 entry = posts_this_tax.get(val)
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
143 if entry is None:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
144 entry = BlogTaxonomyEntry(source, page, post_term)
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
145 posts_this_tax[val] = entry
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
146 entry._items.append(post.content_item)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
147
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
148 self._yearly = list(sorted(
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
149 yearly_index.values(),
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
150 key=lambda e: e.timestamp, reverse=True))
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
151 self._monthly = list(sorted(
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
152 monthly_index.values(),
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
153 key=lambda e: e.timestamp, reverse=True))
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
154
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
155 self._taxonomies = {}
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
156 for tax_name, entries in tax_index.items():
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 905
diff changeset
157 self._taxonomies[tax_name] = list(
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 905
diff changeset
158 sorted(entries.values(), key=lambda i: i.term))
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
159
1033
57283302b3ee data: Fix a bug when listing a blog's posts twice on a page.
Ludovic Chabant <ludovic@chabant.com>
parents: 982
diff changeset
160 self._onIteration()
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
161
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
162 self._archives_built = True
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
163
1033
57283302b3ee data: Fix a bug when listing a blog's posts twice on a page.
Ludovic Chabant <ludovic@chabant.com>
parents: 982
diff changeset
164 def _onIteration(self):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
165 if not self._ctx_set:
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
166 rcs = self._app.env.render_ctx_stack
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
167 if rcs.current_ctx:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
168 rcs.current_ctx.addUsedSource(self._sources[0])
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
169 self._ctx_set = True
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
170
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
171
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
172 class BlogArchiveEntry:
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
173 debug_render = ['name', 'timestamp', 'posts']
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
174 debug_render_invoke = ['name', 'timestamp', 'posts']
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
175
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
176 def __init__(self, source, page, name, timestamp):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
177 self.name = name
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
178 self.timestamp = timestamp
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
179 self._source = source
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
180 self._page = page
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
181 self._items = []
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
182 self._iterator = None
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
183
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
184 def __str__(self):
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 905
diff changeset
185 return str(self.name)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
186
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
187 def __int__(self):
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
188 return int(self.name)
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
189
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
190 @property
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
191 def posts(self):
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
192 self._load()
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
193 self._iterator.reset()
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
194 return self._iterator
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
195
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
196 def _load(self):
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197 if self._iterator is not None:
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
198 return
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
199
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
200 src = ListSource(self._source, self._items)
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
201 self._iterator = PageIterator(src, current_page=self._page)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
202
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
203
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
204 class BlogTaxonomyEntry:
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
205 debug_render = ['name', 'post_count', 'posts']
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
206 debug_render_invoke = ['name', 'post_count', 'posts']
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
207
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
208 def __init__(self, source, page, term):
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
209 self.term = term
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
210 self._source = source
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
211 self._page = page
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
212 self._items = []
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213 self._iterator = None
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
214
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
215 def __str__(self):
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
216 return self.term
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
217
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
218 @property
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
219 def name(self):
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
220 return self.term
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
221
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
222 @property
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
223 def posts(self):
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
224 self._load()
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
225 self._iterator.reset()
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
226 return self._iterator
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
227
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
228 @property
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
229 def post_count(self):
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
230 return len(self._items)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
231
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
232 def _load(self):
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
233 if self._iterator is not None:
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
234 return
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
235
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
236 src = ListSource(self._source, self._items)
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
237 self._iterator = PageIterator(src, current_page=self._page)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
238