cb5c2e7ea9a72af513d2485019631b64030d2964
[wolnelektury.git] / src / wolnelektury / templatetags / blog.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from datetime import date
5 from django.conf import settings
6 from django.template import Library
7 import feedparser
8
9
10 register = Library()
11
12
13 @register.inclusion_tag('latest_blog_posts.html')
14 def latest_blog_posts(feed_url=None, posts_to_show=5):
15     if feed_url is None:
16         feed_url = settings.LATEST_BLOG_POSTS
17     posts = []
18     try:
19         feed = feedparser.parse(str(feed_url))
20         for i in range(posts_to_show):
21             pub_date = feed['entries'][i].published_parsed
22             published = date(pub_date[0], pub_date[1], pub_date[2])
23             posts.append({
24                 'title': feed['entries'][i].title,
25                 'summary': feed['entries'][i].summary,
26                 'link': feed['entries'][i].link,
27                 'date': published,
28                 })
29     except:
30         pass
31     return {
32         'posts': posts
33     }