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