Source

gnocchi-blog / gnocchi / blog / views.py

Full commit
funkybob e89b710 














funkybob 925e9d3 

funkybob e89b710 





funkybob 2c07731 






funkybob e89b710 










funkybob 00f81ed 
funkybob e89b710 














funkybob 2c07731 


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)),
        )