Handle missing image in sites blog.
This commit is contained in:
parent
723bd8a390
commit
370b0d7b2d
|
@ -82,7 +82,7 @@ LOGGING = {
|
|||
'loggers': {
|
||||
'mhackspace': {
|
||||
'level': 'DEBUG',
|
||||
'handlers': ['console']
|
||||
'handlers': ['console', 'logfile']
|
||||
},
|
||||
'django.request': {
|
||||
'handlers': ['mail_admins', 'logfile'],
|
||||
|
|
|
@ -7,27 +7,31 @@ from mhackspace.feeds.models import Article
|
|||
class MediaRssFeed(Rss201rev2Feed):
|
||||
"""
|
||||
Implement thumbnails which adhere to Yahoo Media RSS (mrss) feed.
|
||||
|
||||
|
||||
@see http://djangosnippets.org/snippets/1648/
|
||||
"""
|
||||
|
||||
def rss_attributes(self):
|
||||
attrs = super(MediaRssFeed, self).rss_attributes()
|
||||
attrs['xmlns:dc'] = "http://purl.org/dc/elements/1.1/"
|
||||
attrs['xmlns:media'] = 'http://search.yahoo.com/mrss/'
|
||||
attrs["xmlns:dc"] = "http://purl.org/dc/elements/1.1/"
|
||||
attrs["xmlns:media"] = "http://search.yahoo.com/mrss/"
|
||||
return attrs
|
||||
|
||||
def add_item_elements(self, 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['width'] = str(item['thumbnail_width'])
|
||||
thumbnail = {"url": item["thumbnail_url"]}
|
||||
|
||||
if 'thumbnail_height' in item:
|
||||
thumbnail['height'] = str(item['thumbnail_height'])
|
||||
if "thumbnail_width" in item:
|
||||
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):
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from collections import OrderedDict
|
||||
from django.utils.html import escape
|
||||
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.blog.models import Category, Post
|
||||
from mhackspace.feeds.models import Article # as ArticleFeed
|
||||
from mhackspace.feeds.models import Article # as ArticleFeed
|
||||
|
||||
|
||||
class RssFeed(Feed):
|
||||
title = "Maidstone Hackspace News"
|
||||
link = "/"
|
||||
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):
|
||||
self.current_site = get_current_site(request)
|
||||
|
@ -22,64 +28,71 @@ class RssFeed(Feed):
|
|||
def items(self, category):
|
||||
items = []
|
||||
# for item in Post.objects.all():
|
||||
for item in Post.objects.select_related('author').filter(
|
||||
active=True,
|
||||
members_only=False):
|
||||
img = '/static/images/card.png'
|
||||
for item in Post.objects.select_related("author").filter(
|
||||
active=True, members_only=False
|
||||
):
|
||||
img = "/static/images/card.png"
|
||||
if item.image:
|
||||
img = item.image.home.url
|
||||
items.append({
|
||||
'title': item.title,
|
||||
'description': item.description,
|
||||
'author': item.author,
|
||||
'pubdate': item.published_date,
|
||||
'updated': item.updated_date,
|
||||
'image': img})
|
||||
items.append(
|
||||
{
|
||||
"title": item.title,
|
||||
"description": item.description,
|
||||
"author": item.author,
|
||||
"pubdate": item.published_date,
|
||||
"updated": item.updated_date,
|
||||
"image": img,
|
||||
}
|
||||
)
|
||||
|
||||
for item in Article.objects.select_related('feed').all():
|
||||
img = '/static/images/card.png'
|
||||
for item in Article.objects.select_related("feed").all():
|
||||
img = "/static/images/card.png"
|
||||
if item.image:
|
||||
img = item.image.home.url
|
||||
items.append({
|
||||
'title': item.title,
|
||||
'description': item.description,
|
||||
'author': item.feed.author,
|
||||
'pubdate': item.date,
|
||||
'updated': item.date,
|
||||
'image': img})
|
||||
items.append(
|
||||
{
|
||||
"title": item.title,
|
||||
"description": item.description,
|
||||
"author": item.feed.author,
|
||||
"pubdate": item.date,
|
||||
"updated": item.date,
|
||||
"image": img,
|
||||
}
|
||||
)
|
||||
|
||||
items.sort(key=lambda r: r.get('pubdate'))
|
||||
items.sort(key=lambda r: r.get("pubdate"))
|
||||
return items
|
||||
|
||||
def item_title(self, post):
|
||||
return post.get('title')
|
||||
return post.get("title")
|
||||
|
||||
def item_description(self, post):
|
||||
return escape(post.get('description'))
|
||||
return escape(post.get("description"))
|
||||
|
||||
def item_author_name(self, post):
|
||||
return post.get('author')
|
||||
return post.get("author")
|
||||
|
||||
def item_link(self, post):
|
||||
return 'link'
|
||||
return "link"
|
||||
|
||||
def item_categories(self, post):
|
||||
return ''
|
||||
return ""
|
||||
|
||||
def item_pubdate(self, post):
|
||||
return post.get('pubdate')
|
||||
return post.get("pubdate")
|
||||
|
||||
def item_updateddate(self, post):
|
||||
return post.get('updated')
|
||||
return post.get("updated")
|
||||
|
||||
def item_extra_kwargs(self, post):
|
||||
return {
|
||||
'thumbnail_url': add_domain(
|
||||
"thumbnail_url": add_domain(
|
||||
domain=self.current_site.domain,
|
||||
url=post.get('image'),
|
||||
secure=True),
|
||||
'thumbnail_width': 100,
|
||||
'thumbnail_height': 100,
|
||||
url=post.get("image"),
|
||||
secure=True,
|
||||
),
|
||||
"thumbnail_width": 100,
|
||||
"thumbnail_height": 100,
|
||||
}
|
||||
|
||||
|
||||
|
@ -87,17 +100,18 @@ class BlogFeed(Feed):
|
|||
title = "Maidstone Hackspace Blog"
|
||||
link = "/blog/"
|
||||
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):
|
||||
self.current_site = get_current_site(request)
|
||||
return super(BlogFeed, self).get_feed(obj, request)
|
||||
|
||||
def items(self, category):
|
||||
return Post.objects.select_related('author').filter(
|
||||
active=True,
|
||||
members_only=False,
|
||||
published_date__lte=timezone.now())[:20]
|
||||
return Post.objects.select_related("author").filter(
|
||||
active=True, members_only=False, published_date__lte=timezone.now()
|
||||
)[:20]
|
||||
|
||||
def item_title(self, post):
|
||||
return post.title
|
||||
|
@ -122,14 +136,22 @@ class BlogFeed(Feed):
|
|||
return post.updated_date
|
||||
|
||||
def item_extra_kwargs(self, post):
|
||||
return {
|
||||
'thumbnail_url': add_domain(
|
||||
domain=self.current_site.domain,
|
||||
url=post.image.full.url,
|
||||
secure=True),
|
||||
'thumbnail_width': post.image.full.width,
|
||||
'thumbnail_height': post.image.full.height,
|
||||
}
|
||||
if not post.image.name:
|
||||
return {}
|
||||
|
||||
try:
|
||||
return {
|
||||
"thumbnail_url": add_domain(
|
||||
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):
|
||||
|
@ -137,11 +159,12 @@ class BlogCategoryFeed(BlogFeed):
|
|||
return Category.objects.filter(slug=category).first()
|
||||
|
||||
def items(self, category):
|
||||
return Post.objects.select_related('author').filter(
|
||||
return Post.objects.select_related("author").filter(
|
||||
active=True,
|
||||
members_only=False,
|
||||
categories=category,
|
||||
published_date__lte=timezone.now())[:20]
|
||||
published_date__lte=timezone.now(),
|
||||
)[:20]
|
||||
|
||||
def title(self, category):
|
||||
return "Maidstone Hackspace Blog: %s" % category.name
|
||||
|
|
|
@ -2,14 +2,15 @@
|
|||
-r base.txt
|
||||
|
||||
# linting
|
||||
flake8==3.3.0 # pyup: != 2.6.0
|
||||
flake8==3.7.5 # pyup: != 2.6.0
|
||||
|
||||
coverage==4.4.1
|
||||
factory-boy==2.8.1
|
||||
coverage==4.5.2
|
||||
factory-boy==2.11.1
|
||||
|
||||
django-coverage-plugin==1.5.0
|
||||
django-coverage-plugin==1.6.0
|
||||
django-test-plus==1.1.1
|
||||
|
||||
# pytest
|
||||
pytest-django==3.1.2
|
||||
pytest-sugar==0.8.0
|
||||
pytest-django==3.4.7
|
||||
pytest-sugar==0.9.2
|
||||
model_mommy==1.6.0
|
||||
|
|
Loading…
Reference in New Issue