agdj/blog/views.py
author Adam Gomaa <code@adam.gomaa.us>
Sun Feb 01 01:35:02 2009 -0500
changeset 416 5a586a8c2f8f
parent 415 5e48e2725b18
child 419 b16f4fab397a
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.
     1 """Views for the blog portion of my site"""
     2 
     3 from django.core.urlresolvers import reverse
     4 from django.http import HttpResponse, HttpResponseRedirect
     5 from django.shortcuts import get_object_or_404
     6 
     7 from agdj.blog import forms, models
     8 from agdj.utils import use_template
     9 
    10 
    11 def redirect_to_date_version(request, slug):
    12     "Redirect requests from the old URL scheme to the new one"
    13     post = get_object_or_404(models.Entry, slug=slug)
    14     return HttpResponseRedirect(post.urls.view)
    15 
    16 
    17 @use_template("blog/single_post.html")
    18 def single_post(request, year, month, day, slug):
    19     "Show a single post"
    20 
    21     post = get_object_or_404(models.Entry, slug=slug)
    22     if request.method=="POST":
    23         comment_form = forms.PublicCommentForm(request.POST)
    24         spam = not ("not_spam" in request.POST)
    25         if comment_form.is_valid():
    26             comment = comment_form.save(commit=False)
    27             comment.entry = post
    28             comment.ip = request.META['REMOTE_ADDR']
    29             comment.public = not spam
    30             comment.save()
    31             return HttpResponseRedirect(reverse(single_post, args=[slug]))
    32     else:
    33         comment_form = forms.PublicCommentForm()
    34     comments = post.comments.filter(public=True).order_by("pub_date")
    35     return dict(post=post, comments=comments, comment_form=comment_form)
    36 
    37 
    38 @use_template("blog/post_list.html")
    39 def post_list(request):
    40     "List all entries"
    41 
    42     posts = models.Entry.objects.filter(public=True).order_by("-pub_date")
    43     return dict(posts=posts)
    44 
    45 
    46 @use_template("blog/post_list_tag.html")
    47 def view_tag(request, tag):
    48     "Show the entries with a tag"
    49     from tagging.models import TaggedItem
    50 
    51     posts = models.Entry.objects.filter(public=True).order_by("-pub_date")
    52     posts = TaggedItem.objects.get_by_model(posts, tag)
    53     return dict(posts=posts, tag=tag)