Commit e28bec24 authored by Thibaut Broggi's avatar Thibaut Broggi

Merge branch '10-filters-and-search' into 'rp2'

Resolve "FIlters and Search"

Closes #10

See merge request !6
parents cd731689 7b9e9cc7
Pipeline #1222 passed with stages
in 2 minutes and 42 seconds
from rest_framework import serializers
from taggit.managers import TaggableManager
from rp.models import Article
class TagListSerializer(serializers.Field):
class Meta:
model = TaggableManager(blank=True)
def to_representation(self, obj):
if type(obj) is not list:
return [tag.name for tag in obj.all()]
return obj
class ArticleSerializer(serializers.ModelSerializer):
tags = TagListSerializer()
class Meta:
model = Article
fields = ('id', 'url', 'title', 'und_score_up', 'und_score_down')
fields = ('id', 'url', 'title', 'tags', 'extracts', 'und_score_up', 'und_score_down')
from rest_framework import routers
from .views import ArticleViewSet
from .views import ArticleViewSet, ArticleSearch, ArticleTag
router = routers.DefaultRouter()
router.register(r"articles", ArticleViewSet)
router.register(r"articles-by-tag/(?P<filter_tag>.+)", ArticleTag)
router.register(r"articles-search/(?P<search_keywords>.+)", ArticleSearch)
urlpatterns = router.urls
from rest_framework import viewsets
from django.db.models import Q
from rest_framework import viewsets, mixins
from rp.models import Article
from .serializers import ArticleSerializer
......@@ -10,3 +12,20 @@ ArticleMixin = get_viewset_transition_actions_mixin(Article)
class ArticleViewSet(ArticleMixin, viewsets.ModelViewSet):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
class ArticleSearch(viewsets.ModelViewSet, mixins.ListModelMixin):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
def list(self, request, *args, **kwargs):
self.queryset = self.queryset.filter(Q(title__icontains=kwargs['search_keywords'])
| Q(extracts__icontains=kwargs['search_keywords']))
return super().list(request, args, kwargs)
class ArticleTag(viewsets.ModelViewSet, mixins.ListModelMixin):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
def list(self, request, *args, **kwargs):
self.queryset = self.queryset.filter(tags__name__in=[kwargs['filter_tag']])
return super().list(request, args, kwargs)
from django.contrib.syndication.views import Feed
from django.db.models import Q
from rp.models import Article
class ArticlesFeed(Feed):
title = "Revue de presse de la Quadrature"
link = "/feed/"
description = "La revue de presse recense les articles de presse relatifs aux sujets de la Quadrature. Elle est compilée chaque jour par ses bénévoles, à partir de la presse francophone et internationale. Bonne lecture !"
def __init__(self, **kwargs):
self.filter_lang = kwargs['filter_lang']
def items(self):
return Article.objects.filter(lang=self.filter_lang) \
.order_by('-created_at')[:25]
def item_title(self, item):
return item.title
def item_description(self, item):
return item.extracts
def item_link(self, item):
return item.url
def item_pubdate(self, item):
return item.published_at
def item_updateddate(self, item):
return item.updated_at
def item_categories(self, item):
return item.tags.all()
class ArticlesTagsFeed(ArticlesFeed):
def __init__(self, **kwargs):
pass
def items(self):
return Article.objects.filter(tags__name__in=[self.filter_tag]) \
.order_by('-created_at')[:25]
def get_object(self, request, filter_tag):
self.filter_tag = filter_tag
class ArticlesSearchFeed(ArticlesFeed):
def __init__(self, **kwargs):
pass
def items(self):
return Article.objects.filter(Q(title__icontains=self.search_keywords)
| Q(extracts__icontains=self.search_keywords)) \
.order_by('-created_at')[:25]
def get_object(self, request, search_keywords):
self.search_keywords = search_keywords
from django.conf.urls import url
from .feeds import ArticlesFeed, ArticlesTagsFeed, ArticlesSearchFeed
urlpatterns = [
url(r'^$', ArticlesFeed(filter_lang='FR'), name='articles-feed'),
url(r'^international$', ArticlesFeed(filter_lang='EN'), name='articles-feed-international'),
url(r'^by-tag/(?P<filter_tag>.*)', ArticlesTagsFeed()),
url(r'^search/(?P<search_keywords>.*)', ArticlesSearchFeed())
]
......@@ -65,10 +65,12 @@
{% endif %}
<li class="nav-item ml-auto">
<a class="nav-link disabled" href="#">
<i class="fa fa-search"></i>
Rechercher
</a>
<form method="get" action="{% url 'rp:public-article-list' %}">
<div class="input-group">
<input type="search" placeholder="Rechercher" name="q" class="fa fa-search" value="{{ search }}" />
<button type="submit" class="btn btn-default input-group-addon"><i class="fa fa-search"></i></button>
</div>
</form>
</li>
<li class="nav-item">
......
......@@ -5,6 +5,7 @@ from django.views.generic.edit import UpdateView
from django.utils.translation import ugettext_lazy as _
from django.urls import reverse, reverse_lazy
from django.db.models import Count
from django.db.models import Q
from django import forms
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
......@@ -34,6 +35,11 @@ class ArticleList(ListView):
else:
qs = Article.objects.filter()
search_keywords = self.request.GET.get('q', '')
if search_keywords != '':
qs = qs.filter(Q(title__icontains=search_keywords)
| Q(extracts__icontains=search_keywords))
filter_tag = self.kwargs.get("filter_tag", self.filter_tag)
if filter_tag is not None:
qs = qs.filter(tags__name__in=[filter_tag])
......@@ -46,6 +52,7 @@ class ArticleList(ListView):
qs = Tag.objects.annotate(num_times=Count('taggit_taggeditem_items')).all()
qs = qs.order_by('-num_times')
context["tags"] = qs
context["search"] = self.request.GET.get('q', '')
return context
......
......@@ -19,6 +19,7 @@ urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r"^api/", include(router.urls, namespace="api")),
url(r"^feeds/", include("rp.feeds.urls", namespace="feeds")),
url(r"^rp/", include("rp.urls", namespace="rp")),
url(r'^accounts/', include('allauth.urls')),
]
......
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