-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathviews.py
95 lines (74 loc) · 3.2 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from django.http import HttpResponsePermanentRedirect
from django.views.generic import DetailView, ListView
from fluent_faq import appsettings
from fluent_utils.softdeps.fluent_pages import CurrentPageMixin
from fluent_faq.models import FaqCategory, FaqQuestion
from parler.views import TranslatableSlugMixin
class BaseFaqMixin(CurrentPageMixin):
context_object_name = None
prefetch_translations = False
def get_context_data(self, **kwargs):
context = super(BaseFaqMixin, self).get_context_data(**kwargs)
context['FLUENT_FAQ_BASE_TEMPLATE'] = appsettings.FLUENT_FAQ_BASE_TEMPLATE
return context
class FaqQuestionList(BaseFaqMixin, ListView):
"""
List view for FAQ questions.
"""
model = FaqQuestion
view_url_name = 'faqquestion_index'
prefetch_translations = False
def get_queryset(self):
qs = super(FaqQuestionList, self).get_queryset().select_related('category').active_translations()
if self.prefetch_translations:
qs = qs.prefetch_related('translations')
return qs
def get_context_data(self, **kwargs):
context = super(FaqQuestionList, self).get_context_data(**kwargs)
qs = context['object_list']
groups = {}
for obj in qs:
groups.setdefault(obj.category, []).append(obj)
categories = []
for category in sorted(groups.keys(), key=lambda c: (c.order, c.title)):
categories.append(
(category, groups[category])
)
context['categories'] = categories
return context
def get_template_names(self):
names = super(FaqQuestionList, self).get_template_names() # faqquestion_list.html
names.insert(0, "fluent_faq/index.html")
return names
class BaseFaqDetailView(BaseFaqMixin, TranslatableSlugMixin, DetailView):
# Only relevant at the detail page, e.g. for a language switch menu.
prefetch_translations = appsettings.FLUENT_FAQ_PREFETCH_TRANSLATIONS
def get_queryset(self):
# Not filtering active_languages(), let TranslatableSlugMixin handle everything.
qs = super(BaseFaqDetailView, self).get_queryset()
if self.prefetch_translations:
qs = qs.prefetch_related('translations')
return qs
def get_language_choices(self):
return appsettings.FLUENT_FAQ_LANGUAGES.get_active_choices()
class FaqCategoryDetail(BaseFaqDetailView):
"""
Detail view for FAQ categories.
"""
model = FaqCategory
view_url_name = 'faqcategory_detail'
class FaqQuestionDetail(BaseFaqDetailView):
"""
Detail view for FAQ questions.
"""
model = FaqQuestion
view_url_name = 'faqquestion_detail'
def render_to_response(self, context, **response_kwargs):
"""
Make sure the view opens at the canonical URL, or redirect otherwise.
"""
# Redirect to the canonical URL when the category slug is incorrect.
if self.kwargs['cat_slug'] != self.object.category.slug:
# NOTE: this doesn't deal with multiple root nodes yet.
return HttpResponsePermanentRedirect(self.object.get_absolute_url())
return super(FaqQuestionDetail, self).render_to_response(context, **response_kwargs)