agdj/blog/feeds.py
author Adam Gomaa <code@adam.gomaa.us>
Sun Feb 01 01:35:02 2009 -0500
changeset 416 5a586a8c2f8f
parent 414 4b15f0f7ba8a
child 422 cfcb86035488
permissions -rw-r--r--
Switch to date-based URLs everywhere except the feed URLs of old posts, so I don't spam everyone's feedreader.
code@67
     1
"""
code@332
     2
Make feeds for my blog
code@332
     3
Use PyRSS2gen
code@332
     4
Not Django's - it's odd.
code@332
     5
code@332
     6
I like the function-
code@332
     7
oriented interface
code@332
     8
not the method one
code@67
     9
code@390
    10
TODO: max() on an empty sequence in the lastBuildDate section causes
code@390
    11
exceptions
code@390
    12
code@67
    13
"""
code@332
    14
code@413
    15
from django.http import HttpResponse, HttpResponseRedirect, Http404
code@314
    16
from django.views.decorators.cache import cache_page
code@100
    17
code@67
    18
import PyRSS2Gen
code@67
    19
code@88
    20
code@67
    21
def _build_entry_item(entry):
code@272
    22
    "Build the RSSItem for a blog Entry"
code@333
    23
code@333
    24
    entry_kwargs = {}
code@333
    25
    entry_kwargs['title'] = entry.title
code@416
    26
    entry_kwargs['guid'] = guid = entry.urls.feed_url
code@333
    27
    entry_kwargs['link'] = "http://adam.gomaa.us%s" % guid
code@333
    28
    entry_kwargs['description'] = entry.rendered
code@333
    29
    entry_kwargs['pubDate'] = entry.pub_date
code@333
    30
code@333
    31
    return PyRSS2Gen.RSSItem(**entry_kwargs)
code@67
    32
code@272
    33
code@198
    34
def _build_comment_item(comment):
code@272
    35
    "Return the RSSItem for the blog Comment"
code@333
    36
code@333
    37
    item_kwargs = {}
code@333
    38
    item_kwargs['title'] = "Comment on %s" % comment.entry.title
code@416
    39
    abs_url = comment.entry.feed_url
code@333
    40
    item_kwargs['link'] = "http://adam.gomaa.us%s" % abs_url
code@333
    41
    item_kwargs['guid'] = comment.entry.slug+str(comment.id)
code@333
    42
    item_kwargs['description'] = comment.rendered
code@333
    43
    item_kwargs['pubDate'] = comment.pub_date
code@333
    44
code@333
    45
    return PyRSS2Gen.RSSItem(**item_kwargs)
code@67
    46
code@314
    47
code@314
    48
@cache_page(60 * 60)
code@100
    49
def latest(request):
code@272
    50
    "Show the latest blog Entries"
code@272
    51
    from agdj.blog.models import Entry
code@198
    52
code@332
    53
    limit = request.GET.get("limit", None)
code@332
    54
    if limit is None:
code@332
    55
        limit = 5
code@332
    56
code@334
    57
    items = Entry.public_objects.order_by("-pub_date")[:limit]
code@333
    58
code@333
    59
    feed_kwargs = {}
code@335
    60
    feed_kwargs['title'] = "204 No Content Blog"
code@333
    61
    feed_kwargs['link'] = "http://adam.gomaa.us/blog/"
code@333
    62
    feed_kwargs['description'] = "Feed for 204 No Content, Adam Gomaa's blog."
code@333
    63
    feed_kwargs['lastBuildDate'] = max([item.pub_date for item in items])
code@333
    64
    feed_kwargs['items'] = [_build_entry_item(item) for item in items]
code@333
    65
code@333
    66
    rss = PyRSS2Gen.RSS2(**feed_kwargs)
code@100
    67
    res = HttpResponse(rss.to_xml())
code@235
    68
    res['content-type'] = "application/rss+xml"
code@100
    69
    return res
code@67
    70
code@67
    71
code@272
    72
def per_tag(request, tag):
code@272
    73
    "Feed for the latest Entries for a tag"
code@88
    74
    from agdj.blog.models import Entry
code@100
    75
    from tagging.models import TaggedItem
code@272
    76
code@100
    77
    items = Entry.objects.filter(public=True).order_by("-pub_date")
code@100
    78
    items = TaggedItem.objects.get_by_model(items, tag)[:5]
code@413
    79
    items = list(items)
code@413
    80
    if not items:
code@414
    81
        raise Http404()
code@333
    82
code@333
    83
    feed_kwargs = {}
code@333
    84
    feed_kwargs['title'] = "204 No Content Blog"
code@333
    85
    feed_kwargs['link'] = "http://adam.gomaa.us/blog/"
code@333
    86
    feed_kwargs['description'] = "Feed for '%s' tag in 204 No Content, Adam Gomaa's blog." % tag
code@333
    87
    feed_kwargs['lastBuildDate'] = max([item.pub_date for item in items])
code@333
    88
    feed_kwargs['items'] = [_build_entry_item(item) for item in items]
code@333
    89
code@333
    90
    rss = PyRSS2Gen.RSS2(**feed_kwargs)
code@100
    91
    res = HttpResponse(rss.to_xml())
code@235
    92
    res['content-type'] = "application/rss+xml"
code@100
    93
    return res
code@67
    94
code@67
    95
code@198
    96
def comments(request):
code@272
    97
    "Feed of the latest comments"
code@198
    98
    from agdj.blog.models import Comment
code@272
    99
code@332
   100
    items = Comment.objects.order_by("-pub_date")[:15]
code@333
   101
code@333
   102
    feed_kwargs = {}
code@333
   103
    feed_kwargs['title'] = "204 Blog Comments"
code@333
   104
    feed_kwargs['link'] = "http://adam.gomaa.us/blog/"
code@333
   105
    feed_kwargs['description'] = "Comment feed for 204 No Content."
code@333
   106
    feed_kwargs['lastBuildDate'] = max([item.pub_date for item in items])
code@333
   107
    feed_kwargs['items'] = [_build_comment_item(item) for item in items]
code@333
   108
code@333
   109
    rss = PyRSS2Gen.RSS2(**feed_kwargs)
code@333
   110
code@198
   111
    res = HttpResponse(rss.to_xml())
code@235
   112
    res['content-type'] = "application/rss+xml"
code@198
   113
    return res
code@198
   114
code@198
   115