f3e9279a842e8443dadb184b5923777aad6d3384
[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, new=False):
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         'new': new,
46     }
47     
48
49 @register.filter
50 def url_for_level(comp, level):
51     try:
52         return comp.url_for_level(level)
53     except:
54         # WTF
55         return comp.get_absolute_url()
56
57
58 @register.inclusion_tag("curriculum/snippets/course_box.html")
59 def course_box(course):
60     lessons = SortedDict()
61     for lesson in course.lesson_set.all():
62         if lesson.level not in lessons:
63             newdict = SortedDict()
64             newdict['synthetic'] = []
65             newdict['course'] = []
66             lessons[lesson.level] = newdict
67         if lesson.type not in lessons[lesson.level]:
68             lessons[lesson.level][lesson.type] = []
69         lessons[lesson.level][lesson.type].append(lesson)
70     return {
71         "course": course,
72         "lessons": lessons,
73     }
74
75
76 @register.inclusion_tag("curriculum/snippets/course_boxes.html")
77 def course_boxes():
78     return {'object_list': CurriculumCourse.objects.all()}
79
80
81 @register.inclusion_tag("curriculum/snippets/course_boxes_toc.html")
82 def course_boxes_toc(accusative=False):
83     last = None, None
84     object_list = []
85     lessons = Lesson.curriculum_courses.through.objects\
86         .select_related('lesson__level', 'curriculumcourse')\
87         .order_by('lesson__level', 'curriculumcourse')
88     for l in lessons:
89         level, course = l.lesson.level, l.curriculumcourse
90         if (level, course) == last:
91             continue
92         if level != last[0]:
93             object_list.append((level, []))
94         object_list[-1][1].append(course)
95         last = (level, course)
96     return {
97         'object_list': object_list,
98         'accusative': accusative,
99     }