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