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