Introducing edumed.coffee from redakcja repository
[edumed.git] / curriculum / views.py
1 # -*- coding: utf-8
2 from django.db import models
3 from django.views.generic import DetailView, ListView
4 from django.utils.datastructures import SortedDict
5 from django.utils.translation import ugettext as _
6
7 from .models import Competence, Section, Level, CompetenceLevel
8
9
10 class CompetenceDetailView(DetailView):
11     model = Competence
12
13
14 class CompetencesView(ListView):
15     model = Competence
16
17     def get_context_data(self, **kwargs):
18         context = super(CompetencesView, self).get_context_data(**kwargs)
19         
20         context['levels'] = SortedDict()
21         for level in Level.objects.all():
22             context['levels'].setdefault(level.group, []).append(level)
23
24         context['sections'] = Section.objects.all()
25
26         errors = {}
27
28         try:
29             level = Level.objects.get(slug=self.request.GET.get('level'))
30         except Level.DoesNotExist:
31             level = None
32         context['level'] = level
33
34         comp_ids = set()
35         for c in self.request.GET.getlist('c'):
36             try:
37                 comp_ids.add(int(c))
38             except ValueError:
39                 pass
40         context['comp_ids'] = comp_ids
41         sect_ids = set()
42         for c in self.request.GET.getlist('s'):
43             try:
44                 sect_ids.add(int(c))
45             except ValueError:
46                 pass
47         context['sect_ids'] = sect_ids
48
49         if not (comp_ids or sect_ids):
50             if level:
51                 errors["competences"] = _('You must select at least one competency from the list.')
52         elif level is None:
53             errors["level"] = _('You must select at least one education level.')
54         else:
55             chosen_competences = SortedDict()
56             for competence in Competence.objects.filter(
57                     models.Q(pk__in=comp_ids) | models.Q(section__pk__in=sect_ids)):
58                 try:
59                     competence.for_level_ = competence.for_level(level)
60                 except CompetenceLevel.DoesNotExist:
61                     pass
62                 chosen_competences.setdefault(competence.section, []).append(competence)
63             context['chosen_competences'] = chosen_competences
64
65         context["errors"] = errors
66         return context