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',
'easy_thumbnails.processors.filters',
),
USE_TZ=True,
TIME_ZONE='UTC',
FILE_UPLOAD_TEMP_DIR=mkdtemp(),
SITE_ID=1,
HAYSTACK_CONNECTIONS={

View file

@ -11,6 +11,7 @@ from django.utils.feedgenerator import Rss201rev2Feed
from django.utils.html import strip_tags
from django.utils.safestring import mark_safe
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 lxml import etree
@ -87,6 +88,7 @@ class TagFeed(LatestEntriesFeed):
class FBInstantFeed(Rss201rev2Feed):
date_format = '%Y-%m-%dT%H:%M:%S%z'
def rss_attributes(self):
return {
@ -104,7 +106,9 @@ class FBInstantFeed(Rss201rev2Feed):
handler.addQuickElement('category', cat)
if self.feed['feed_copyright'] is not None:
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:
handler.addQuickElement('ttl', self.feed['ttl'])
@ -112,12 +116,15 @@ class FBInstantFeed(Rss201rev2Feed):
super(FBInstantFeed, self).add_item_elements(handler, item)
if 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:
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._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.startElement('content:encoded', {})
handler._write('<![CDATA[')
@ -131,6 +138,11 @@ class FBInstantArticles(LatestEntriesFeed):
feed_type = FBInstantFeed
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):
body = BytesIO(content)
document = etree.iterparse(body, html=True)

View file

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

View file

@ -10,33 +10,22 @@
<article>
<header>
<h1>{{ post.title }}</h1>
<time class="op-published" datetime="{{ post.date_published.isoformat }}">{{ post.date_published|date:"DATE_FORMAT" }}</time>
<time class="op-modified" dateTime="{{ post.date_modified.isoformat }}">{{ post.date_modified|date:"DATE_FORMAT" }}</time>
<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-modified" dateTime="{{ post.date_modified|date:"Y-m-d\TH:i:sO" }}">{{ post.date_modified|date:"DATE_FORMAT" }}</time>
<address>
<a {% if og_author_url %}rel="facebook" href="{{ og_author_url }}"{% endif %}>{{ post.get_author_name }}</a>
</address>
<figure>
<img src="{{ meta.image }}" alt="{{ post.main_image.default_alt_text|default:'' }}" />
{% if post.main_image.default_caption %}
<figcaption>{{ post.main_image.default_caption }}</figcaption>{% endif %}
{% if post.main_image.default_caption %}<figcaption>{{ post.main_image.default_caption }}</figcaption>{% endif %}
</figure>
<h3 class="op-kicker">
{{ post.abstract|striptags|safe }}
</h3>
<h3 class="op-kicker">{{ post.abstract|striptags|safe }}</h3>
</header>
{% if post.app_config.use_placeholder %}
<div class="blog-content">{% render_placeholder post.content %}</div>
{% else %}
<div class="blog-content">{% render_model post "post_text" "post_text" %}</div>
{% endif %}
</article>
</body>
</html>

View file

@ -6,7 +6,6 @@ from copy import deepcopy
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.cache import cache
from djangocms_helper.base_test import BaseTestCase
from haystack import connections
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.models import BlogCategory, Post, ThumbnailOption
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.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.settings import get_setting
from djangocms_blog.sitemaps import BlogSitemap
@ -383,8 +383,8 @@ class ViewTest(BaseTest):
xml = feed(request)
self.assertContains(xml, '<guid>{0}</guid>'.format(posts[0].guid))
self.assertContains(xml, 'content:encoded')
self.assertContains(xml, 'class="op-published" datetime="{0}"'.format(
posts[0].date_published.isoformat()
self.assertContains(xml, 'class="op-modified" datetime="{0}"'.format(
posts[0].date_modified.strftime(FBInstantFeed.date_format)
))
self.assertContains(xml, '<link rel="canonical" href="{0}"/>'.format(
posts[0].get_full_url()