annotate piecrust/dataproviders/blog.py @ 1070:a013a3bea22a

cm: Upgrade release script.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 13 Feb 2018 23:37:59 -0800
parents 57283302b3ee
children d464c1b1d686
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
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import collections.abc
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
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 class BlogDataProvider(DataProvider, collections.abc.Mapping):
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()
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
46 return self._montly
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 def __getitem__(self, name):
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
49 self._buildArchives()
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
50 return self._taxonomies[name]
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
52 def __getattr__(self, name):
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
53 self._buildArchives()
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
54 try:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
55 return self._taxonomies[name]
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
56 except KeyError:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
57 raise AttributeError("No such taxonomy: %s" % name)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 def __iter__(self):
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
60 self._buildPosts()
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
61 self._buildArchives()
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 905
diff changeset
62 return ['posts', 'years', 'months'] + list(
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 905
diff changeset
63 sorted(self._taxonomies.keys()))
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 def __len__(self):
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
66 self._buildPosts()
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
67 self._buildArchives()
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
68 return 3 + len(self._taxonomies)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 def _debugRenderTaxonomies(self):
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
71 return 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
72
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
73 def _buildPosts(self):
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
74 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
75 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
76 self._onIteration()
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
77 self._posts = it
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
79 def _buildArchives(self):
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
80 if self._archives_built:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
81 return
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
82
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
83 yearly_index = {}
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
84 monthly_index = {}
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
85 tax_index = {}
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
87 taxonomies = []
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
88 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
89 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
90 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
91 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
92 tax_index[tn] = {}
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
94 page = self._page
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
95 source = self._sources[0]
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
96
905
1d0364614665 internal: Sources can cache their pages in addition to their items.
Ludovic Chabant <ludovic@chabant.com>
parents: 874
diff changeset
97 for post in source.getAllPages():
874
f4608e2e80ce data: Optimize page data creation.
Ludovic Chabant <ludovic@chabant.com>
parents: 857
diff changeset
98 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
99
874
f4608e2e80ce data: Optimize page data creation.
Ludovic Chabant <ludovic@chabant.com>
parents: 857
diff changeset
100 year = post_dt.year
f4608e2e80ce data: Optimize page data creation.
Ludovic Chabant <ludovic@chabant.com>
parents: 857
diff changeset
101 month = (post_dt.month, post_dt.year)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 posts_this_year = yearly_index.get(year)
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 if posts_this_year is None:
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 timestamp = time.mktime(
874
f4608e2e80ce data: Optimize page data creation.
Ludovic Chabant <ludovic@chabant.com>
parents: 857
diff changeset
106 (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
107 posts_this_year = BlogArchiveEntry(
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
108 source, page, year, timestamp)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 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
110 posts_this_year._items.append(post.content_item)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
112 posts_this_month = monthly_index.get(month)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 if posts_this_month is None:
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 timestamp = time.mktime(
874
f4608e2e80ce data: Optimize page data creation.
Ludovic Chabant <ludovic@chabant.com>
parents: 857
diff changeset
115 (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
116 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
117 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
118 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
119 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
120 posts_this_month._items.append(post.content_item)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
122 for tax in taxonomies:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
123 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
124 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
125 continue
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
127 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
128 if tax.is_multiple:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
129 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
130 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
131 if entry is None:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
132 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
133 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
134 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
135 else:
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, post_term)
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)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
142 self._yearly = list(sorted(
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
143 yearly_index.values(),
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
144 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
145 self._monthly = list(sorted(
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
146 monthly_index.values(),
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
147 key=lambda e: e.timestamp, reverse=True))
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
148
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
149 self._taxonomies = {}
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
150 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
151 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
152 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
153
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
154 self._onIteration()
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
155
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
156 self._archives_built = True
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
157
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
158 def _onIteration(self):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
159 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
160 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
161 if rcs.current_ctx:
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
162 rcs.current_ctx.addUsedSource(self._sources[0])
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
163 self._ctx_set = True
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
164
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
165
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
166 class BlogArchiveEntry:
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
167 debug_render = ['name', 'timestamp', 'posts']
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
168 debug_render_invoke = ['name', 'timestamp', 'posts']
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
169
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
170 def __init__(self, source, page, name, timestamp):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
171 self.name = name
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
172 self.timestamp = timestamp
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
173 self._source = source
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
174 self._page = page
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
175 self._items = []
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
176 self._iterator = None
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
177
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
178 def __str__(self):
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 905
diff changeset
179 return str(self.name)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
180
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
181 def __int__(self):
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
182 return int(self.name)
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 @property
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
185 def posts(self):
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
186 self._load()
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
187 self._iterator.reset()
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
188 return self._iterator
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 def _load(self):
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
191 if self._iterator is not None:
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
192 return
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
193
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
194 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
195 self._iterator = PageIterator(src, current_page=self._page)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
196
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
198 class BlogTaxonomyEntry:
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
199 debug_render = ['name', 'post_count', 'posts']
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
200 debug_render_invoke = ['name', 'post_count', 'posts']
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
201
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
202 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
203 self.term = term
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
204 self._source = source
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
205 self._page = page
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
206 self._items = []
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
207 self._iterator = None
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
209 def __str__(self):
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
210 return self.term
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
211
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
212 @property
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213 def name(self):
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
214 return self.term
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
215
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
216 @property
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
217 def posts(self):
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
218 self._load()
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
219 self._iterator.reset()
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
220 return self._iterator
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 post_count(self):
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
224 return len(self._items)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
225
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
226 def _load(self):
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
227 if self._iterator is not None:
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
228 return
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
229
857
d231a10d18f9 refactor: Make the data providers and blog archives source functional.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
230 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
231 self._iterator = PageIterator(src, current_page=self._page)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
232