Enable questions tagging.
[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.views.generic import ListView
7 from django.views.generic.edit import FormView
8 from .forms import QuestionForm
9 from .models import Question, Tag
10
11
12 class QuestionFormView(FormView):
13     form_class = QuestionForm
14     template_name = "questions/question_form.html"
15     success_url = reverse_lazy("questions_thanks")
16
17     def form_valid(self, form):
18         form.save()
19         return super(QuestionFormView, self).form_valid(form)
20
21
22 class QuestionListView(ListView):
23     def get(self, request, *args, **kwargs):
24         self.tag = None
25         if 'tag' in request.GET:
26             try:
27                 self.tag = Tag.objects.get(slug=request.GET['tag'])
28             except Tag.DoesNotExist:
29                 pass
30         return super(QuestionListView, self).get(request, *args, **kwargs)
31
32     def get_queryset(self):
33         qs = Question.objects.filter(published=True
34                 ).order_by('-published_at')
35         if 'tag' in self.request.GET:
36             qs = qs.filter(tags__slug=self.request.GET['tag'])
37         return qs
38
39     def get_context_data(self, *args, **kwargs):
40         context = super(QuestionListView, self).get_context_data(*args, **kwargs)
41         context['tags'] = Tag.objects.all()
42         context['tag'] = self.tag
43         return context