Added Image to RSS feed using Yahoo thumbnail standard

This commit is contained in:
Sam Collins 2017-04-10 00:05:43 +01:00
parent b9513e59af
commit e835ea0c20
No known key found for this signature in database
GPG Key ID: 233C5943C800FE30
3 changed files with 56 additions and 21 deletions

View File

@ -1,7 +1,35 @@
from django.contrib.syndication.views import Feed from django.contrib.syndication.views import Feed
from django.urls import reverse from django.utils.feedgenerator import Rss201rev2Feed
from mhackspace.feeds.models import Article 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/'
return attrs
def add_item_elements(self, handler, item):
super(MediaRssFeed, self).add_item_elements(handler, item)
thumbnail = {'url': item['thumbnail_url']}
if 'thumbnail_width' in item:
thumbnail['width'] = str(item['thumbnail_width'])
if 'thumbnail_height' in item:
thumbnail['height'] = str(item['thumbnail_height'])
handler.addQuickElement(u"media:thumbnail", '', thumbnail)
class LatestEntriesFeed(Feed): class LatestEntriesFeed(Feed):
title = "Maidstone hackspace site news" title = "Maidstone hackspace site news"
link = "/latest/" link = "/latest/"
@ -17,4 +45,4 @@ class LatestEntriesFeed(Feed):
return item.description return item.description
def item_link(self, item): def item_link(self, item):
return item.url return item.url

View File

@ -1,38 +1,47 @@
from django.contrib.syndication.views import Feed from django.contrib.syndication.views import Feed
from django.utils import timezone from django.utils import timezone
from mhackspace.base.feeds import MediaRssFeed
from mhackspace.blog.models import Category, Post from mhackspace.blog.models import Category, Post
class BlogFeed(Feed): class BlogFeed(Feed):
title = "Maidstone Hackspace Blog" title = "Maidstone Hackspace Blog"
link = "/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 items(self, category): 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, item): def item_title(self, post):
return item.title return post.title
def item_description(self, item): def item_description(self, post):
return item.description return post.description
def item_author_name(self, item): def item_author_name(self, post):
return item.author.name return post.author.name
def item_author_email(self, item): def item_author_email(self, post):
if item.author.public: if post.author.public:
return item.author.email return post.author.email
def item_categories(self, item): def item_categories(self, post):
return item.categories.all() return post.categories.all()
def item_pubdate(self, item): def item_pubdate(self, post):
return item.published_date return post.published_date
def item_updateddate(self, item): def item_updateddate(self, post):
return item.updated_date return post.updated_date
def item_extra_kwargs(self, post):
return {
'thumbnail_url': post.image.full.url,
'thumbnail_width': post.image.full.width,
'thumbnail_height': post.image.full.height,
}
class BlogCategoryFeed(BlogFeed): class BlogCategoryFeed(BlogFeed):

View File

@ -3,7 +3,7 @@ from django.core.mail import EmailMessage
from django.contrib import messages from django.contrib import messages
from mhackspace.contact.forms import ContactForm from mhackspace.contact.forms import ContactForm
# add to your views
def contact(request): def contact(request):
form_class = ContactForm form_class = ContactForm
if request.method == 'POST': if request.method == 'POST':
@ -15,14 +15,12 @@ def contact(request):
data['message'], data['message'],
data['contact_email'], data['contact_email'],
to=['contact@maidstone-hackspace.org.uk'], to=['contact@maidstone-hackspace.org.uk'],
headers = {'Reply-To': data['contact_email']}) headers={'Reply-To': data['contact_email']})
email.send() email.send()
messages.add_message(request, messages.INFO, 'E-Mail sent') messages.add_message(request, messages.INFO, 'E-Mail sent')
else: else:
form = form_class() form = form_class()
return render(request, 'pages/contact.html', { return render(request, 'pages/contact.html', {
'form': form, 'form': form,
}) })