00959ee5e2b007ec5dab333a845eeebd9c6cbf55
[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
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