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 _
7 from .models import Competence, Section, Level, CompetenceLevel
10 class CompetenceDetailView(DetailView):
14 class CompetencesView(ListView):
17 def get_context_data(self, **kwargs):
18 context = super(CompetencesView, self).get_context_data(**kwargs)
20 context['levels'] = SortedDict()
21 for level in Level.objects.all():
22 context['levels'].setdefault(level.group, []).append(level)
24 context['sections'] = Section.objects.all()
29 level = Level.objects.get(slug=self.request.GET.get('level'))
30 except Level.DoesNotExist:
32 context['level'] = level
35 for c in self.request.GET.getlist('c'):
40 context['comp_ids'] = comp_ids
42 for c in self.request.GET.getlist('s'):
47 context['sect_ids'] = sect_ids
49 if not (comp_ids or sect_ids):
51 errors["competences"] = _('You must select at least one competency from the list.')
53 errors["level"] = _('You must select at least one education level.')
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)):
59 competence.for_level_ = competence.for_level(level)
60 except CompetenceLevel.DoesNotExist:
62 chosen_competences.setdefault(competence.section, []).append(competence)
63 context['chosen_competences'] = chosen_competences
65 context["errors"] = errors