Showing categories with click count
[prawokultury.git] / questions / views.py
index 9c75ac6..6a2dc52 100644 (file)
@@ -8,7 +8,7 @@ from django.shortcuts import get_object_or_404
 from django.views.generic import ListView
 from django.views.generic.edit import FormView
 from .forms import QuestionForm
-from .models import Question, Tag
+from .models import Question, Tag, TagCategory
 
 
 class QuestionFormView(FormView):
@@ -36,6 +36,8 @@ class QuestionListView(ListView):
                 ).order_by('-published_at')
         if self.tag:
             qs = qs.filter(tags=self.tag)
+            self.tag.click_count += 1
+            self.tag.save()
         return qs
 
     def get_context_data(self, *args, **kwargs):
@@ -43,4 +45,5 @@ class QuestionListView(ListView):
         context['tags'] = Tag.objects.filter(items__question__published=True
             ).annotate(c=models.Count('items__tag')).order_by('-c', 'slug')
         context['tag'] = self.tag
+        context['tag_categories'] = TagCategory.objects.all().annotate(click_count = models.Sum('tags__click_count'))
         return context