auto-count on main
[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     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     return {
37         "section": section,
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 = Lesson.objects.filter(type='course', level=lesson.level, section=root)
46         mark_level = False
47     else:
48         root = None
49         siblings = Lesson.objects.filter(type=lesson.type)
50         mark_level = True
51     return {
52         "lesson": lesson,
53         "root": root,
54         "siblings": siblings,
55         "mark_level": mark_level
56     }
57
58 @register.inclusion_tag("catalogue/snippets/lesson_link.html")
59 def lesson_link(uri):
60     try:
61         return {'lesson': Lesson.objects.get(slug=WLURI(uri).slug)}
62     except Lesson.DoesNotExist:
63         return {}
64
65 @register.filter
66 def person_list(persons):
67     return u", ".join(Person.from_text(p).readable() for p in persons)
68
69
70 # FIXME: Move to fnpdjango
71 import feedparser
72 import datetime
73 @register.inclusion_tag('catalogue/latest_blog_posts.html')
74 def latest_blog_posts(feed_url, posts_to_show=5):
75     try:
76         feed = feedparser.parse(str(feed_url))
77         posts = []
78         for i in range(posts_to_show):
79             pub_date = feed['entries'][i].updated_parsed
80             published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
81             posts.append({
82                 'title': feed['entries'][i].title,
83                 'summary': feed['entries'][i].summary,
84                 'link': feed['entries'][i].link,
85                 'date': published,
86                 })
87         return {'posts': posts}
88     except:
89         return {'posts': []}