add submodule librarian
[edumed.git] / catalogue / templatetags / catalogue_tags.py
1 from collections import defaultdict
2 from django import template
3 from django.utils.datastructures import SortedDict
4 from ..models import Lesson, Section
5 from curriculum.models import Level, CurriculumCourse
6 from librarian.dcparser import WLURI, Person
7
8 register = template.Library()
9
10
11 @register.inclusion_tag("catalogue/snippets/carousel.html")
12 def catalogue_carousel():
13     return {
14         "object_list": Section.objects.all()
15     }
16
17 @register.inclusion_tag("catalogue/snippets/levels_main.html")
18 def catalogue_levels_main():
19     object_list = Level.objects.exclude(lesson=None)
20     c = object_list.count()
21     return {
22         'object_list': object_list,
23         #'section_width': (700 - 20 * (c - 1)) / c,
24         'section_width': (700 - 20 * 2) / 3
25     }
26
27
28 @register.inclusion_tag("catalogue/snippets/level_box.html")
29 def level_box(level):
30     lessons = dict(
31         synthetic = [],
32         course = SortedDict(),
33         project = [],
34     )
35     by_course = defaultdict(lambda: defaultdict(list))
36
37     lesson_lists = [alist for alist in [
38         list(level.lesson_set.exclude(type='appendix').order_by('section__order', 'order')),
39         list(level.lessonstub_set.all())
40     ] if alist]
41
42     while lesson_lists:
43         min_index, min_list = min(enumerate(lesson_lists), key=lambda x: x[1][0].order)
44         lesson = min_list.pop(0)
45         if not min_list:
46             lesson_lists.pop(min_index)
47
48         if lesson.type == 'course':
49             if lesson.section not in lessons['course']:
50                 lessons['course'][lesson.section] = []
51             lessons['course'][lesson.section].append(lesson)
52         elif lesson.type.startswith('added'): continue
53         else:
54             lessons[lesson.type].append(lesson)
55
56         if hasattr(lesson, 'curriculum_courses'):
57             for course in lesson.curriculum_courses.all():
58                 by_course[course][lesson.type].append(lesson)
59
60     courses = [(course, by_course[course]) for course in
61         CurriculumCourse.objects.filter(lesson__level=level).distinct()]
62
63     added = []
64     if level.slug == 'liceum':
65         added.append({
66             'slug': 'filmowa',
67             'title': u'Edukacja filmowa',
68             'lessons': [
69                 Lesson.objects.get(slug=s) for s in [
70 'film-co-to-wlasciwie-jest',
71 'scenariusz-scenopis-i-srodki-realizacyjne',
72 'kompozycja-obrazu-filmowego',
73 'praca-kamery-kadr-kat',
74 'montaz-materialu-filmowego',
75 'swiatlo-i-dzwiek-w-filmie',
76 'scenografia-charakteryzacja-kostiumy-i-aktorzy',
77 'narracja-w-filmie-tekst-i-fabula',
78                 ]
79             ],
80             })
81         added.append({
82             'slug': 'varsaviana',
83             'title': u'Edukacja varsavianistyczna',
84             'lessons': [
85                 Lesson.objects.get(slug=s) for s in
86 '''
87 czego-prus-w-lalce-o-zydach-nie-powiedzial
88 jak-zmienila-sie-warszawa-o-dworcu-dawniej-i-dzis
89 o-gwarze-praskiej
90 poznaj-i-pokaz-prage
91 praga-trzech-religii
92 sladami-zydow-w-warszawie
93 tajemnice-palacu-saskiego
94 warszawa-przedwojenne-miasto-neonow
95 warszawski-barok
96 ziemianska-jako-soczewka-swiata-lat-miedzywojennych
97 '''.strip().split()
98             ],
99             })
100
101
102     return {
103         "level": level,
104         "lessons": lessons,
105         "courses": courses,
106         "added": added,
107     }
108
109 @register.inclusion_tag("catalogue/snippets/lesson_nav.html")
110 def lesson_nav(lesson):
111     if lesson.type == 'course':
112         root = lesson.section
113         siblings = Lesson.objects.filter(type='course', level=lesson.level, section=root)
114     elif lesson.type == 'appendix':
115         root = None
116         siblings = Lesson.objects.filter(type=lesson.type)
117     elif lesson.type == 'added':
118         root = None
119         siblings = [
120                 Lesson.objects.get(slug=s) for s in [
121 'film-co-to-wlasciwie-jest',
122 'scenariusz-scenopis-i-srodki-realizacyjne',
123 'kompozycja-obrazu-filmowego',
124 'praca-kamery-kadr-kat',
125 'montaz-materialu-filmowego',
126 'swiatlo-i-dzwiek-w-filmie',
127 'scenografia-charakteryzacja-kostiumy-i-aktorzy',
128 'narracja-w-filmie-tekst-i-fabula',
129                 ]
130             ]
131     else:
132         root = None
133         siblings = Lesson.objects.filter(type=lesson.type, level=lesson.level)
134     return {
135         "lesson": lesson,
136         "root": root,
137         "siblings": siblings,
138     }
139
140 @register.inclusion_tag("catalogue/snippets/lesson_link.html")
141 def lesson_link(uri):
142     try:
143         return {'lesson': Lesson.objects.get(slug=WLURI(uri).slug)}
144     except Lesson.DoesNotExist:
145         return {}
146
147 @register.filter
148 def person_list(persons):
149     return u", ".join(Person.from_text(p).readable() for p in persons)
150
151
152 # FIXME: Move to fnpdjango
153 import feedparser
154 import datetime
155 @register.inclusion_tag('catalogue/latest_blog_posts.html')
156 def latest_blog_posts(feed_url, posts_to_show=5):
157     try:
158         feed = feedparser.parse(str(feed_url))
159         posts = []
160         for i in range(posts_to_show):
161             pub_date = feed['entries'][i].updated_parsed
162             published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
163             posts.append({
164                 'title': feed['entries'][i].title,
165                 'summary': feed['entries'][i].summary,
166                 'link': feed['entries'][i].link,
167                 'date': published,
168                 })
169         return {'posts': posts}
170     except:
171         return {'posts': []}