cf0cc7d717b7d5493176977c177a524368f1b3d5
[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
5 register = template.Library()
6
7
8 @register.inclusion_tag("curriculum/snippets/competence.html")
9 def competence(texts, level):
10     try:
11         comps = [Competence.from_text(text) for text in texts]
12     except:
13         return {'texts': texts}
14     return {
15         'comps': comps,
16         'level': level,
17     }
18
19 @register.inclusion_tag("curriculum/snippets/curriculum.html")
20 def curriculum(identifiers):
21     try:
22         currs = [Curriculum.objects.get(identifier=identifier)
23                     for identifier in identifiers]
24     except Curriculum.DoesNotExist:
25         return {'identifiers': identifiers}
26
27     currset = SortedDict()
28     for curr in currs:
29         k = curr.course, curr.level
30         if k not in currset:
31             currset[k] = SortedDict()
32         typename = Curriculum.TYPES[curr.type]
33         if typename not in currset[k]:
34             currset[k][typename] = []
35         currset[k][typename].append(curr)
36
37     return {
38         'currset': currset,
39     }
40     
41
42 @register.filter
43 def url_for_level(comp, level):
44     try:
45         return comp.url_for_level(level)
46     except:
47         return comp.get_absolute_url()
48
49
50 @register.inclusion_tag("curriculum/snippets/course_box.html")
51 def course_box(course):
52     lessons = SortedDict()
53     for lesson in course.lesson_set.all():
54         if lesson.level not in lessons:
55             newdict = SortedDict()
56             newdict['synthetic'] = []
57             newdict['course'] = []
58             lessons[lesson.level] = newdict
59         if lesson.type not in lessons[lesson.level]:
60             lessons[lesson.level][lesson.type] = []
61         lessons[lesson.level][lesson.type].append(lesson)
62     return {
63         "lessons": lessons,
64     }
65
66 @register.inclusion_tag("curriculum/snippets/course_boxes.html")
67 def course_boxes():
68     return {'object_list': CurriculumCourse.objects.all()}
69
70 @register.inclusion_tag("curriculum/snippets/course_boxes_toc.html")
71 def course_boxes_toc(accusative=False):
72     return {
73         'object_list': CurriculumCourse.objects.all(),
74         'accusative': accusative,
75     }