Add search feature in RSS feed

parent 75c766ec
Pipeline #1221 passed with stages
in 1 minute and 57 seconds
from django.contrib.syndication.views import Feed from django.contrib.syndication.views import Feed
from django.urls import reverse from django.db.models import Q
from rp.models import Article from rp.models import Article
class ArticlesFeed(Feed): class ArticlesFeed(Feed):
...@@ -37,7 +38,20 @@ class ArticlesTagsFeed(ArticlesFeed): ...@@ -37,7 +38,20 @@ class ArticlesTagsFeed(ArticlesFeed):
pass pass
def items(self): def items(self):
return Article.objects.filter(tags__name__in=[self.filter_tag])[:25] return Article.objects.filter(tags__name__in=[self.filter_tag]) \
.order_by('-created_at')[:25]
def get_object(self, request, filter_tag): def get_object(self, request, filter_tag):
self.filter_tag = 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 django.conf.urls import url
from .feeds import ArticlesFeed, ArticlesTagsFeed from .feeds import ArticlesFeed, ArticlesTagsFeed, ArticlesSearchFeed
urlpatterns = [ urlpatterns = [
url(r'^$', ArticlesFeed(filter_lang='FR'), name='articles-feed'), url(r'^$', ArticlesFeed(filter_lang='FR'), name='articles-feed'),
url(r'^international$', ArticlesFeed(filter_lang='EN'), name='articles-feed-international'), url(r'^international$', ArticlesFeed(filter_lang='EN'), name='articles-feed-international'),
url(r'^by-tag/(?P<filter_tag>.*)', ArticlesTagsFeed()) url(r'^by-tag/(?P<filter_tag>.*)', ArticlesTagsFeed()),
url(r'^search/(?P<search_keywords>.*)', ArticlesSearchFeed())
] ]
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