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
7 class CompetenceDetailView(DetailView):
11 class CompetencesView(ListView):
14 def get_context_data(self, **kwargs):
15 context = super(CompetencesView, self).get_context_data(**kwargs)
17 context['levels'] = SortedDict()
18 for level in Level.objects.all():
19 context['levels'].setdefault(level.group, []).append(level)
21 context['sections'] = Section.objects.all()
26 level = Level.objects.get(slug=self.request.GET.get('level'))
27 except Level.DoesNotExist:
29 context['level'] = level
32 for c in self.request.GET.getlist('c'):
37 context['comp_ids'] = comp_ids
39 for c in self.request.GET.getlist('s'):
44 context['sect_ids'] = sect_ids
46 if not (comp_ids or sect_ids):
48 errors["competences"] = "Please!"
50 errors["level"] = "Please!"
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)):
56 competence.for_level_ = competence.for_level(level)
57 except CompetenceLevel.DoesNotExist:
59 chosen_competences.setdefault(competence.section, []).append(competence)
60 context['chosen_competences'] = chosen_competences
62 context["errors"] = errors