983ceac8021b386ae2aaa92ed2caa2314a204fa2
[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     # shouldn't be needed, but is
26     identifiers = [id for id in identifiers if id]
27     try:
28         currs = [Curriculum.objects.get(identifier__iexact=identifier.replace(' ', ''))
29                  for identifier in identifiers]
30     except Curriculum.DoesNotExist:
31         return {'identifiers': identifiers}
32
33     currset = SortedDict()
34     for curr in currs:
35         k = curr.course, curr.level
36         if k not in currset:
37             currset[k] = SortedDict()
38         typename = Curriculum.TYPES[curr.type]
39         if typename not in currset[k]:
40             currset[k][typename] = []
41         currset[k][typename].append(curr)
42
43     return {
44         'currset': currset,
45     }
46     
47
48 @register.filter
49 def url_for_level(comp, level):
50     try:
51         return comp.url_for_level(level)
52     except:
53         # WTF
54         return comp.get_absolute_url()
55
56
57 @register.inclusion_tag("curriculum/snippets/course_box.html")
58 def course_box(course):
59     lessons = SortedDict()
60     for lesson in course.lesson_set.all():
61         if lesson.level not in lessons:
62             newdict = SortedDict()
63             newdict['synthetic'] = []
64             newdict['course'] = []
65             lessons[lesson.level] = newdict
66         if lesson.type not in lessons[lesson.level]:
67             lessons[lesson.level][lesson.type] = []
68         lessons[lesson.level][lesson.type].append(lesson)
69     return {
70         "course": course,
71         "lessons": lessons,
72     }
73
74
75 @register.inclusion_tag("curriculum/snippets/course_boxes.html")
76 def course_boxes():
77     return {'object_list': CurriculumCourse.objects.all()}
78
79
80 @register.inclusion_tag("curriculum/snippets/course_boxes_toc.html")
81 def course_boxes_toc(accusative=False):
82     last = None, None
83     object_list = []
84     lessons = Lesson.curriculum_courses.through.objects\
85         .select_related('lesson__level', 'curriculumcourse')\
86         .order_by('lesson__level', 'curriculumcourse')
87     for l in lessons:
88         level, course = l.lesson.level, l.curriculumcourse
89         if (level, course) == last:
90             continue
91         if level != last[0]:
92             object_list.append((level, []))
93         object_list[-1][1].append(course)
94         last = (level, course)
95     return {
96         'object_list': object_list,
97         'accusative': accusative,
98     }