/
aggregations.py
71 lines (59 loc) · 2.88 KB
/
aggregations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
This plugin provides the data for the "latest"-boxes on the frontpage. It has
to come after the event_info plugin as this is prepares the event data which
is used here.
"""
from pelican import generators
from pelican import signals
from pelican.contents import Article
from collections import defaultdict
_categories = defaultdict(dict)
_speakers = defaultdict(dict)
_tags = defaultdict(dict)
CATEGORY_BLACKLIST = {'Undefined'}
SPEAKER_BLACKLIST = {'Unknown'}
TAGS_BLACKLIST = {'pycon'}
def _handle_content_object_init(obj):
if isinstance(obj, Article):
category = getattr(obj, 'category')
if category and category.slug:
if category.name not in CATEGORY_BLACKLIST:
count = _categories[category.slug].get('count', 0)
latest = _categories[category.slug].get('latest')
if not latest or obj.date > latest:
latest = obj.date
_categories[category.slug]['count'] = count + 1
_categories[category.slug]['latest'] = latest
_categories[category.slug]['name'] = category.name
speaker = getattr(obj, 'author')
if speaker.name not in SPEAKER_BLACKLIST:
count = _speakers[speaker.slug].get('count', 0)
_speakers[speaker.slug]['name'] = speaker.name
_speakers[speaker.slug]['slug'] = speaker.slug
_speakers[speaker.slug]['url'] = speaker.url
_speakers[speaker.slug]['count'] = count + 1
tags = getattr(obj, 'tags', ())
if tags:
for tag in tags:
if tag.name not in TAGS_BLACKLIST:
count = _tags[tag.slug].get('count', 0)
_tags[tag.slug]['name'] = tag.name
_tags[tag.slug]['slug'] = tag.slug
_tags[tag.slug]['url'] = tag.url
_tags[tag.slug]['count'] = count + 1
def _inject_aggregates(generator):
if isinstance(generator, generators.ArticlesGenerator):
latest_categories = sorted(_categories.values(), key=lambda x: x.get('latest'), reverse=True)[:5]
active_speakers = sorted(_speakers.values(), key=lambda x: x.get('count'), reverse=True)
active_tags = sorted(_tags.values(), key=lambda x: x.get('count'), reverse=True)
# The actual category URL has to be fetched in the
# article_generator_finalized hook in order for the event_info plugin to
# have done its magic.
for category in latest_categories:
category['url'] = generator.event_by_name[category['name']].url
generator.context['latest_categories'] = latest_categories
generator.context['active_speakers'] = active_speakers
generator.context['active_tags'] = active_tags
def register():
signals.content_object_init.connect(_handle_content_object_init)
signals.article_generator_finalized.connect(_inject_aggregates)