Add related lessons.
[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 from librarian.dcparser import WLURI, Person
5
6 register = template.Library()
7
8
9 @register.inclusion_tag("catalogue/snippets/carousel.html")
10 def catalogue_carousel():
11     return {
12     }
13
14 @register.inclusion_tag("catalogue/snippets/section_buttons.html")
15 def catalogue_section_buttons():
16     return {
17         "object_list": Section.objects.all()
18     }
19
20 @register.inclusion_tag("catalogue/snippets/chosen_topics.html")
21 def catalogue_chosen_topics():
22     return {
23     }
24
25 @register.inclusion_tag("catalogue/snippets/section_box.html")
26 def section_box(section):
27     lessons = SortedDict()
28     for lesson in section.lesson_set.all():
29         if lesson.level not in lessons:
30             newdict = SortedDict()
31             newdict['synthetic'] = []
32             newdict['course'] = []
33             lessons[lesson.level] = newdict
34         if lesson.type not in lessons[lesson.level]:
35             lessons[lesson.level][lesson.type] = []
36         lessons[lesson.level][lesson.type].append(lesson)
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.inclusion_tag("catalogue/snippets/lesson_link.html")
56 def lesson_link(uri):
57     return {'lesson': Lesson.objects.get(slug=WLURI(uri).slug)}
58
59 @register.filter
60 def person_list(persons):
61     return u", ".join(Person.from_text(p).readable() for p in persons)
62
63
64 # FIXME: Move to fnpdjango
65 import feedparser
66 import datetime
67 @register.inclusion_tag('catalogue/latest_blog_posts.html')
68 def latest_blog_posts(feed_url, posts_to_show=5):
69     try:
70         feed = feedparser.parse(str(feed_url))
71         posts = []
72         for i in range(posts_to_show):
73             pub_date = feed['entries'][i].updated_parsed
74             published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
75             posts.append({
76                 'title': feed['entries'][i].title,
77                 'summary': feed['entries'][i].summary,
78                 'link': feed['entries'][i].link,
79                 'date': published,
80                 })
81         return {'posts': posts}
82     except:
83         return {'posts': []}