Add caching to RSS/Atom feeds (#6546)
* . * tryceptance Co-authored-by: Kalob Taulien <4743971+KalobTaulien@users.noreply.github.com>
This commit is contained in:
Родитель
89d21088da
Коммит
230da745c7
|
@ -128,3 +128,6 @@ translations_github_commit_*
|
|||
|
||||
# VSCode project config
|
||||
.vscode/settings.json
|
||||
|
||||
# Localization testing SSH keys
|
||||
wagtail-localize-key*
|
||||
|
|
|
@ -44,6 +44,7 @@ env = environ.Env(
|
|||
DATABASE_URL=(str, None),
|
||||
DEBUG=(bool, False),
|
||||
DJANGO_LOG_LEVEL=(str, 'INFO'),
|
||||
FEED_CACHE_TIMEOUT=(int, 60*60*24),
|
||||
DOMAIN_REDIRECT_MIDDLEWARE_ENABLED=(bool, False),
|
||||
FEED_LIMIT=(int, 10),
|
||||
FILEBROWSER_DEBUG=(bool, False),
|
||||
|
@ -629,6 +630,7 @@ PNI_STATS_DB_URL = env('PNI_STATS_DB_URL')
|
|||
NETWORK_SITE_URL = env('NETWORK_SITE_URL')
|
||||
|
||||
# RSS / ATOM settings
|
||||
FEED_CACHE_TIMEOUT = env('FEED_CACHE_TIMEOUT')
|
||||
FEED_LIMIT = env('FEED_LIMIT')
|
||||
|
||||
# Support pages with a large number of fields
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.contrib.syndication.views import Feed
|
||||
from django.utils.feedgenerator import Atom1Feed
|
||||
|
||||
|
@ -16,18 +17,34 @@ class RSSFeed(Feed):
|
|||
description = 'The Mozilla Foundation Blog'
|
||||
|
||||
def items(self):
|
||||
# Pull this object specifically using the English page title, as an IndexPage
|
||||
# rather than a BlogIndexPage, to make sure we're not filtering out all the
|
||||
# "featured" posts (which we need to do for site content purposes))
|
||||
index = IndexPage.objects.get(title_en__iexact='Blog')
|
||||
# Try to get the RSS items from cache first
|
||||
feed_set = cache.get('rss_feed_set')
|
||||
|
||||
# If that doesn't yield the blog page, pull using the universal title
|
||||
if index is None:
|
||||
index = IndexPage.objects.get(title__iexact='Blog')
|
||||
if feed_set is None:
|
||||
# If we don't have an active cache, we build one: pull the index page
|
||||
# specifically using the English page title, as an IndexPage rather than
|
||||
# as a BlogIndexPage, to make sure we're not filtering out all the
|
||||
# "featured" posts (which we need to do for site content purposes).
|
||||
try:
|
||||
index = IndexPage.objects.get(title_en__iexact='Blog')
|
||||
|
||||
blog_pages = index.get_all_entries().order_by('-first_published_at')
|
||||
except IndexPage.DoesNotExist:
|
||||
# If that doesn't yield the blog page, pull using the universal title
|
||||
try:
|
||||
index = IndexPage.objects.get(title__iexact='Blog')
|
||||
|
||||
return blog_pages[:settings.FEED_LIMIT]
|
||||
except IndexPage.DoesNotExist:
|
||||
# At this point there's not much we can do other than to pretend
|
||||
# there are no posts to serialize to RSS/Atom format.
|
||||
return []
|
||||
|
||||
# Then sort the collection and only yield the top FEED_LIMIT posts
|
||||
blog_pages = index.get_all_entries().order_by('-first_published_at')
|
||||
feed_set = blog_pages[:settings.FEED_LIMIT]
|
||||
|
||||
cache.set('rss_feed_set', feed_set, settings.FEED_CACHE_TIMEOUT)
|
||||
|
||||
return feed_set
|
||||
|
||||
def item_title(self, item):
|
||||
return item.title
|
||||
|
|
Загрузка…
Ссылка в новой задаче