Improve instant articles feed
This commit is contained in:
parent
d58d9ab687
commit
1118a8300b
3 changed files with 32 additions and 3 deletions
|
@ -455,6 +455,10 @@ Global Settings
|
|||
* BLOG_TAGS_PLUGIN_NAME: Blog tags plugin name (default: ``Tags``)
|
||||
* BLOG_CATEGORY_PLUGIN_NAME: Blog categories plugin name (default: ``Categories``)
|
||||
* 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
|
||||
++++++++++++++++++
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
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 django.contrib.sites.models import Site
|
||||
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.utils.encoding import force_text
|
||||
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.translation import get_language_from_request, ugettext as _
|
||||
|
||||
|
@ -19,6 +23,7 @@ from .models import Post
|
|||
|
||||
class LatestEntriesFeed(Feed):
|
||||
feed_type = Rss201rev2Feed
|
||||
feed_items_number = get_setting('FEED_LATEST_ITEMS')
|
||||
|
||||
def __call__(self, request, *args, **kwargs):
|
||||
self.request = request
|
||||
|
@ -32,7 +37,7 @@ class LatestEntriesFeed(Feed):
|
|||
return _('Blog articles on %(site_name)s') % {'site_name': Site.objects.get_current().name}
|
||||
|
||||
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):
|
||||
return item.safe_translation_getter('title')
|
||||
|
@ -44,12 +49,13 @@ class LatestEntriesFeed(Feed):
|
|||
|
||||
|
||||
class TagFeed(LatestEntriesFeed):
|
||||
feed_items_number = get_setting('FEED_TAGS_ITEMS')
|
||||
|
||||
def get_object(self, request, tag):
|
||||
return tag # pragma: no cover
|
||||
|
||||
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):
|
||||
|
@ -86,6 +92,14 @@ class FBInstantFeed(Rss201rev2Feed):
|
|||
|
||||
class FBInstantArticles(LatestEntriesFeed):
|
||||
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):
|
||||
if not item:
|
||||
|
@ -97,10 +111,15 @@ class FBInstantArticles(LatestEntriesFeed):
|
|||
view = PostDetailView.as_view(instant_article=True)
|
||||
response = view(self.request, slug=item.safe_translation_getter('slug'))
|
||||
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'))
|
||||
return {
|
||||
'content': content,
|
||||
'slug': item.safe_translation_getter('slug'),
|
||||
'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'))
|
||||
|
|
|
@ -123,6 +123,12 @@ def get_setting(name):
|
|||
settings, 'BLOG_ARCHIVE_PLUGIN_NAME', _('Archive')),
|
||||
'BLOG_FEED_CACHE_TIMEOUT': getattr(
|
||||
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]
|
||||
|
|
Loading…
Reference in a new issue