4e02e042c0fc65c94a80d27ab160297b120549a2
[edumed.git] / curriculum / templatetags / curriculum_tags.py
1 from django import template
2 from django.utils.datastructures import SortedDict
3 from ..models import Competence, Curriculum, CurriculumCourse
4 from catalogue.models import Lesson
5
6 register = template.Library()
7
8
9 @register.inclusion_tag("curriculum/snippets/competence.html")
10 def competence(texts, level):
11     try:
12         comps = [Competence.from_text(text) for text in texts]
13     except:
14         return {'texts': texts}
15     return {
16         'comps': comps,
17         'level': level,
18     }
19
20 @register.inclusion_tag("curriculum/snippets/curriculum.html")
21 def curriculum(identifiers):
22     try:
23         currs = [Curriculum.objects.get(identifier__iexact=identifier.replace(' ', ''))
24                     for identifier in identifiers]
25     except Curriculum.DoesNotExist:
26         return {'identifiers': identifiers}
27
28     currset = SortedDict()
29     for curr in currs:
30         k = curr.course, curr.level
31         if k not in currset:
32             currset[k] = SortedDict()
33         typename = Curriculum.TYPES[curr.type]
34         if typename not in currset[k]:
35             currset[k][typename] = []
36         currset[k][typename].append(curr)
37
38     return {
39         'currset': currset,
40     }
41     
42
43 @register.filter
44 def url_for_level(comp, level):
45     try:
46         return comp.url_for_level(level)
47     except:
48         return comp.get_absolute_url()
49
50
51 @register.inclusion_tag("curriculum/snippets/course_box.html")
52 def course_box(course):
53     lessons = SortedDict()
54     for lesson in course.lesson_set.all():
55         if lesson.level not in lessons:
56             newdict = SortedDict()
57             newdict['synthetic'] = []
58             newdict['course'] = []
59             lessons[lesson.level] = newdict
60         if lesson.type not in lessons[lesson.level]:
61             lessons[lesson.level][lesson.type] = []
62         lessons[lesson.level][lesson.type].append(lesson)
63     return {
64         "course": course,
65         "lessons": lessons,
66     }
67
68 @register.inclusion_tag("curriculum/snippets/course_boxes.html")
69 def course_boxes():
70     return {'object_list': CurriculumCourse.objects.all()}
71
72 @register.inclusion_tag("curriculum/snippets/course_boxes_toc.html")
73 def course_boxes_toc(accusative=False):
74     last = None, None
75     object_list = []
76     for l in Lesson.curriculum_courses.through.objects.all().order_by(
77             'lesson__level', 'curriculumcourse'):
78         level, course = l.lesson.level, l.curriculumcourse
79         if (level, course) == last:
80             continue
81         if level != last[0]:
82             object_list.append((level, []))
83         object_list[-1][1].append(course)
84         last = (level, course)
85     return {
86         'object_list': object_list,
87         'accusative': accusative,
88     }