Beispiel #1
0
def blog_index(request, slug):
    """Содерижмое блога"""
    form = BlogPostForm(request.POST or None)
    blog = get_object_or_404(Blog, slug=slug)

    if request.method == "POST":
        if not blog.permissions.can_post(request.user):
            return HttpResponseForbidden(u"Пользователь {0} не может постить в блог \"{1}\".".format(
                request.user.username, blog.title
            ))
        if form.is_valid():
            post = form.save(commit=False)
            post.blog = blog
            post.author = request.user
            post.save()
            form.save_m2m()
            form = BlogPostForm()
            request.flash['message'] = _(u'Пост добавлен.')

    posts = BlogPost.objects.filter(blog__pk=blog.pk).order_by('-created')[:settings.POSTS_PER_PAGE]
    return direct_to_template(request, 'blogs/blog.html', dict(
        blog=blog,
        posts=posts,
        form=form,
        user_can_post=blog.permissions.can_post(request.user)
    ))
Beispiel #2
0
def post_edit(request, blog_pk, post_pk):
    post = get_object_or_404(BlogPost, blog__pk=blog_pk, pk=post_pk)
    form = BlogPostForm(request.POST or None, instance=post)
    saved = False
    if request.method == 'POST' and form.is_valid():
        if not post.permissions.can_edit(request.user):
            return HttpResponseForbidden(
                u'Пользователь {0} не может редактировать пост \"{1}\" блога \"{2)\".'.format(
                    request.user.username,
                    post.pk,
                    post.blog.slug
                ))
        post = form.save(commit=False)
        post.save()
        form.save_m2m()
        form = BlogPostForm(instance=post)
        request.flash['message'] = _(u'Пост сохранен.')
    return direct_to_template(request, "blogs/post_edit.html", dict(
        form=form,
        post=post,
        saved=saved
    ))