initial commit
[edumed.git] / catalogue / templatetags / catalogue_tags.py
1 from django import template
2 from django.utils.datastructures import SortedDict
3 from ..models import Section
4
5 register = template.Library()
6
7
8 @register.inclusion_tag("catalogue/snippets/carousel.html")
9 def catalogue_carousel():
10     return {
11     }
12
13 @register.inclusion_tag("catalogue/snippets/section_buttons.html")
14 def catalogue_section_buttons():
15     return {
16         "object_list": Section.objects.all()
17     }
18
19 @register.inclusion_tag("catalogue/snippets/chosen_topics.html")
20 def catalogue_chosen_topics():
21     return {
22     }
23
24 @register.inclusion_tag("catalogue/snippets/section_box.html")
25 def section_box(section):
26     lessons = SortedDict()
27     for lesson in section.lesson_set.all():
28         if lesson.level not in lessons:
29             lessons[lesson.level] = SortedDict()
30         if lesson.depth not in lessons[lesson.level]:
31             lessons[lesson.level][lesson.depth] = []
32         lessons[lesson.level][lesson.depth].append(lesson)
33     return {
34         "lessons": lessons,
35     }
36
37
38 # FIXME: Move to fnpdjango
39 import feedparser
40 import datetime
41 @register.inclusion_tag('catalogue/latest_blog_posts.html')
42 def latest_blog_posts(feed_url, posts_to_show=5):
43     try:
44         feed = feedparser.parse(str(feed_url))
45         posts = []
46         for i in range(posts_to_show):
47             pub_date = feed['entries'][i].updated_parsed
48             published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
49             posts.append({
50                 'title': feed['entries'][i].title,
51                 'summary': feed['entries'][i].summary,
52                 'link': feed['entries'][i].link,
53                 'date': published,
54                 })
55         return {'posts': posts}
56     except:
57         return {'posts': []}