Commit b82a8565 authored by okhin's avatar okhin 🚴

Created a mixin to manage filter by query parameters (q=, archive=, etc …)

parent 4fc3fefd
Pipeline #2611 passed with stages
in 3 minutes and 14 seconds
......@@ -21,22 +21,17 @@ from rp.forms import TagMultipleChoiceField
from rp.models import Article
class ArticleList(ListView):
model = Article
paginate_by = 10
template_name = "rp/article_list_public.html"
filter_lang = None
filter_tag = None
archive = 'false'
class ArticleFilterMixin:
"""
This mixin allows to filter views using various parameters in the query
string.
"""
archive = 'both'
quote = 'both'
speak = 'both'
def get_queryset(self):
if self.filter_lang in ["EN", "FR"]:
qs = Article.objects.filter(lang=self.filter_lang)
else:
qs = Article.objects.filter()
qs = super().get_queryset()
# Tags filtering
self.quote = self.request.GET.get('quote', self.quote)
self.speak = self.request.GET.get('speak', self.speak)
......@@ -65,6 +60,35 @@ class ArticleList(ListView):
qs = qs.filter(Q(title__icontains=search_keywords)
| Q(extracts__icontains=search_keywords))
return qs
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["search"] = self.request.GET.get('q', '')
context["archive"] = self.request.GET.get('archive', self.archive)
context["speak"] = self.request.GET.get('speak', self.speak)
context["quote"] = self.request.GET.get('quote', self.quote)
return context
class ArticleList(ArticleFilterMixin, ListView):
model = Article
paginate_by = 10
template_name = "rp/article_list_public.html"
filter_lang = None
filter_tag = None
archive = 'false' # We do not want to display archived items by default
def get_queryset(self):
qs = super().get_queryset()
if self.filter_lang in ["EN", "FR"]:
qs = qs.filter(lang=self.filter_lang)
else:
qs = qs.filter()
filter_tag = self.kwargs.get("filter_tag", self.filter_tag)
if filter_tag is not None:
qs = qs.filter(tags__name__in=[filter_tag])
......@@ -78,25 +102,22 @@ class ArticleList(ListView):
num_times=Count('taggit_taggeditem_items')).all()
qs = qs.order_by('-num_times')
context["tags"] = qs
context["search"] = self.request.GET.get('q', '')
context["archive"] = self.request.GET.get('archive', self.archive)
context["speak"] = self.request.GET.get('speak', self.speak)
context["quote"] = self.request.GET.get('quote', self.quote)
return context
class ArticleListFlux(LoginRequiredMixin, ListView):
class ArticleListFlux(LoginRequiredMixin, ArticleFilterMixin, ListView):
model = Article
paginate_by = 10
def get_queryset(self):
filter_view = self.kwargs.get("filter_view", "draft")
qs = super().get_queryset()
if filter_view in ["published", "draft", "rejected"]:
qs = Article.objects.filter(status=filter_view.upper())
qs = qs.filter(status=filter_view.upper())
else:
qs = Article.objects.filter(status="NEW")
qs = qs.filter(status="NEW")
return qs.order_by('-created_at')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment