From 2a01c5e793b218add8b1b8d9c9ed84952930b1bc Mon Sep 17 00:00:00 2001 From: Andrew Browning Date: Fri, 4 Nov 2016 02:46:53 -0400 Subject: [PATCH] Apply consistency & PEP8 to atom feeds This patch applies the atom feed thumbnail changes from 65d8ca7 to user atom feeds and rearranges the code in both views for better consistency with PEP8 and with each other. It also fixes a spacing issue with the feed title on the main page. --- mediagoblin/listings/views.py | 42 ++++++++++++++++------------- mediagoblin/user_pages/views.py | 48 +++++++++++++++++++-------------- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/mediagoblin/listings/views.py b/mediagoblin/listings/views.py index 181486c2..fa6b5ba7 100644 --- a/mediagoblin/listings/views.py +++ b/mediagoblin/listings/views.py @@ -73,19 +73,25 @@ def atom_feed(request): tag_slug = request.matchdict.get(u'tag') feed_title = "MediaGoblin Feed" if tag_slug: - cursor = media_entries_for_tag_slug(request.db, tag_slug) + feed_title += " for tag '%s'" % tag_slug link = request.urlgen('mediagoblin.listings.tags_listing', qualified=True, tag=tag_slug ) - feed_title += "for tag '%s'" % tag_slug + cursor = media_entries_for_tag_slug(request.db, tag_slug) else: # all recent item feed - cursor = MediaEntry.query.filter_by(state=u'processed') + feed_title += " for all recent items" link = request.urlgen('index', qualified=True) - feed_title += "for all recent items" + cursor = MediaEntry.query.filter_by(state=u'processed') + cursor = cursor.order_by(MediaEntry.created.desc()) + cursor = cursor.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS) - atomlinks = [ - {'href': link, - 'rel': 'alternate', - 'type': 'text/html'}] + + """ + ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI) + """ + atomlinks = [{ + 'href': link, + 'rel': 'alternate', + 'type': 'text/html'}] if mg_globals.app_config["push_urls"]: for push_url in mg_globals.app_config["push_urls"]: @@ -93,9 +99,6 @@ def atom_feed(request): 'rel': 'hub', 'href': push_url}) - cursor = cursor.order_by(MediaEntry.created.desc()) - cursor = cursor.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS) - feed = AtomFeed( feed_title, feed_url=request.url, @@ -111,19 +114,22 @@ def atom_feed(request): else: content = entry.description_html - feed.add(entry.get('title'), + feed.add( + entry.get('title'), content, - id=entry.url_for_self(request.urlgen,qualified=True), + id=entry.url_for_self(request.urlgen, qualified=True), content_type='html', - author={'name': entry.get_actor.username, + author={ + 'name': entry.get_actor.username, 'uri': request.urlgen( 'mediagoblin.user_pages.user_home', - qualified=True, user=entry.get_actor.username)}, + qualified=True, + user=entry.get_actor.username)}, updated=entry.get('created'), links=[{ - 'href':entry.url_for_self( - request.urlgen, - qualified=True), + 'href': entry.url_for_self( + request.urlgen, + qualified=True), 'rel': 'alternate', 'type': 'text/html'}]) diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index 28d3ba79..eaae1bd7 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -24,6 +24,7 @@ from mediagoblin import messages, mg_globals from mediagoblin.db.models import (MediaEntry, MediaTag, Collection, Comment, CollectionItem, LocalUser, Activity, \ GenericModelReference) +from mediagoblin.plugins.api.tools import get_media_file_paths from mediagoblin.tools.response import render_to_response, render_404, \ redirect, redirect_obj from mediagoblin.tools.text import cleaned_markdown_conversion @@ -538,23 +539,21 @@ def atom_feed(request): username = request.matchdict['user']).first() if not user or not user.has_privilege(u'active'): return render_404(request) + feed_title = "MediaGoblin Feed for user '%s'" % request.matchdict['user'] + link = request.urlgen('mediagoblin.user_pages.user_home', + qualified=True, user=request.matchdict['user']) + cursor = MediaEntry.query.filter_by(actor=user.id, state=u'processed') + cursor = cursor.order_by(MediaEntry.created.desc()) + cursor = cursor.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS) - cursor = MediaEntry.query.filter_by( - actor = user.id, - state = u'processed').\ - order_by(MediaEntry.created.desc()).\ - limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS) """ ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI) """ atomlinks = [{ - 'href': request.urlgen( - 'mediagoblin.user_pages.user_home', - qualified=True, user=request.matchdict['user']), - 'rel': 'alternate', - 'type': 'text/html' - }] + 'href': link, + 'rel': 'alternate', + 'type': 'text/html'}] if mg_globals.app_config["push_urls"]: for push_url in mg_globals.app_config["push_urls"]: @@ -563,25 +562,34 @@ def atom_feed(request): 'href': push_url}) feed = AtomFeed( - "MediaGoblin: Feed for user '%s'" % request.matchdict['user'], - feed_url=request.url, - id='tag:{host},{year}:gallery.user-{user}'.format( - host=request.host, - year=datetime.datetime.today().strftime('%Y'), - user=request.matchdict['user']), - links=atomlinks) + feed_title, + feed_url=request.url, + id='tag:{host},{year}:gallery.user-{user}'.format( + host=request.host, + year=datetime.datetime.today().strftime('%Y'), + user=request.matchdict['user']), + links=atomlinks) for entry in cursor: + # Include a thumbnail image in content. + file_urls = get_media_file_paths(entry.media_files, request.urlgen) + if 'thumb' in file_urls: + content = ' {desc}'.format( + thumb=file_urls['thumb'], desc=entry.description_html) + else: + content = entry.description_html + feed.add( entry.get('title'), - entry.description_html, + content, id=entry.url_for_self(request.urlgen, qualified=True), content_type='html', author={ 'name': entry.get_actor.username, 'uri': request.urlgen( 'mediagoblin.user_pages.user_home', - qualified=True, user=entry.get_actor.username)}, + qualified=True, + user=entry.get_actor.username)}, updated=entry.get('created'), links=[{ 'href': entry.url_for_self( -- 2.25.1