X-Git-Url: https://git.mdrn.pl/edumed.git/blobdiff_plain/e76fdcf2a2488b1787c74fe8dccfa38d7ac04c84..638edfcc8bd88a8e3ba8ee28437e0263c998f409:/curriculum/views.py?ds=sidebyside diff --git a/curriculum/views.py b/curriculum/views.py index 4239f11..e5d30c7 100644 --- a/curriculum/views.py +++ b/curriculum/views.py @@ -1,19 +1,63 @@ -from .models import Competence, Section, Level - - +from django.db import models from django.views.generic import DetailView, ListView +from django.utils.datastructures import SortedDict +from .models import Competence, Section, Level, CompetenceLevel + class CompetenceDetailView(DetailView): model = Competence + class CompetencesView(ListView): model = Competence def get_context_data(self, **kwargs): - print 'kwargs', kwargs context = super(CompetencesView, self).get_context_data(**kwargs) + + context['levels'] = SortedDict() + for level in Level.objects.all(): + context['levels'].setdefault(level.group, []).append(level) - queryset = self.get_queryset() + context['sections'] = Section.objects.all() + errors = {} + + try: + level = Level.objects.get(slug=self.request.GET.get('level')) + except Level.DoesNotExist: + level = None + context['level'] = level + + comp_ids = set() + for c in self.request.GET.getlist('c'): + try: + comp_ids.add(int(c)) + except ValueError: + pass + context['comp_ids'] = comp_ids + sect_ids = set() + for c in self.request.GET.getlist('s'): + try: + sect_ids.add(int(c)) + except ValueError: + pass + context['sect_ids'] = sect_ids + + if not (comp_ids or sect_ids): + if level: + errors["competences"] = "Please!" + elif level is None: + errors["level"] = "Please!" + else: + chosen_competences = SortedDict() + for competence in Competence.objects.filter( + models.Q(pk__in=comp_ids) | models.Q(section__pk__in=sect_ids)): + try: + competence.for_level_ = competence.for_level(level) + except CompetenceLevel.DoesNotExist: + pass + chosen_competences.setdefault(competence.section, []).append(competence) + context['chosen_competences'] = chosen_competences + + context["errors"] = errors return context -