Merge pull request #263 from nephila/feature/fbarts

More instant articles fixes
This commit is contained in:
Iacopo Spalletti 2016-05-26 08:29:56 +02:00
commit edcb354840
6 changed files with 35 additions and 34 deletions

View file

@ -88,6 +88,8 @@ HELPER_SETTINGS = dict(
'filer.thumbnail_processors.scale_and_crop_with_subject_location', 'filer.thumbnail_processors.scale_and_crop_with_subject_location',
'easy_thumbnails.processors.filters', 'easy_thumbnails.processors.filters',
), ),
USE_TZ=True,
TIME_ZONE='UTC',
FILE_UPLOAD_TEMP_DIR=mkdtemp(), FILE_UPLOAD_TEMP_DIR=mkdtemp(),
SITE_ID=1, SITE_ID=1,
HAYSTACK_CONNECTIONS={ HAYSTACK_CONNECTIONS={

View file

@ -11,6 +11,7 @@ from django.utils.feedgenerator import Rss201rev2Feed
from django.utils.html import strip_tags from django.utils.html import strip_tags
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.six import BytesIO from django.utils.six import BytesIO
from django.utils.text import normalize_newlines
from django.utils.translation import get_language_from_request, ugettext as _ from django.utils.translation import get_language_from_request, ugettext as _
from lxml import etree from lxml import etree
@ -87,6 +88,7 @@ class TagFeed(LatestEntriesFeed):
class FBInstantFeed(Rss201rev2Feed): class FBInstantFeed(Rss201rev2Feed):
date_format = '%Y-%m-%dT%H:%M:%S%z'
def rss_attributes(self): def rss_attributes(self):
return { return {
@ -104,7 +106,9 @@ class FBInstantFeed(Rss201rev2Feed):
handler.addQuickElement('category', cat) handler.addQuickElement('category', cat)
if self.feed['feed_copyright'] is not None: if self.feed['feed_copyright'] is not None:
handler.addQuickElement('copyright', self.feed['feed_copyright']) handler.addQuickElement('copyright', self.feed['feed_copyright'])
handler.addQuickElement('lastBuildDate', self.latest_post_date().isoformat()) handler.addQuickElement(
'lastBuildDate', self.latest_post_date().strftime(self.date_format)
)
if self.feed['ttl'] is not None: if self.feed['ttl'] is not None:
handler.addQuickElement('ttl', self.feed['ttl']) handler.addQuickElement('ttl', self.feed['ttl'])
@ -112,12 +116,15 @@ class FBInstantFeed(Rss201rev2Feed):
super(FBInstantFeed, self).add_item_elements(handler, item) super(FBInstantFeed, self).add_item_elements(handler, item)
if item['author']: if item['author']:
handler.addQuickElement('author', item['author']) handler.addQuickElement('author', item['author'])
if item['date_mod'] is not None:
handler.addQuickElement('pubDate', item['date'].isoformat())
if item['date_pub'] is not None: if item['date_pub'] is not None:
handler.addQuickElement('modDate', item['date'].isoformat()) handler.addQuickElement('pubDate', item['date_pub'].strftime(self.date_format))
if item['date_mod'] is not None:
handler.addQuickElement('modDate', item['date_mod'].strftime(self.date_format))
handler.startElement('description', {}) handler.startElement('description', {})
handler._write('<![CDATA[{0}]]>'.format(h.unescape(force_text(item['abstract'])))) handler._write('<![CDATA[{0}]]>'.format(
h.unescape(normalize_newlines(force_text(item['abstract'])).replace('\n', ' ')))
)
handler.endElement('description') handler.endElement('description')
handler.startElement('content:encoded', {}) handler.startElement('content:encoded', {})
handler._write('<![CDATA[') handler._write('<![CDATA[')
@ -131,6 +138,11 @@ class FBInstantArticles(LatestEntriesFeed):
feed_type = FBInstantFeed feed_type = FBInstantFeed
feed_items_number = get_setting('FEED_INSTANT_ITEMS') feed_items_number = get_setting('FEED_INSTANT_ITEMS')
def items(self, obj=None):
return Post.objects.namespace(
self.namespace
).published().order_by('-date_modified')[:self.feed_items_number]
def _clean_html(self, content): def _clean_html(self, content):
body = BytesIO(content) body = BytesIO(content)
document = etree.iterparse(body, html=True) document = etree.iterparse(body, html=True)

View file

@ -212,7 +212,7 @@ class Post(KnockerModel, ModelMeta, TranslatableModel):
def guid(self, language=None): def guid(self, language=None):
if not language: if not language:
language = self.get_current_language() language = self.get_current_language()
base_string = '{0}{2}{1}'.format( base_string = '-{0}-{2}-{1}-'.format(
language, self.app_config.namespace, language, self.app_config.namespace,
self.safe_translation_getter('slug', language_code=language, any_language=True) self.safe_translation_getter('slug', language_code=language, any_language=True)
) )

View file

@ -2,41 +2,30 @@
<!doctype html> <!doctype html>
<html lang="{{ post.get_current_language }}" prefix="op: http://media.facebook.com/op#"> <html lang="{{ post.get_current_language }}" prefix="op: http://media.facebook.com/op#">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
{% block canonical_url %}<link rel="canonical" href="{{ meta.url }}"/>{% endblock canonical_url %} {% block canonical_url %}<link rel="canonical" href="{{ meta.url }}"/>{% endblock canonical_url %}
<meta property="op:markup_version" content="v1.0"> <meta property="op:markup_version" content="v1.0">
</head> </head>
<body> <body>
<article> <article>
<header> <header>
<h1>{{ post.title }}</h1> <h1>{{ post.title }}</h1>
<time class="op-published" datetime="{{ post.date_published|date:"Y-m-d\TH:i:sO" }}">{{ post.date_published|date:"DATE_FORMAT" }}</time>
<time class="op-published" datetime="{{ post.date_published.isoformat }}">{{ post.date_published|date:"DATE_FORMAT" }}</time> <time class="op-modified" dateTime="{{ post.date_modified|date:"Y-m-d\TH:i:sO" }}">{{ post.date_modified|date:"DATE_FORMAT" }}</time>
<time class="op-modified" dateTime="{{ post.date_modified.isoformat }}">{{ post.date_modified|date:"DATE_FORMAT" }}</time>
<address> <address>
<a {% if og_author_url %}rel="facebook" href="{{ og_author_url }}"{% endif %}>{{ post.get_author_name }}</a> <a {% if og_author_url %}rel="facebook" href="{{ og_author_url }}"{% endif %}>{{ post.get_author_name }}</a>
</address> </address>
<figure> <figure>
<img src="{{ meta.image }}" alt="{{ post.main_image.default_alt_text|default:'' }}" /> <img src="{{ meta.image }}" alt="{{ post.main_image.default_alt_text|default:'' }}" />
{% if post.main_image.default_caption %} {% if post.main_image.default_caption %}<figcaption>{{ post.main_image.default_caption }}</figcaption>{% endif %}
<figcaption>{{ post.main_image.default_caption }}</figcaption>{% endif %}
</figure> </figure>
<h3 class="op-kicker">{{ post.abstract|striptags|safe }}</h3>
<h3 class="op-kicker">
{{ post.abstract|striptags|safe }}
</h3>
</header> </header>
{% if post.app_config.use_placeholder %}
{% if post.app_config.use_placeholder %} <div class="blog-content">{% render_placeholder post.content %}</div>
<div class="blog-content">{% render_placeholder post.content %}</div> {% else %}
{% else %} <div class="blog-content">{% render_model post "post_text" "post_text" %}</div>
<div class="blog-content">{% render_model post "post_text" "post_text" %}</div> {% endif %}
{% endif %}
</article> </article>
</body> </body>
</html> </html>

View file

@ -6,7 +6,6 @@ from copy import deepcopy
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core.cache import cache from django.core.cache import cache
from djangocms_helper.base_test import BaseTestCase from djangocms_helper.base_test import BaseTestCase
from haystack import connections from haystack import connections
from haystack.constants import DEFAULT_ALIAS from haystack.constants import DEFAULT_ALIAS
@ -15,7 +14,6 @@ from parler.utils.context import smart_override
from djangocms_blog.cms_appconfig import BlogConfig from djangocms_blog.cms_appconfig import BlogConfig
from djangocms_blog.models import BlogCategory, Post, ThumbnailOption from djangocms_blog.models import BlogCategory, Post, ThumbnailOption
User = get_user_model() User = get_user_model()

View file

@ -17,7 +17,7 @@ from parler.tests.utils import override_parler_settings
from parler.utils.conf import add_default_language_settings from parler.utils.conf import add_default_language_settings
from parler.utils.context import smart_override, switch_language from parler.utils.context import smart_override, switch_language
from djangocms_blog.feeds import FBInstantArticles, LatestEntriesFeed, TagFeed from djangocms_blog.feeds import FBInstantArticles, FBInstantFeed, LatestEntriesFeed, TagFeed
from djangocms_blog.models import BLOG_CURRENT_NAMESPACE from djangocms_blog.models import BLOG_CURRENT_NAMESPACE
from djangocms_blog.settings import get_setting from djangocms_blog.settings import get_setting
from djangocms_blog.sitemaps import BlogSitemap from djangocms_blog.sitemaps import BlogSitemap
@ -383,8 +383,8 @@ class ViewTest(BaseTest):
xml = feed(request) xml = feed(request)
self.assertContains(xml, '<guid>{0}</guid>'.format(posts[0].guid)) self.assertContains(xml, '<guid>{0}</guid>'.format(posts[0].guid))
self.assertContains(xml, 'content:encoded') self.assertContains(xml, 'content:encoded')
self.assertContains(xml, 'class="op-published" datetime="{0}"'.format( self.assertContains(xml, 'class="op-modified" datetime="{0}"'.format(
posts[0].date_published.isoformat() posts[0].date_modified.strftime(FBInstantFeed.date_format)
)) ))
self.assertContains(xml, '<link rel="canonical" href="{0}"/>'.format( self.assertContains(xml, '<link rel="canonical" href="{0}"/>'.format(
posts[0].get_full_url() posts[0].get_full_url()