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