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