Added tags atom feed and linked it in the appropriate places
authorChristopher Allan Webber <cwebber@dustycloud.org>
Mon, 1 Aug 2011 02:24:33 +0000 (21:24 -0500)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Mon, 1 Aug 2011 02:24:33 +0000 (21:24 -0500)
mediagoblin/listings/routing.py
mediagoblin/listings/views.py
mediagoblin/templates/mediagoblin/listings/tag.html

index 90580601cbb56ccecf85141a3711f9fd2f2c2269..61dd5210a9aee94ddebcb760fbf6dfeb431420a3 100644 (file)
@@ -21,5 +21,8 @@ tag_routes = [
     # Route('mediagoblin.listings.tags_home', "/",
     #    controller="mediagoblin.listings.views:tags_home"),
     Route('mediagoblin.listings.tags_listing', "/{tag}/",
-        controller="mediagoblin.listings.views:tag_listing")]
+        controller="mediagoblin.listings.views:tag_listing"),
+    Route('mediagoblin.listings.tag_atom_feed', "/{tag}/atom/",
+        controller="mediagoblin.listings.views:tag_atom_feed"),
+    ]
     
index cdb3db31312ccb9265f3bf853ad900038b8ddaeb..aade7e64404e6ee3fa05a5ab0315f04834fa8d43 100644 (file)
@@ -19,6 +19,23 @@ from mediagoblin.db.util import DESCENDING
 from mediagoblin.util import Pagination, render_to_response
 from mediagoblin.decorators import uses_pagination
 
+from werkzeug.contrib.atom import AtomFeed
+
+
+def _get_tag_name_from_entries(media_entries, tag_slug):
+    """
+    Get a tag name from the first entry by looking it up via its slug.
+    """
+    # ... this is slightly hacky looking :\
+    tag_name = tag_slug
+    if media_entries.count():
+        for tag in media_entries[0]['tags']:
+            if tag['slug'] == tag_slug:
+                tag_name == tag['name']
+                break
+
+    return tag_name
+
 
 @uses_pagination
 def tag_listing(request, page):
@@ -33,21 +50,42 @@ def tag_listing(request, page):
     pagination = Pagination(page, cursor)
     media_entries = pagination()
 
-    # Take the tag "name" from the first MediaEntry's non-normalized
-    # tag naming.
-    # ... this is slightly hacky looking :\
-    tag_name = tag_slug
-    if media_entries.count():
-        for tag in media_entries[0]['tags']:
-            if tag['slug'] == tag_slug:
-                tag_name == tag['name']
-                break
-    else:
-        tag_name = tag_slug
+    tag_name = _get_tag_name_from_entries(media_entries, tag_slug)
 
     return render_to_response(
         request,
         'mediagoblin/listings/tag.html',
-        {'tag_name': tag_name,
+        {'tag_slug': tag_slug,
+         'tag_name': tag_name,
          'media_entries': media_entries,
          'pagination': pagination})
+
+
+ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
+
+def tag_atom_feed(request):
+    """
+    generates the atom feed with the tag images
+    """
+    tag_slug = request.matchdict[u'tag']
+
+    cursor = request.db.MediaEntry.find(
+        {u'state': u'processed',
+         u'tags.slug': tag_slug})
+    cursor = cursor.sort('created', DESCENDING)
+    cursor = cursor.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
+
+    feed = AtomFeed(
+        "MediaGoblin: Feed for tag '%s'" % tag_slug,
+        feed_url=request.url,
+        url=request.host_url)
+
+    for entry in cursor:
+        feed.add(entry.get('title'),
+            entry.get('description_html'),
+            content_type='html',
+            author=entry.uploader()['username'],
+            updated=entry.get('created'),
+            url=entry.url_for_self(request.urlgen))
+
+    return feed.get_response()
index db3381d204a7c319e3697d98f9a5fd6dac54a991..6f10ec8da205295e03c44c8e068984062e1c3ba4 100644 (file)
 #}
 {% extends "mediagoblin/base.html" %}
 
+{% block mediagoblin_head %}
+    <link rel="alternate" type="application/atom+xml"
+          href="{{ request.urlgen(
+                       'mediagoblin.listings.tag_atom_feed',
+                       tag=tag_slug) }}">
+{% endblock mediagoblin_head %}
+
 {% block mediagoblin_content -%}
   <h1>
     Media tagged with: {{ tag_name }}
   <div class="container_16 media_gallery">
     {% include "mediagoblin/utils/object_gallery.html" %}
   </div>
+
+  <div class="grid_16">
+    <a href="{{ request.urlgen(
+                  'mediagoblin.listings.tag_atom_feed',
+                  tag=tag_slug) }}">atom feed</a>
+  </div>
 {% endblock %}