Commit b82a8565 authored by Okhin's avatar Okhin
Browse files

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 ...@@ -21,22 +21,17 @@ from rp.forms import TagMultipleChoiceField
from rp.models import Article from rp.models import Article
class ArticleList(ListView): class ArticleFilterMixin:
model = Article """
paginate_by = 10 This mixin allows to filter views using various parameters in the query
template_name = "rp/article_list_public.html" string.
filter_lang = None """
filter_tag = None archive = 'both'
archive = 'false'
quote = 'both' quote = 'both'
speak = 'both' speak = 'both'
def get_queryset(self): def get_queryset(self):
if self.filter_lang in ["EN", "FR"]: qs = super().get_queryset()
qs = Article.objects.filter(lang=self.filter_lang)
else:
qs = Article.objects.filter()
# Tags filtering # Tags filtering
self.quote = self.request.GET.get('quote', self.quote) self.quote = self.request.GET.get('quote', self.quote)
self.speak = self.request.GET.get('speak', self.speak) self.speak = self.request.GET.get('speak', self.speak)
...@@ -65,6 +60,35 @@ class ArticleList(ListView): ...@@ -65,6 +60,35 @@ class ArticleList(ListView):
qs = qs.filter(Q(title__icontains=search_keywords) qs = qs.filter(Q(title__icontains=search_keywords)
| Q(extracts__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) filter_tag = self.kwargs.get("filter_tag", self.filter_tag)
if filter_tag is not None: if filter_tag is not None:
qs = qs.filter(tags__name__in=[filter_tag]) qs = qs.filter(tags__name__in=[filter_tag])
...@@ -78,25 +102,22 @@ class ArticleList(ListView): ...@@ -78,25 +102,22 @@ class ArticleList(ListView):
num_times=Count('taggit_taggeditem_items')).all() num_times=Count('taggit_taggeditem_items')).all()
qs = qs.order_by('-num_times') qs = qs.order_by('-num_times')
context["tags"] = qs 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 return context
class ArticleListFlux(LoginRequiredMixin, ListView): class ArticleListFlux(LoginRequiredMixin, ArticleFilterMixin, ListView):
model = Article model = Article
paginate_by = 10 paginate_by = 10
def get_queryset(self): def get_queryset(self):
filter_view = self.kwargs.get("filter_view", "draft") filter_view = self.kwargs.get("filter_view", "draft")
qs = super().get_queryset()
if filter_view in ["published", "draft", "rejected"]: if filter_view in ["published", "draft", "rejected"]:
qs = Article.objects.filter(status=filter_view.upper()) qs = qs.filter(status=filter_view.upper())
else: else:
qs = Article.objects.filter(status="NEW") qs = qs.filter(status="NEW")
return qs.order_by('-created_at') return qs.order_by('-created_at')
......
Supports Markdown
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