From 5c367ca404c5e2f6484e472a4d606d1c55aef69e Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Fri, 27 Jan 2017 18:13:35 +0100 Subject: [PATCH] limit items in rss feed (#14798) --- corbo/settings.py | 1 + corbo/views.py | 3 ++- tests/test_announces.py | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/corbo/settings.py b/corbo/settings.py index 16930b5..c21edaa 100644 --- a/corbo/settings.py +++ b/corbo/settings.py @@ -119,6 +119,7 @@ RSS_TITLE = 'Announces' RSS_DESCRIPTION = '' RSS_LINK = '' RSS_LINK_TEMPLATE = '/#announce{0}' +RSS_ITEMS_LIMIT = 15 # Authentication settings try: diff --git a/corbo/views.py b/corbo/views.py index f616942..6acd03d 100644 --- a/corbo/views.py +++ b/corbo/views.py @@ -202,8 +202,9 @@ class AtomView(Feed): self.__dict__.update(kwargs) def items(self): + limit = settings.RSS_ITEMS_LIMIT return models.Announce.objects.filter(publication_time__lte=timezone.now()).exclude( - expiration_time__lt=timezone.now()).order_by('-publication_time') + expiration_time__lt=timezone.now()).order_by('-publication_time')[:limit] def item_title(self, item): return item.title diff --git a/tests/test_announces.py b/tests/test_announces.py index 521b26a..2d82b1d 100644 --- a/tests/test_announces.py +++ b/tests/test_announces.py @@ -6,6 +6,7 @@ import feedparser from django.core.files.storage import DefaultStorage from django.utils import timezone from django.core.urlresolvers import reverse +from django.conf import settings from corbo.models import Category, Announce @@ -85,3 +86,17 @@ def test_announces_publishing(app): a.save() feed_content = feedparser.parse(app.get(reverse('atom')).text) assert len(feed_content['entries']) == 0 + +def test_rss_feed_items(app): + c = Category.objects.create(name='Test announces') + for i in xrange(10): + a = Announce.objects.create(category=c, title='Test %s' % i, + publication_time=timezone.now(), + text='text of announce %s' % i) + feed_content = feedparser.parse(app.get(reverse('atom')).text) + assert len(feed_content['entries']) <= settings.RSS_ITEMS_LIMIT + for i in xrange(i, 10): + a = Announce.objects.create(category=c, title='Test %s' % i, + publication_time=timezone.now(), + text='text of announce %s' % i) + assert len(feed_content['entries']) <= settings.RSS_ITEMS_LIMIT -- 2.11.0