Importing whole sections.
[edumed.git] / catalogue / templatetags / catalogue_tags.py
1 from django import template
2 from django.utils.datastructures import SortedDict
3 from ..models import Lesson, 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             newdict = SortedDict()
30             newdict['synthetic'] = []
31             newdict['course'] = []
32             lessons[lesson.level] = newdict
33         if lesson.type not in lessons[lesson.level]:
34             lessons[lesson.level][lesson.type] = []
35         lessons[lesson.level][lesson.type].append(lesson)
36     print lessons
37     return {
38         "lessons": lessons,
39     }
40
41 @register.inclusion_tag("catalogue/snippets/lesson_nav.html")
42 def lesson_nav(lesson):
43     if lesson.type == 'course':
44         root = lesson.section
45         siblings = root.lesson_set.filter(type='course')
46     else:
47         root = None
48         siblings = Lesson.objects.filter(type=lesson.type)
49     return {
50         "lesson": lesson,
51         "root": root,
52         "siblings": siblings,
53     }
54
55 @register.filter
56 def person_list(persons):
57     from librarian.dcparser import Person
58     return u", ".join(Person.from_text(p).readable() for p in persons)
59
60
61 # FIXME: Move to fnpdjango
62 import feedparser
63 import datetime
64 @register.inclusion_tag('catalogue/latest_blog_posts.html')
65 def latest_blog_posts(feed_url, posts_to_show=5):
66     try:
67         feed = feedparser.parse(str(feed_url))
68         posts = []
69         for i in range(posts_to_show):
70             pub_date = feed['entries'][i].updated_parsed
71             published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
72             posts.append({
73                 'title': feed['entries'][i].title,
74                 'summary': feed['entries'][i].summary,
75                 'link': feed['entries'][i].link,
76                 'date': published,
77                 })
78         return {'posts': posts}
79     except:
80         return {'posts': []}