Improve instant articles feed

This commit is contained in:
Iacopo Spalletti 2016-04-30 11:04:26 +02:00
parent d58d9ab687
commit 1118a8300b
No known key found for this signature in database
GPG key ID: BDCBC2EB289F60C6
3 changed files with 32 additions and 3 deletions

View file

@ -455,6 +455,10 @@ Global Settings
* BLOG_TAGS_PLUGIN_NAME: Blog tags plugin name (default: ``Tags``) * BLOG_TAGS_PLUGIN_NAME: Blog tags plugin name (default: ``Tags``)
* BLOG_CATEGORY_PLUGIN_NAME: Blog categories plugin name (default: ``Categories``) * BLOG_CATEGORY_PLUGIN_NAME: Blog categories plugin name (default: ``Categories``)
* BLOG_ARCHIVE_PLUGIN_NAME: Blog archive plugin name (default: ``Archive``) * BLOG_ARCHIVE_PLUGIN_NAME: Blog archive plugin name (default: ``Archive``)
* BLOG_FEED_CACHE_TIMEOUT: Cache timeout for RSS feeds
* BLOG_FEED_INSTANT_ITEMS: Number of items in Instant Article feed
* BLOG_FEED_LATEST_ITEMS: Number of items in latest items feed
* BLOG_FEED_TAGS_ITEMS: Number of items in per tags feed
Read-only settings Read-only settings
++++++++++++++++++ ++++++++++++++++++

View file

@ -1,6 +1,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals from __future__ import absolute_import, print_function, unicode_literals
from django.utils.six import StringIO
from lxml import etree
from aldryn_apphooks_config.utils import get_app_instance from aldryn_apphooks_config.utils import get_app_instance
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.contrib.syndication.views import Feed from django.contrib.syndication.views import Feed
@ -8,6 +11,7 @@ from django.core.cache import cache
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils.feedgenerator import Rss201rev2Feed, rfc2822_date from django.utils.feedgenerator import Rss201rev2Feed, rfc2822_date
from django.utils.html import strip_tags
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.translation import get_language_from_request, ugettext as _ from django.utils.translation import get_language_from_request, ugettext as _
@ -19,6 +23,7 @@ from .models import Post
class LatestEntriesFeed(Feed): class LatestEntriesFeed(Feed):
feed_type = Rss201rev2Feed feed_type = Rss201rev2Feed
feed_items_number = get_setting('FEED_LATEST_ITEMS')
def __call__(self, request, *args, **kwargs): def __call__(self, request, *args, **kwargs):
self.request = request self.request = request
@ -32,7 +37,7 @@ class LatestEntriesFeed(Feed):
return _('Blog articles on %(site_name)s') % {'site_name': Site.objects.get_current().name} return _('Blog articles on %(site_name)s') % {'site_name': Site.objects.get_current().name}
def items(self, obj=None): def items(self, obj=None):
return Post.objects.namespace(self.namespace).published().order_by('-date_published')[:10] return Post.objects.namespace(self.namespace).published().order_by('-date_published')[:self.feed_items_number]
def item_title(self, item): def item_title(self, item):
return item.safe_translation_getter('title') return item.safe_translation_getter('title')
@ -44,12 +49,13 @@ class LatestEntriesFeed(Feed):
class TagFeed(LatestEntriesFeed): class TagFeed(LatestEntriesFeed):
feed_items_number = get_setting('FEED_TAGS_ITEMS')
def get_object(self, request, tag): def get_object(self, request, tag):
return tag # pragma: no cover return tag # pragma: no cover
def items(self, obj=None): def items(self, obj=None):
return Post.objects.published().filter(tags__slug=obj)[:10] return Post.objects.published().filter(tags__slug=obj)[:self.feed_items_number]
class FBInstantFeed(Rss201rev2Feed): class FBInstantFeed(Rss201rev2Feed):
@ -86,6 +92,14 @@ class FBInstantFeed(Rss201rev2Feed):
class FBInstantArticles(LatestEntriesFeed): class FBInstantArticles(LatestEntriesFeed):
feed_type = FBInstantFeed feed_type = FBInstantFeed
feed_items_number = get_setting('FEED_INSTANT_ITEMS')
def _clean_html(self, content):
document = etree.iterparse(StringIO(content), html=True)
for a, e in document:
if not (e.text and e.text.strip()) and len(e) == 0:
e.getparent().remove(e)
return etree.tostring(document.root)
def item_extra_kwargs(self, item): def item_extra_kwargs(self, item):
if not item: if not item:
@ -97,10 +111,15 @@ class FBInstantArticles(LatestEntriesFeed):
view = PostDetailView.as_view(instant_article=True) view = PostDetailView.as_view(instant_article=True)
response = view(self.request, slug=item.safe_translation_getter('slug')) response = view(self.request, slug=item.safe_translation_getter('slug'))
response.render() response.render()
content = mark_safe(response.content) content = mark_safe(self._clean_html(response.content))
cache.set(key, content, timeout=get_setting('FEED_CACHE_TIMEOUT')) cache.set(key, content, timeout=get_setting('FEED_CACHE_TIMEOUT'))
return { return {
'content': content, 'content': content,
'slug': item.safe_translation_getter('slug'), 'slug': item.safe_translation_getter('slug'),
'guid': item.guid, 'guid': item.guid,
} }
def item_description(self, item):
if item.app_config.use_abstract:
return strip_tags(item.safe_translation_getter('abstract'))
return strip_tags(item.safe_translation_getter('post_text'))

View file

@ -123,6 +123,12 @@ def get_setting(name):
settings, 'BLOG_ARCHIVE_PLUGIN_NAME', _('Archive')), settings, 'BLOG_ARCHIVE_PLUGIN_NAME', _('Archive')),
'BLOG_FEED_CACHE_TIMEOUT': getattr( 'BLOG_FEED_CACHE_TIMEOUT': getattr(
settings, 'BLOG_FEED_CACHE_TIMEOUT', 3600), settings, 'BLOG_FEED_CACHE_TIMEOUT', 3600),
'BLOG_FEED_INSTANT_ITEMS': getattr(
settings, 'BLOG_FEED_INSTANT_ITEMS', 50),
'BLOG_FEED_LATEST_ITEMS': getattr(
settings, 'BLOG_FEED_LATEST_ITEMS', 10),
'BLOG_FEED_TAGS_ITEMS': getattr(
settings, 'BLOG_FEED_TAGS_ITEMS', 10),
} }
return default['BLOG_%s' % name] return default['BLOG_%s' % name]