Implement period_archives common context variable

Also, set default patterns for time-period *_ARCHIVE_URL settings.
This commit is contained in:
DJ Ramones 2023-06-18 11:07:39 +08:00
commit 5214248344
No known key found for this signature in database
GPG key ID: D0EB42161D927EB0
5 changed files with 256 additions and 70 deletions

View file

@ -405,6 +405,103 @@ class TestArticlesGenerator(unittest.TestCase):
self.assertIn(custom_template, self.articles)
self.assertIn(standard_template, self.articles)
def test_period_archives_context(self):
"""Test correctness of the period_archives context values."""
old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, 'C')
settings = get_settings()
settings['CACHE_PATH'] = self.temp_cache
# No period archives enabled:
context = get_context(settings)
generator = ArticlesGenerator(
context=context, settings=settings,
path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
generator.generate_context()
period_archives = generator.context['period_archives']
self.assertEqual(len(period_archives.items()), 0)
# Year archives enabled:
settings['YEAR_ARCHIVE_SAVE_AS'] = 'posts/{date:%Y}/index.html'
settings['YEAR_ARCHIVE_URL'] = 'posts/{date:%Y}/'
context = get_context(settings)
generator = ArticlesGenerator(
context=context, settings=settings,
path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
generator.generate_context()
period_archives = generator.context['period_archives']
self.assertEqual(len(period_archives.items()), 1)
self.assertIn('year', period_archives.keys())
archive_years = [p['period'][0] for p in period_archives['year']]
self.assertIn(1970, archive_years)
self.assertIn(2014, archive_years)
# Month archives enabled:
settings['MONTH_ARCHIVE_SAVE_AS'] = \
'posts/{date:%Y}/{date:%b}/index.html'
settings['MONTH_ARCHIVE_URL'] = \
'posts/{date:%Y}/{date:%b}/'
context = get_context(settings)
generator = ArticlesGenerator(
context=context, settings=settings,
path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
generator.generate_context()
period_archives = generator.context['period_archives']
self.assertEqual(len(period_archives.items()), 2)
self.assertIn('month', period_archives.keys())
month_archives_tuples = [p['period'] for p in period_archives['month']]
self.assertIn((1970, 'January'), month_archives_tuples)
self.assertIn((2014, 'February'), month_archives_tuples)
# Day archives enabled:
settings['DAY_ARCHIVE_SAVE_AS'] = \
'posts/{date:%Y}/{date:%b}/{date:%d}/index.html'
settings['DAY_ARCHIVE_URL'] = \
'posts/{date:%Y}/{date:%b}/{date:%d}/'
context = get_context(settings)
generator = ArticlesGenerator(
context=context, settings=settings,
path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
generator.generate_context()
period_archives = generator.context['period_archives']
self.assertEqual(len(period_archives.items()), 3)
self.assertIn('day', period_archives.keys())
day_archives_tuples = [p['period'] for p in period_archives['day']]
self.assertIn((1970, 'January', 1), day_archives_tuples)
self.assertIn((2014, 'February', 9), day_archives_tuples)
# Further item values tests
filtered_archives = [
p for p in period_archives['day']
if p['period'] == (2014, 'February', 9)
]
self.assertEqual(len(filtered_archives), 1)
sample_archive = filtered_archives[0]
self.assertEqual(sample_archive['period_num'], (2014, 2, 9))
self.assertEqual(
sample_archive['save_as'], 'posts/2014/Feb/09/index.html')
self.assertEqual(
sample_archive['url'], 'posts/2014/Feb/09/')
articles = [
d for d in generator.articles if
d.date.year == 2014 and
d.date.month == 2 and
d.date.day == 9
]
self.assertEqual(len(sample_archive['articles']), len(articles))
dates = [
d for d in generator.dates if
d.date.year == 2014 and
d.date.month == 2 and
d.date.day == 9
]
self.assertEqual(len(sample_archive['dates']), len(dates))
self.assertEqual(sample_archive['dates'][0].title, dates[0].title)
self.assertEqual(sample_archive['dates'][0].date, dates[0].date)
locale.setlocale(locale.LC_ALL, old_locale)
def test_period_in_timeperiod_archive(self):
"""
Test that the context of a generated period_archive is passed