Let the annoying commence.
[edumed.git] / curriculum / views.py
index 4239f11..25606f1 100644 (file)
@@ -1,19 +1,66 @@
-from .models import Competence, Section, Level
+# -*- coding: utf-8
+from django.db import models
+from django.views.generic import DetailView, ListView
+from django.utils.datastructures import SortedDict
+from django.utils.translation import ugettext as _
 
+from .models import Competence, Section, Level, CompetenceLevel
 
-from django.views.generic import DetailView, ListView
 
 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)
+
+        context['sections'] = Section.objects.all()
 
-        queryset = self.get_queryset()
+        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"] = _('You must select at least one competency from the list.')
+        elif level is None:
+            errors["level"] = _('You must select at least one education level.')
+        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
-