repair build_*
[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 .models import Competence, Section, Level, CompetenceLevel
6
7
8 class CompetenceDetailView(DetailView):
9     model = Competence
10
11
12 class CompetencesView(ListView):
13     model = Competence
14
15     def get_context_data(self, **kwargs):
16         context = super(CompetencesView, self).get_context_data(**kwargs)
17         
18         context['levels'] = SortedDict()
19         for level in Level.objects.all():
20             context['levels'].setdefault(level.group, []).append(level)
21
22         context['sections'] = Section.objects.all()
23
24         errors = {}
25
26         try:
27             level = Level.objects.get(slug=self.request.GET.get('level'))
28         except Level.DoesNotExist:
29             level = None
30         context['level'] = level
31
32         comp_ids = set()
33         for c in self.request.GET.getlist('c'):
34             try:
35                 comp_ids.add(int(c))
36             except ValueError:
37                 pass
38         context['comp_ids'] = comp_ids
39         sect_ids = set()
40         for c in self.request.GET.getlist('s'):
41             try:
42                 sect_ids.add(int(c))
43             except ValueError:
44                 pass
45         context['sect_ids'] = sect_ids
46
47         if not (comp_ids or sect_ids):
48             if level:
49                 errors["competences"] = u"Proszę wybrać kompetencje z listy."
50         elif level is None:
51             errors["level"] = u"Proszę wybrać poziom edukacyjny."
52         else:
53             chosen_competences = SortedDict()
54             for competence in Competence.objects.filter(
55                     models.Q(pk__in=comp_ids) | models.Q(section__pk__in=sect_ids)):
56                 try:
57                     competence.for_level_ = competence.for_level(level)
58                 except CompetenceLevel.DoesNotExist:
59                     pass
60                 chosen_competences.setdefault(competence.section, []).append(competence)
61             context['chosen_competences'] = chosen_competences
62
63         context["errors"] = errors
64         return context