From e835ea0c205be86316214560a1b3788fc24f558a Mon Sep 17 00:00:00 2001 From: Sam Collins Date: Mon, 10 Apr 2017 00:05:43 +0100 Subject: [PATCH] Added Image to RSS feed using Yahoo thumbnail standard --- mhackspace/base/feeds.py | 32 ++++++++++++++++++++++++++++-- mhackspace/blog/feeds.py | 39 +++++++++++++++++++++++-------------- mhackspace/contact/views.py | 6 ++---- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/mhackspace/base/feeds.py b/mhackspace/base/feeds.py index 148e47f..401100a 100644 --- a/mhackspace/base/feeds.py +++ b/mhackspace/base/feeds.py @@ -1,7 +1,35 @@ from django.contrib.syndication.views import Feed -from django.urls import reverse +from django.utils.feedgenerator import Rss201rev2Feed + 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): title = "Maidstone hackspace site news" link = "/latest/" @@ -17,4 +45,4 @@ class LatestEntriesFeed(Feed): return item.description def item_link(self, item): - return item.url + return item.url diff --git a/mhackspace/blog/feeds.py b/mhackspace/blog/feeds.py index 25e49fd..102a24e 100644 --- a/mhackspace/blog/feeds.py +++ b/mhackspace/blog/feeds.py @@ -1,38 +1,47 @@ from django.contrib.syndication.views import Feed from django.utils import timezone +from mhackspace.base.feeds import MediaRssFeed from mhackspace.blog.models import Category, Post class BlogFeed(Feed): title = "Maidstone Hackspace Blog" link = "/blog/" + feed_type = MediaRssFeed description = "The latest blog posts and news from the Maidstone Hackspace site" def items(self, category): return Post.objects.select_related('author').filter(active=True, members_only=False, published_date__lte=timezone.now())[:20] - def item_title(self, item): - return item.title + def item_title(self, post): + return post.title - def item_description(self, item): - return item.description + def item_description(self, post): + return post.description - def item_author_name(self, item): - return item.author.name + def item_author_name(self, post): + return post.author.name - def item_author_email(self, item): - if item.author.public: - return item.author.email + def item_author_email(self, post): + if post.author.public: + return post.author.email - def item_categories(self, item): - return item.categories.all() + def item_categories(self, post): + return post.categories.all() - def item_pubdate(self, item): - return item.published_date + def item_pubdate(self, post): + return post.published_date - def item_updateddate(self, item): - return item.updated_date + def item_updateddate(self, post): + 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): diff --git a/mhackspace/contact/views.py b/mhackspace/contact/views.py index d513895..0cb33db 100644 --- a/mhackspace/contact/views.py +++ b/mhackspace/contact/views.py @@ -3,7 +3,7 @@ from django.core.mail import EmailMessage from django.contrib import messages from mhackspace.contact.forms import ContactForm -# add to your views + def contact(request): form_class = ContactForm if request.method == 'POST': @@ -15,14 +15,12 @@ def contact(request): data['message'], data['contact_email'], to=['contact@maidstone-hackspace.org.uk'], - headers = {'Reply-To': data['contact_email']}) + headers={'Reply-To': data['contact_email']}) email.send() messages.add_message(request, messages.INFO, 'E-Mail sent') else: form = form_class() - return render(request, 'pages/contact.html', { 'form': form, }) -