Apply consistency & PEP8 to atom feeds
authorAndrew Browning <ayleph@thisshitistemp.com>
Fri, 4 Nov 2016 06:46:53 +0000 (02:46 -0400)
committerAndrew Browning <ayleph@thisshitistemp.com>
Fri, 4 Nov 2016 06:53:07 +0000 (02:53 -0400)
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
mediagoblin/user_pages/views.py

index 181486c25778316caabd428566871aaf66b26cf3..fa6b5ba779be9af4c559561ece6432d057becac9 100644 (file)
@@ -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'}])
 
index 28d3ba79bc81438c588c140f9cba54b24ce7869d..eaae1bd74eb1ad560d0f1b581c30676f311311a8 100644 (file)
@@ -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 = '<img src="{thumb}" alt='' /> {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(