Apply consistency & PEP8 to atom feeds
[mediagoblin.git] / mediagoblin / user_pages / views.py
index 6949aaf617d5680aafd4a739355f5bc467a12c60..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
@@ -205,7 +206,8 @@ def media_post_comment(request, media):
         link.save()
 
         messages.add_message(
-            request, messages.SUCCESS,
+            request,
+            messages.SUCCESS,
             _('Your comment has been posted!'))
         trigger_notification(link, media, request)
 
@@ -240,7 +242,9 @@ def media_collect(request, media):
     if request.method != 'POST' or not form.validate():
         # No POST submission, or invalid form
         if not form.validate():
-            messages.add_message(request, messages.ERROR,
+            messages.add_message(
+                request,
+                messages.ERROR,
                 _('Please check your entries and try again.'))
 
         return render_to_response(
@@ -259,9 +263,11 @@ def media_collect(request, media):
             type=Collection.USER_DEFINED_TYPE
         ).first()
         if existing_collection:
-            messages.add_message(request, messages.ERROR,
-                _('You already have a collection called "%s"!')
-                % existing_collection.title)
+            messages.add_message(
+                request,
+                messages.ERROR,
+                _('You already have a collection called "%s"!') %
+                    existing_collection.title)
             return redirect(request, "mediagoblin.user_pages.media_home",
                             user=media.get_actor.username,
                             media=media.slug_or_id)
@@ -291,7 +297,8 @@ def media_collect(request, media):
 
     if not collection:
         messages.add_message(
-            request, messages.ERROR,
+            request,
+            messages.ERROR,
             _('You have to select or add a collection'))
         return redirect(request, "mediagoblin.user_pages.media_collect",
                     user=media.get_actor.username,
@@ -299,15 +306,19 @@ def media_collect(request, media):
 
     # Check whether media already exists in collection
     elif item is not None:
-        messages.add_message(request, messages.ERROR,
-                             _('"%s" already in collection "%s"')
-                             % (media.title, collection.title))
+        messages.add_message(
+            request,
+            messages.ERROR,
+            _('"%s" already in collection "%s"') %
+                (media.title, collection.title))
     else: # Add item to collection
         add_media_to_collection(collection, media, form.note.data)
         create_activity("add", media, request.user, target=collection)
-        messages.add_message(request, messages.SUCCESS,
-                             _('"%s" added to collection "%s"')
-                             % (media.title, collection.title))
+        messages.add_message(
+            request,
+            messages.SUCCESS,
+            _('"%s" added to collection "%s"') %
+                (media.title, collection.title))
 
     return redirect_obj(request, media)
 
@@ -337,7 +348,9 @@ def media_confirm_delete(request, media):
             # Delete MediaEntry and all related files, comments etc.
             media.delete()
             messages.add_message(
-                request, messages.SUCCESS, _('You deleted the media.'))
+                request,
+                messages.SUCCESS,
+                _('You deleted the media.'))
 
             location = media.url_to_next(request.urlgen)
             if not location:
@@ -348,14 +361,17 @@ def media_confirm_delete(request, media):
             return redirect(request, location=location)
         else:
             messages.add_message(
-                request, messages.ERROR,
-                _("The media was not deleted because you didn't check that you were sure."))
+                request,
+                messages.ERROR,
+                _("The media was not deleted because you didn't check "
+                  "that you were sure."))
             return redirect_obj(request, media)
 
     if ((request.user.has_privilege(u'admin') and
          request.user.id != media.actor)):
         messages.add_message(
-            request, messages.WARNING,
+            request,
+            messages.WARNING,
             _("You are about to delete another user's media. "
               "Proceed with caution."))
 
@@ -429,18 +445,23 @@ def collection_item_confirm_remove(request, collection_item):
             collection.save()
 
             messages.add_message(
-                request, messages.SUCCESS, _('You deleted the item from the collection.'))
+                request,
+                messages.SUCCESS,
+                _('You deleted the item from the collection.'))
         else:
             messages.add_message(
-                request, messages.ERROR,
-                _("The item was not removed because you didn't check that you were sure."))
+                request,
+                messages.ERROR,
+                _("The item was not removed because you didn't check "
+                  "that you were sure."))
 
         return redirect_obj(request, collection)
 
     if ((request.user.has_privilege(u'admin') and
          request.user.id != collection_item.in_collection.actor)):
         messages.add_message(
-            request, messages.WARNING,
+            request,
+            messages.WARNING,
             _("You are about to delete an item from another user's collection. "
               "Proceed with caution."))
 
@@ -476,15 +497,20 @@ def collection_confirm_delete(request, collection):
                 item.delete()
 
             collection.delete()
-            messages.add_message(request, messages.SUCCESS,
-                _('You deleted the collection "%s"') % collection_title)
+            messages.add_message(
+                request,
+                messages.SUCCESS,
+                _('You deleted the collection "%s"') %
+                    collection_title)
 
             return redirect(request, "mediagoblin.user_pages.user_home",
                 user=username)
         else:
             messages.add_message(
-                request, messages.ERROR,
-                _("The collection was not deleted because you didn't check that you were sure."))
+                request,
+                messages.ERROR,
+                _("The collection was not deleted because you didn't "
+                  "check that you were sure."))
 
             return redirect_obj(request, collection)
 
@@ -513,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"]:
@@ -538,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(