a8d69e6e7e70ca237f17ad9fb79bfaa275e7059e
[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     lessons_count = Lesson.objects.filter(type__in=('course', 'synthetic')).count()
12     if 1 < lessons_count % 10 < 5 and lessons_count / 10 % 10 != 1:
13         lessons_desc = u'kompletne lekcje'
14     else:
15         lessons_desc = u'kompletnych lekcji'
16     return locals()
17
18 @register.inclusion_tag("catalogue/snippets/section_buttons.html")
19 def catalogue_section_buttons():
20     return {
21         "object_list": Section.objects.all()
22     }
23
24 @register.inclusion_tag("catalogue/snippets/section_box.html")
25 def section_box(section):
26     lessons = SortedDict()
27     lesson_lists = [alist for alist in [
28         list(section.lesson_set.all()),
29         list(section.lessonstub_set.all())
30     ] if alist]
31     while lesson_lists:
32         min_index, min_list = min(enumerate(lesson_lists), key=lambda x: x[1][0].order)
33         lesson = min_list.pop(0)
34         if not min_list:
35             lesson_lists.pop(min_index)
36
37         if lesson.level not in lessons:
38             newdict = SortedDict()
39             newdict['synthetic'] = []
40             newdict['course'] = []
41             lessons[lesson.level] = newdict
42         if lesson.type not in lessons[lesson.level]:
43             lessons[lesson.level][lesson.type] = []
44         lessons[lesson.level][lesson.type].append(lesson)
45     return {
46         "section": section,
47         "lessons": lessons,
48     }
49
50 @register.inclusion_tag("catalogue/snippets/lesson_nav.html")
51 def lesson_nav(lesson):
52     if lesson.type == 'course':
53         root = lesson.section
54         siblings = Lesson.objects.filter(type='course', level=lesson.level, section=root)
55         mark_level = False
56         link_other_level = True
57     else:
58         root = None
59         siblings = Lesson.objects.filter(type=lesson.type)
60         mark_level = link_other_level = lesson.type == 'course'
61     return {
62         "lesson": lesson,
63         "root": root,
64         "siblings": siblings,
65         "mark_level": mark_level
66     }
67
68 @register.inclusion_tag("catalogue/snippets/lesson_link.html")
69 def lesson_link(uri):
70     try:
71         return {'lesson': Lesson.objects.get(slug=WLURI(uri).slug)}
72     except Lesson.DoesNotExist:
73         return {}
74
75 @register.filter
76 def person_list(persons):
77     return u", ".join(Person.from_text(p).readable() for p in persons)
78
79
80 # FIXME: Move to fnpdjango
81 import feedparser
82 import datetime
83 @register.inclusion_tag('catalogue/latest_blog_posts.html')
84 def latest_blog_posts(feed_url, posts_to_show=5):
85     try:
86         feed = feedparser.parse(str(feed_url))
87         posts = []
88         for i in range(posts_to_show):
89             pub_date = feed['entries'][i].updated_parsed
90             published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
91             posts.append({
92                 'title': feed['entries'][i].title,
93                 'summary': feed['entries'][i].summary,
94                 'link': feed['entries'][i].link,
95                 'date': published,
96                 })
97         return {'posts': posts}
98     except:
99         return {'posts': []}