328ad3e7c33dc40a2ab090d1cd23ed35fde9b8c8
[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                 tag = Tag.objects.get(slug=request.GET['tag'])
30                 assert Question.objects.filter(tags=self.tag).exists()
31             except (Tag.DoesNotExist, AssertionError):
32                 pass
33             else:
34                 self.tag = tag
35         return super(QuestionListView, self).get(request, *args, **kwargs)
36
37     def get_queryset(self):
38         qs = Question.objects.filter(published=True
39                 ).order_by('-published_at')
40         if self.tag:
41             qs = qs.filter(tags=self.tag)
42             self.tag.click_count += 1
43             self.tag.save()
44         return qs
45
46     def get_context_data(self, *args, **kwargs):
47         def get_cloud_size(clicks, relate_to):
48             return '%.2f' % (0.7 + (float(clicks) / relate_to if relate_to != 0 else 0))
49         
50         context = super(QuestionListView, self).get_context_data(*args, **kwargs)
51         context['tag'] = self.tag
52         context['tag_categories'] = TagCategory.objects.all().annotate(click_count = models.Sum('tags__click_count'))
53         context['tag_lists'] = dict()
54         
55         annotated_categories = dict()
56         all_tags_click_count = Tag.objects.all().aggregate(models.Sum('click_count'))['click_count__sum']
57         for category in context['tag_categories']:
58             annotated_categories[category.id] = category
59             category.cloud_size =  get_cloud_size(category.click_count, all_tags_click_count)
60
61         # This wouldn't happen if we were using taggit without generic relations.
62         tags = Tag.objects.raw("""
63             SELECT t.category_id, t.click_count, t.id, t.name, t.slug, count(t.id) AS c
64             FROM questions_tag t
65             LEFT JOIN questions_tagcategory c ON c.id=t.category_id
66             LEFT JOIN questions_tagitem i ON i.tag_id=t.id
67             LEFT JOIN questions_question q ON q.id=i.object_id
68             WHERE q.published
69             GROUP BY t.category_id, t.click_count, t.id, t.name, t.slug
70             ORDER BY c.slug, c DESC, t.slug
71             """)
72         uncategorized_tags_click_count = Tag.objects.filter(category=None).aggregate(models.Sum('click_count'))['click_count__sum']
73         for tag in tags:
74             if tag.category:
75                 category_click_count = annotated_categories[tag.category.id].click_count
76             else:
77                 category_click_count = uncategorized_tags_click_count
78             tag.cloud_size = get_cloud_size(tag.click_count, category_click_count)
79             context['tag_lists'].setdefault(tag.category.id if tag.category else 0, []).append(tag)
80         context['has_uncategorized_tags'] = 0 in context['tag_lists']
81
82         return context