예제 #1
0
파일: views.py 프로젝트: yotamoron/blog
def post(request, post_id, action):

        is_edit = action == 'edit'
        the_post = None
        if post_id:
                the_post = get_post_by_id(post_id)
                if not the_post:
                        return message(request,
                                        "The post you asked for doesn't exist")

        if is_edit and not request.user.has_object_perm(the_post, 'edit'):
                return message(request,
                                "You have no permissions to edit this post")

        if request.method == "POST":
                if not is_edit:
                        the_post = Post()
                        the_post.user_id = request.user.id
                        the_post.posted_at = datetime.datetime.utcnow()
                        the_post.views = 0
                form = PostForm(request.POST, instance=the_post)
                if form.is_valid():
                        form.save()
                        request.user.grant_object_perm(the_post,
                                        ['edit', 'delete'])
                        return HttpResponseRedirect(reverse('main.views.index',
                                args=(request.user.username,)))
                else:
                        return render_to_response(request, 'post.html',
                                        {'form':form},
                                        context_instance=RequestContext(request))
        else:
                instance = None
                if is_edit:
                        instance = the_post
                form = PostForm(instance=instance)
                return render_to_response(request, 'post.html', {'form':form},
                                 context_instance=RequestContext(request))