W trybie naglym.
[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     }
25
26
27 @register.inclusion_tag("catalogue/snippets/level_box.html")
28 def level_box(level):
29     lessons = dict(
30         synthetic = [],
31         course = SortedDict(),
32         project = [],
33     )
34     by_course = defaultdict(lambda: defaultdict(list))
35
36     lesson_lists = [alist for alist in [
37         list(level.lesson_set.exclude(type='appendix').order_by('section__order', 'order')),
38         list(level.lessonstub_set.all())
39     ] if alist]
40
41     while lesson_lists:
42         min_index, min_list = min(enumerate(lesson_lists), key=lambda x: x[1][0].order)
43         lesson = min_list.pop(0)
44         if not min_list:
45             lesson_lists.pop(min_index)
46
47         if lesson.type == 'course':
48             if lesson.section not in lessons['course']:
49                 lessons['course'][lesson.section] = []
50             lessons['course'][lesson.section].append(lesson)
51         elif lesson.type == 'added': continue
52         else:
53             lessons[lesson.type].append(lesson)
54
55         if hasattr(lesson, 'curriculum_courses'):
56             for course in lesson.curriculum_courses.all():
57                 by_course[course][lesson.type].append(lesson)
58
59     courses = [(course, by_course[course]) for course in
60         CurriculumCourse.objects.filter(lesson__level=level).distinct()]
61
62     added = []
63     if level.slug == 'liceum':
64         added.append({
65             'slug': 'filmowa',
66             'title': u'Edukacja filmowa',
67             'lessons': [
68                 Lesson.objects.get(slug=s) for s in [
69 'film-co-to-wlasciwie-jest',
70 'scenariusz-scenopis-i-srodki-realizacyjne',
71 'kompozycja-obrazu-filmowego',
72 'praca-kamery-kadr-kat',
73 'montaz-materialu-filmowego',
74 'swiatlo-i-dzwiek-w-filmie',
75 'scenografia-charakteryzacja-kostiumy-i-aktorzy',
76 'narracja-w-filmie-tekst-i-fabula',
77                 ]
78             ],
79             })
80
81     return {
82         "level": level,
83         "lessons": lessons,
84         "courses": courses,
85         "added": added,
86     }
87
88 @register.inclusion_tag("catalogue/snippets/lesson_nav.html")
89 def lesson_nav(lesson):
90     if lesson.type == 'course':
91         root = lesson.section
92         siblings = Lesson.objects.filter(type='course', level=lesson.level, section=root)
93     elif lesson.type == 'appendix':
94         root = None
95         siblings = Lesson.objects.filter(type=lesson.type)
96     elif lesson.type == 'added':
97         root = None
98         siblings = [
99                 Lesson.objects.get(slug=s) for s in [
100 'film-co-to-wlasciwie-jest',
101 'scenariusz-scenopis-i-srodki-realizacyjne',
102 'kompozycja-obrazu-filmowego',
103 'praca-kamery-kadr-kat',
104 'montaz-materialu-filmowego',
105 'swiatlo-i-dzwiek-w-filmie',
106 'scenografia-charakteryzacja-kostiumy-i-aktorzy',
107 'narracja-w-filmie-tekst-i-fabula',
108                 ]
109             ]
110     else:
111         root = None
112         siblings = Lesson.objects.filter(type=lesson.type, level=lesson.level)
113     return {
114         "lesson": lesson,
115         "root": root,
116         "siblings": siblings,
117     }
118
119 @register.inclusion_tag("catalogue/snippets/lesson_link.html")
120 def lesson_link(uri):
121     try:
122         return {'lesson': Lesson.objects.get(slug=WLURI(uri).slug)}
123     except Lesson.DoesNotExist:
124         return {}
125
126 @register.filter
127 def person_list(persons):
128     return u", ".join(Person.from_text(p).readable() for p in persons)
129
130
131 # FIXME: Move to fnpdjango
132 import feedparser
133 import datetime
134 @register.inclusion_tag('catalogue/latest_blog_posts.html')
135 def latest_blog_posts(feed_url, posts_to_show=5):
136     try:
137         feed = feedparser.parse(str(feed_url))
138         posts = []
139         for i in range(posts_to_show):
140             pub_date = feed['entries'][i].updated_parsed
141             published = datetime.date(pub_date[0], pub_date[1], pub_date[2] )
142             posts.append({
143                 'title': feed['entries'][i].title,
144                 'summary': feed['entries'][i].summary,
145                 'link': feed['entries'][i].link,
146                 'date': published,
147                 })
148         return {'posts': posts}
149     except:
150         return {'posts': []}