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)
16 context['levels'] = Level.objects.all()
17 context['sections'] = Section.objects.all()
22 level = Level.objects.get(slug=self.request.GET.get('level'))
23 except Level.DoesNotExist:
25 context['level'] = level
28 for c in self.request.GET.getlist('c'):
33 context['comp_ids'] = comp_ids
35 for c in self.request.GET.getlist('s'):
40 context['sect_ids'] = sect_ids
42 if not (comp_ids or sect_ids):
44 errors["competences"] = "Please!"
46 errors["level"] = "Please!"
48 chosen_competences = SortedDict()
49 for competence in Competence.objects.filter(
50 models.Q(pk__in=comp_ids) | models.Q(section__pk__in=sect_ids)):
52 competence.for_level_ = competence.for_level(level)
53 except CompetenceLevel.DoesNotExist:
55 chosen_competences.setdefault(competence.section, []).append(competence)
56 context['chosen_competences'] = chosen_competences
58 context["errors"] = errors