Various minor changes
[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/section_box.html")
21 def section_box(section):
22     lessons = SortedDict()
23     for lesson in section.lesson_set.all():
24         if lesson.level not in lessons:
25             newdict = SortedDict()
26             newdict['synthetic'] = []
27             newdict['course'] = []
28             lessons[lesson.level] = newdict
29         if lesson.type not in lessons[lesson.level]:
30             lessons[lesson.level][lesson.type] = []
31         lessons[lesson.level][lesson.type].append(lesson)
32     return {
33         "section": section,
34         "lessons": lessons,
35     }
36
37 @register.inclusion_tag("catalogue/snippets/lesson_nav.html")
38 def lesson_nav(lesson):
39     if lesson.type == 'course':
40         root = lesson.section
41         siblings = root.lesson_set.filter(type='course', level=lesson.level)
42         mark_level = False
43     else:
44         root = None
45         siblings = Lesson.objects.filter(type=lesson.type)
46         mark_level = True
47     return {
48         "lesson": lesson,
49         "root": root,
50         "siblings": siblings,
51         "mark_level": mark_level
52     }
53
54 @register.inclusion_tag("catalogue/snippets/lesson_link.html")
55 def lesson_link(uri):
56     try:
57         return {'lesson': Lesson.objects.get(slug=WLURI(uri).slug)}
58     except Lesson.DoesNotExist:
59         return {}
60
61 @register.filter
62 def person_list(persons):
63     return u", ".join(Person.from_text(p).readable() for p in persons)
64
65
66 # FIXME: Move to fnpdjango
67 import feedparser
68 import datetime
69 @register.inclusion_tag('catalogue/latest_blog_posts.html')
70 def latest_blog_posts(feed_url, posts_to_show=5):
71     try:
72         feed = feedparser.parse(str(feed_url))
73         posts = []
74         for i in range(posts_to_show):
75             pub_date = feed['entries'][i].updated_parsed
76             published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
77             posts.append({
78                 'title': feed['entries'][i].title,
79                 'summary': feed['entries'][i].summary,
80                 'link': feed['entries'][i].link,
81                 'date': published,
82                 })
83         return {'posts': posts}
84     except:
85         return {'posts': []}