Source

gnocchi-blog / gnocchi / blog / views.py

from django.views.generic.dates import ArchiveIndexView, DateDetailView
from django.http import HttpResponseRedirect
from gnocchi.blog import models, forms

# This will roll for django-1.3
class PostMixin(object):
    date_field = 'post_date'
    slug_field = 'slug'
    model = models.Post
    username = None

    def get_queryset(self):
        qset = models.Post.objects.current()
        if self.username:
            qset = qset.filter(posted_by__username=self.username)
        for tag in self.request.GET.getlist('tags'):
            qset = qset.filter(tags__name=tag)
        return qset

class PostList(PostMixin, ArchiveIndexView):
    template_name = 'blog/post_list.html'
    allow_empty = True

    def get_context_data(self, **kwargs):
        return dict(
            super(PostList, self).get_context_data(**kwargs),
            most_common=self.model.tags.most_common(),
            tags=self.request.GET.getlist('tags'),
        )

class PostDetail(PostMixin, DateDetailView):
    template_name = 'blog/post_detail.html'
    context_object_name = 'post'

    def _get_remote_ip(self, request):
        try:
            return request.META['HTTP_X_FORWARDED_FOR']
        except KeyError:
            return request.META['REMOTE_ADDR']

    def post(self, request, *args, **kwargs):
        '''Lookie, Maw!  We use POST for what it was originally meant!'''
        self.object = post = self.get_object()
        form = forms.CommentForm(self._get_remote_ip(self.request), request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.ip_address = self._get_remote_ip(self.request)
            if request.user.is_authenticated():
                comment.user = request.user
            comment.save()
            return HttpResponseRedirect(post.get_absolute_url())
        context = self.get_context_data(object=post)
        context['comment_form'] = form
        return self.render_to_response(context)

    def get_context_data(self, **kwargs):
        return dict(
            super(PostDetail, self).get_context_data(**kwargs),
            comment_form = forms.CommentForm(self._get_remote_ip(self.request)),
        )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.