agdj/blog/views.py
author Adam Gomaa <code@adam.gomaa.us>
Sun Feb 01 02:55:17 2009 -0500
changeset 419 b16f4fab397a
parent 416 5a586a8c2f8f
child 420 818f476bc716
permissions -rw-r--r--
Add per-month archive, only show 10 on the main page.
     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, limit=10):
    40     "List all entries"
    41 
    42     posts = models.Entry.objects.filter(public=True).order_by("-pub_date")[:limit]
    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)
    54 
    55 
    56 @use_template("blog/archive.html")
    57 def archive_index(request):
    58     "Show the multi-year archive"
    59     from agdj.blog import archive
    60 
    61     archive = archive.Archive(models.Entry.objects.all())
    62 
    63     return {"archive": archive}
    64 
    65 
    66 def archive_month(request, year, month):
    67     "Show the multi-year archive"
    68     from django.views.generic.date_based import archive_month
    69     from agdj.blog import archive
    70     return archive_month(
    71         # request, year, month, qs, date_field
    72         request, year, month, models.Entry.objects.all(), "pub_date",
    73         template_name="blog/archive_month.html",)
    74