5b90325aa1768430f8f986b4837e78fefe51e57d
[prawokultury.git] / questions / views.py
1 # -*- coding: utf-8 -*-
2 # This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.core.urlresolvers import reverse_lazy
6 from django.db import models
7 from django.shortcuts import get_object_or_404
8 from django.views.generic import ListView
9 from django.views.generic.edit import FormView
10 from .forms import QuestionForm
11 from .models import Question, Tag, TagCategory
12
13
14 class QuestionFormView(FormView):
15     form_class = QuestionForm
16     template_name = "questions/question_form.html"
17     success_url = reverse_lazy("questions_thanks")
18
19     def form_valid(self, form):
20         form.save()
21         return super(QuestionFormView, self).form_valid(form)
22
23
24 class QuestionListView(ListView):
25     def get(self, request, *args, **kwargs):
26         self.tag = None
27         if 'tag' in request.GET:
28             try:
29                 self.tag = Tag.objects.filter(items__question__published=True, slug=request.GET['tag'])[0]
30             except IndexError:
31                 pass
32         return super(QuestionListView, self).get(request, *args, **kwargs)
33
34     def get_queryset(self):
35         qs = Question.objects.filter(published=True
36                 ).order_by('-published_at')
37         if self.tag:
38             qs = qs.filter(tags=self.tag)
39             self.tag.click_count += 1
40             self.tag.save()
41         return qs
42
43     def get_context_data(self, *args, **kwargs):
44         context = super(QuestionListView, self).get_context_data(*args, **kwargs)
45         context['tag'] = self.tag
46         context['tag_categories'] = TagCategory.objects.all().annotate(click_count = models.Sum('tags__click_count'))
47         context['tag_lists'] = dict()
48         
49         tags = Tag.objects.filter(items__question__published=True
50             ).annotate(c=models.Count('items__tag')).order_by('category__slug', '-c', 'slug')
51         all_tag_clicks_count = Tag.objects.all().aggregate(models.Sum('click_count'))['click_count__sum']
52         uncategorized_tag_clicks_count = Tag.objects.filter(category=None).aggregate(models.Sum('click_count'))['click_count__sum']
53         annotated_categories = dict()
54         minimum_factor = 0.7
55         for category in context['tag_categories']:
56             annotated_categories[category.id] = category
57             category.factor =  '%.2f' % (minimum_factor + ((float(category.click_count) / all_tag_clicks_count) if all_tag_clicks_count else 0))
58         for tag in tags:
59             if tag.category:
60                 click_count = annotated_categories[tag.category.id].click_count
61             else:
62                 click_count = uncategorized_tag_clicks_count
63             tag.factor = '%.2f' % (minimum_factor + ((float(tag.click_count) / click_count) if click_count else 0))
64             context['tag_lists'].setdefault(tag.category.id if tag.category else 0, []).append(tag)
65         return context