Commits

timtoo  committed c189e18

add rss support to "newest albums" view; add permalink for albums

  • Participants
  • Parent commits 20d8133

Comments (0)

Files changed (4)

         tmp = self.albums_extra_set.all()
         return tmp and tmp[0].role_id or None
 
+
 class Images(models.Model):
     id = models.IntegerField(editable=False, primary_key=True)
     album = models.ForeignKey(Albums, to_field='id', db_column='album')
     def title_or_name(self):
         return self.title or self.name or str(self.id)
 
+    @models.permalink
+    def get_absolute_url(self):
+        return ('digiweb.views.album', (), { 'album_id': str(self.id), 'slug': '/' + self.slug })
+
+
 
 class Album_Thumb(models.Model):
     albumid = models.OneToOneField(Albums, to_field='id', db_column="albumid", primary_key=True)

File syndication.py

+from django.utils import feedgenerator
+
+def albums_feed_factory(urlbase, albums, description=None):
+    feed = feedgenerator.Rss201rev2Feed(
+            title = 'Newest Albums',
+            link = urlbase,
+            description = description or '',
+            feed_url = urlbase + 'album?fmt=rss',
+            )
+    for item in albums:
+        # strictly speaking we should use content:encoded, but it's been pain enough so far...
+        tn = '<img src="%s" align="left" />' % (urlbase + 'album/tn%i' % item.id,)
+        feed.add_item(
+            title = item.name,
+            description = (item.caption or '') + '\n' + tn,
+            pubdate = item.modificationDate,
+            link = urlbase + '..' + item.get_absolute_url(),
+        )
+    return feed
+
+
+
+
+
+
     (r'^/?$', lambda request: HttpResponsePermanentRedirect('/digiweb/album')),
     (r'^album/?$', 'album'),
     (r'^album/(?P<album_id>\d+)/blog/?', 'album_blog'),
-    (r'^album/(?P<album_id>\d+)', 'album'),
+    (r'^album/(?P<album_id>\d+)(?P<slug>.*)', 'album'),
     (r'^album/new\w*/?', 'album_newest'),
     (r'^tag/(?P<tag>[^/]+)/?', 'album_tag'),
     (r'^album/tip/(?P<album_id>\d+)/?', 'album_tip'),
         cloudify, int2rgb, BlogTag
 from digiweb.templatetags.digiweb_tags import RenderMenuBar
 from digiweb import config
+import digiweb.syndication
+
+def get_url_base(request):
+    """Stupid django doesn't make this available"""
+
 
 def index(request):
     # this is getting 'HttpResponsePermanentRedirect' object has no attribute 'rindex'
     """Converts the integer value to an RGB html string for each tag"""
     return sorted([ (x[0], int2rgb(x[1])) for x in cloudify(getTagsCountsForAlbum(album_id)) ], key=lambda x:x[0].upper())
 
-def album(request, album_id=None):
+def album(request, album_id=None, slug=None):
     """Display all image thumbnails for an album, or list of albums"""
     if album_id:
         album, page = _getAlbumAndPage(request, album_id)
         tags = sorted([ (x[0], int2rgb(x[1])) for x in tags ], key=lambda x:x[0].upper())
         menu = [ 'newest', {'name': 'tags', 'title': 'Tags used in these albums'}, 'slideshow' ]
 
-    return render_to_response('album.html', {'page': page, 'subtitle': subtitle,
+    if request.GET.get('fmt') == 'rss':
+        try:
+            response = HttpResponse(mimetype='application/rss+xml')
+            feed = digiweb.syndication.albums_feed_factory(
+                    request.build_absolute_uri('/digiweb/'),
+                    page.object_list,
+                    description = u'Tags: %s.' % (u', '.join([ x[0] for x in tags]))
+                    )
+            feed.write(response, 'utf-8')
+        except:
+            logging.error('rss generation error', exc_info=True)
+            raise
+    else:
+        response = render_to_response('album.html', {
+            'page': page, 'subtitle': subtitle,
             'title': config.SITE_NAME, 'tags': tags, 'thumbtype': thumbtype,
             'menu': menu, 'isalbum': True })
 
+    return response
+
 def _images_newest(limit=1000):
     return Images.objects.extra(
                 {'role_id': 'role_id'},