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
8 class CompetenceDetailView(DetailView):
12 class CompetencesView(ListView):
15 def get_context_data(self, **kwargs):
16 context = super(CompetencesView, self).get_context_data(**kwargs)
18 context['levels'] = SortedDict()
19 for level in Level.objects.all():
20 context['levels'].setdefault(level.group, []).append(level)
22 context['sections'] = Section.objects.all()
27 level = Level.objects.get(slug=self.request.GET.get('level'))
28 except Level.DoesNotExist:
30 context['level'] = level
33 for c in self.request.GET.getlist('c'):
38 context['comp_ids'] = comp_ids
40 for c in self.request.GET.getlist('s'):
45 context['sect_ids'] = sect_ids
47 if not (comp_ids or sect_ids):
49 errors["competences"] = u"Proszę wybrać kompetencje z listy."
51 errors["level"] = u"Proszę wybrać poziom edukacyjny."
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)):
57 competence.for_level_ = competence.for_level(level)
58 except CompetenceLevel.DoesNotExist:
60 chosen_competences.setdefault(competence.section, []).append(competence)
61 context['chosen_competences'] = chosen_competences
63 context["errors"] = errors