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
6 register = template.Library()
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'
15 lessons_desc = u'kompletnych lekcji'
18 @register.inclusion_tag("catalogue/snippets/section_buttons.html")
19 def catalogue_section_buttons():
21 "object_list": Section.objects.all()
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())
32 min_index, min_list = min(enumerate(lesson_lists), key=lambda x: x[1][0].order)
33 lesson = min_list.pop(0)
35 lesson_lists.pop(min_index)
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)
50 @register.inclusion_tag("catalogue/snippets/lesson_nav.html")
51 def lesson_nav(lesson):
52 if lesson.type == 'course':
54 siblings = Lesson.objects.filter(type='course', level=lesson.level, section=root)
56 link_other_level = True
59 siblings = Lesson.objects.filter(type=lesson.type)
60 mark_level = link_other_level = lesson.type == 'course'
65 "mark_level": mark_level
68 @register.inclusion_tag("catalogue/snippets/lesson_link.html")
71 return {'lesson': Lesson.objects.get(slug=WLURI(uri).slug)}
72 except Lesson.DoesNotExist:
76 def person_list(persons):
77 return u", ".join(Person.from_text(p).readable() for p in persons)
80 # FIXME: Move to fnpdjango
83 @register.inclusion_tag('catalogue/latest_blog_posts.html')
84 def latest_blog_posts(feed_url, posts_to_show=5):
86 feed = feedparser.parse(str(feed_url))
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] )
92 'title': feed['entries'][i].title,
93 'summary': feed['entries'][i].summary,
94 'link': feed['entries'][i].link,
97 return {'posts': posts}