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.
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@419
    39
def post_list(request, limit=10):
code@271
    40
    "List all entries"
code@271
    41
code@419
    42
    posts = models.Entry.objects.filter(public=True).order_by("-pub_date")[:limit]
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)
code@419
    54
code@419
    55
code@419
    56
@use_template("blog/archive.html")
code@419
    57
def archive_index(request):
code@419
    58
    "Show the multi-year archive"
code@419
    59
    from agdj.blog import archive
code@419
    60
code@419
    61
    archive = archive.Archive(models.Entry.objects.all())
code@419
    62
code@419
    63
    return {"archive": archive}
code@419
    64
code@419
    65
code@419
    66
def archive_month(request, year, month):
code@419
    67
    "Show the multi-year archive"
code@419
    68
    from django.views.generic.date_based import archive_month
code@419
    69
    from agdj.blog import archive
code@419
    70
    return archive_month(
code@419
    71
        # request, year, month, qs, date_field
code@419
    72
        request, year, month, models.Entry.objects.all(), "pub_date",
code@419
    73
        template_name="blog/archive_month.html",)
code@419
    74