Handle missing image in sites blog.

This commit is contained in:
Oliver Marks 2019-02-16 14:28:51 +00:00
parent 723bd8a390
commit 370b0d7b2d
4 changed files with 94 additions and 66 deletions

View File

@ -82,7 +82,7 @@ LOGGING = {
'loggers': { 'loggers': {
'mhackspace': { 'mhackspace': {
'level': 'DEBUG', 'level': 'DEBUG',
'handlers': ['console'] 'handlers': ['console', 'logfile']
}, },
'django.request': { 'django.request': {
'handlers': ['mail_admins', 'logfile'], 'handlers': ['mail_admins', 'logfile'],

View File

@ -7,27 +7,31 @@ from mhackspace.feeds.models import Article
class MediaRssFeed(Rss201rev2Feed): class MediaRssFeed(Rss201rev2Feed):
""" """
Implement thumbnails which adhere to Yahoo Media RSS (mrss) feed. Implement thumbnails which adhere to Yahoo Media RSS (mrss) feed.
@see http://djangosnippets.org/snippets/1648/ @see http://djangosnippets.org/snippets/1648/
""" """
def rss_attributes(self): def rss_attributes(self):
attrs = super(MediaRssFeed, self).rss_attributes() attrs = super(MediaRssFeed, self).rss_attributes()
attrs['xmlns:dc'] = "http://purl.org/dc/elements/1.1/" attrs["xmlns:dc"] = "http://purl.org/dc/elements/1.1/"
attrs['xmlns:media'] = 'http://search.yahoo.com/mrss/' attrs["xmlns:media"] = "http://search.yahoo.com/mrss/"
return attrs return attrs
def add_item_elements(self, handler, item): def add_item_elements(self, handler, item):
super(MediaRssFeed, self).add_item_elements(handler, item) super(MediaRssFeed, self).add_item_elements(handler, item)
thumbnail = {'url': item['thumbnail_url']} if item.get("thumbnail_url") is None:
return
if 'thumbnail_width' in item: thumbnail = {"url": item["thumbnail_url"]}
thumbnail['width'] = str(item['thumbnail_width'])
if 'thumbnail_height' in item: if "thumbnail_width" in item:
thumbnail['height'] = str(item['thumbnail_height']) thumbnail["width"] = str(item["thumbnail_width"])
handler.addQuickElement(u"media:thumbnail", '', thumbnail) if "thumbnail_height" in item:
thumbnail["height"] = str(item["thumbnail_height"])
handler.addQuickElement(u"media:thumbnail", "", thumbnail)
class LatestEntriesFeed(Feed): class LatestEntriesFeed(Feed):

View File

@ -1,3 +1,7 @@
import logging
logger = logging.getLogger(__name__)
from collections import OrderedDict from collections import OrderedDict
from django.utils.html import escape from django.utils.html import escape
from django.contrib.syndication.views import Feed, add_domain from django.contrib.syndication.views import Feed, add_domain
@ -6,14 +10,16 @@ from django.utils import timezone
from mhackspace.base.feeds import MediaRssFeed from mhackspace.base.feeds import MediaRssFeed
from mhackspace.blog.models import Category, Post from mhackspace.blog.models import Category, Post
from mhackspace.feeds.models import Article # as ArticleFeed from mhackspace.feeds.models import Article # as ArticleFeed
class RssFeed(Feed): class RssFeed(Feed):
title = "Maidstone Hackspace News" title = "Maidstone Hackspace News"
link = "/" link = "/"
feed_type = MediaRssFeed feed_type = MediaRssFeed
description = "The latest blog posts and news from the Maidstone Hackspace site" description = (
"The latest blog posts and news from the Maidstone Hackspace site"
)
def get_feed(self, obj, request): def get_feed(self, obj, request):
self.current_site = get_current_site(request) self.current_site = get_current_site(request)
@ -22,64 +28,71 @@ class RssFeed(Feed):
def items(self, category): def items(self, category):
items = [] items = []
# for item in Post.objects.all(): # for item in Post.objects.all():
for item in Post.objects.select_related('author').filter( for item in Post.objects.select_related("author").filter(
active=True, active=True, members_only=False
members_only=False): ):
img = '/static/images/card.png' img = "/static/images/card.png"
if item.image: if item.image:
img = item.image.home.url img = item.image.home.url
items.append({ items.append(
'title': item.title, {
'description': item.description, "title": item.title,
'author': item.author, "description": item.description,
'pubdate': item.published_date, "author": item.author,
'updated': item.updated_date, "pubdate": item.published_date,
'image': img}) "updated": item.updated_date,
"image": img,
}
)
for item in Article.objects.select_related('feed').all(): for item in Article.objects.select_related("feed").all():
img = '/static/images/card.png' img = "/static/images/card.png"
if item.image: if item.image:
img = item.image.home.url img = item.image.home.url
items.append({ items.append(
'title': item.title, {
'description': item.description, "title": item.title,
'author': item.feed.author, "description": item.description,
'pubdate': item.date, "author": item.feed.author,
'updated': item.date, "pubdate": item.date,
'image': img}) "updated": item.date,
"image": img,
}
)
items.sort(key=lambda r: r.get('pubdate')) items.sort(key=lambda r: r.get("pubdate"))
return items return items
def item_title(self, post): def item_title(self, post):
return post.get('title') return post.get("title")
def item_description(self, post): def item_description(self, post):
return escape(post.get('description')) return escape(post.get("description"))
def item_author_name(self, post): def item_author_name(self, post):
return post.get('author') return post.get("author")
def item_link(self, post): def item_link(self, post):
return 'link' return "link"
def item_categories(self, post): def item_categories(self, post):
return '' return ""
def item_pubdate(self, post): def item_pubdate(self, post):
return post.get('pubdate') return post.get("pubdate")
def item_updateddate(self, post): def item_updateddate(self, post):
return post.get('updated') return post.get("updated")
def item_extra_kwargs(self, post): def item_extra_kwargs(self, post):
return { return {
'thumbnail_url': add_domain( "thumbnail_url": add_domain(
domain=self.current_site.domain, domain=self.current_site.domain,
url=post.get('image'), url=post.get("image"),
secure=True), secure=True,
'thumbnail_width': 100, ),
'thumbnail_height': 100, "thumbnail_width": 100,
"thumbnail_height": 100,
} }
@ -87,17 +100,18 @@ class BlogFeed(Feed):
title = "Maidstone Hackspace Blog" title = "Maidstone Hackspace Blog"
link = "/blog/" link = "/blog/"
feed_type = MediaRssFeed feed_type = MediaRssFeed
description = "The latest blog posts and news from the Maidstone Hackspace site" description = (
"The latest blog posts and news from the Maidstone Hackspace site"
)
def get_feed(self, obj, request): def get_feed(self, obj, request):
self.current_site = get_current_site(request) self.current_site = get_current_site(request)
return super(BlogFeed, self).get_feed(obj, request) return super(BlogFeed, self).get_feed(obj, request)
def items(self, category): def items(self, category):
return Post.objects.select_related('author').filter( return Post.objects.select_related("author").filter(
active=True, active=True, members_only=False, published_date__lte=timezone.now()
members_only=False, )[:20]
published_date__lte=timezone.now())[:20]
def item_title(self, post): def item_title(self, post):
return post.title return post.title
@ -122,14 +136,22 @@ class BlogFeed(Feed):
return post.updated_date return post.updated_date
def item_extra_kwargs(self, post): def item_extra_kwargs(self, post):
return { if not post.image.name:
'thumbnail_url': add_domain( return {}
domain=self.current_site.domain,
url=post.image.full.url, try:
secure=True), return {
'thumbnail_width': post.image.full.width, "thumbnail_url": add_domain(
'thumbnail_height': post.image.full.height, domain=self.current_site.domain,
} url=post.image.full.url,
secure=True,
),
"thumbnail_width": post.image.full.width,
"thumbnail_height": post.image.full.height,
}
except IOError as e:
logger.exception("File does not exists error")
return {}
class BlogCategoryFeed(BlogFeed): class BlogCategoryFeed(BlogFeed):
@ -137,11 +159,12 @@ class BlogCategoryFeed(BlogFeed):
return Category.objects.filter(slug=category).first() return Category.objects.filter(slug=category).first()
def items(self, category): def items(self, category):
return Post.objects.select_related('author').filter( return Post.objects.select_related("author").filter(
active=True, active=True,
members_only=False, members_only=False,
categories=category, categories=category,
published_date__lte=timezone.now())[:20] published_date__lte=timezone.now(),
)[:20]
def title(self, category): def title(self, category):
return "Maidstone Hackspace Blog: %s" % category.name return "Maidstone Hackspace Blog: %s" % category.name

View File

@ -2,14 +2,15 @@
-r base.txt -r base.txt
# linting # linting
flake8==3.3.0 # pyup: != 2.6.0 flake8==3.7.5 # pyup: != 2.6.0
coverage==4.4.1 coverage==4.5.2
factory-boy==2.8.1 factory-boy==2.11.1
django-coverage-plugin==1.5.0 django-coverage-plugin==1.6.0
django-test-plus==1.1.1 django-test-plus==1.1.1
# pytest # pytest
pytest-django==3.1.2 pytest-django==3.4.7
pytest-sugar==0.8.0 pytest-sugar==0.9.2
model_mommy==1.6.0