示例#1
0
def delete_post(request, post_id, topic_id):
    """
    Deletes a post, if the user has correct permissions.
    Also updates topic.post_count
    """
    try:
        topic = Topic.objects.get(id=topic_id)
        post = Post.objects.get(id=post_id)
    except:
        messages.error(request, 'Sorry, but this post can not be found. It may have been deleted already.')
        raise Http404
    return_url = "/forum/%s/%s/%s/" % (topic.forum.slug, topic.slug, topic_id)
    if request.user.is_authenticated() and (request.user.is_staff or request.user.id == post.author.id):
        post.delete()
        update_post_relations(request.user, topic, deleting=True)
        topic_posts = topic.post_set.count()
        pmax = (topic_posts / PAGINATE_BY) + 1

        # if no posts are left, delete topic.
        if topic_posts == 0:
            topic.delete()
            return HttpResponseRedirect("/forum/%s/" % topic.forum.slug)

        return HttpResponseRedirect("%spage%s/" % (return_url, pmax))
    else:
        raise Http404
示例#2
0
def delete_post(request, post_id, topic_id):
    """
    Deletes a post, if the user has correct permissions.
    Also updates topic.post_count
    """
    try:
        topic = Topic.objects.get(id=topic_id)
        post = Post.objects.get(id=post_id)
    except:
        messages.error(
            request,
            'Sorry, but this post can not be found. It may have been deleted already.'
        )
        raise Http404
    return_url = "/forum/%s/%s/%s/" % (topic.forum.slug, topic.slug, topic_id)
    if request.user.is_authenticated() and (request.user.is_staff or
                                            request.user.id == post.author.id):
        post.delete()
        update_post_relations(request.user, topic, deleting=True)
        topic_posts = topic.post_set.count()
        pmax = (topic_posts / PAGINATE_BY) + 1

        # if no posts are left, delete topic.
        if topic_posts == 0:
            topic.delete()
            return HttpResponseRedirect("/forum/%s/" % topic.forum.slug)

        return HttpResponseRedirect("%spage%s/" % (return_url, pmax))
    else:
        raise Http404
示例#3
0
def add_post(request,
             t_slug,
             t_id,
             p_id=False):  # topic slug, topic id, post id
    """
    Creates a new post and attaches it to a topic
    """
    topic = get_object_or_404(Topic, id=t_id)
    topic_url = '{0}page{1}/'.format(topic.get_short_url(), topic.page_count)
    user = request.user
    form_title = 'Add a post'

    if topic.is_locked:  # If we mistakenly allowed reply on locked topic, bail with error msg.
        messages.error(request, 'Sorry, but this topic is closed')
        return HttpResponseRedirect(topic_url)

    q = None
    if p_id:  # if there's a post id, it's a quote
        q = Post.objects.get(id=p_id)
        form_title = "Respond to post"

    form = PostForm(request.POST or None, request.FILES or None)

    if form.is_valid():
        if topic.post_set.count() % PAGINATE_BY == 0:
            topic_page = topic.page_count + 1
        else:
            topic_page = topic.page_count

        # we're going to save this inital data now,
        # rather than on the model save()
        # because we only want to bother with this stuff one time
        # and it will never update or change.

        instance = form.save(commit=False)
        instance.topic = topic
        instance.author = user
        instance.author_name = user.preferred_name
        instance.avatar = user.avatar
        instance.topic_page = topic_page
        instance.quote = q
        instance.save()

        update_post_relations(user, topic)

        return HttpResponseRedirect('%s?new_post=%s#post-%s' %
                                    (topic_url, t_id, instance.id))
    return render(request, 'fretboard/add_edit.html', {
        'form': form,
        'form_title': form_title,
        'quote': q
    })
示例#4
0
def add_post(request, t_slug, t_id, p_id = False):  # topic slug, topic id, post id
    """
    Creates a new post and attaches it to a topic
    """
    topic     = get_object_or_404(Topic, id=t_id)
    topic_url = '{0}page{1}/'.format(topic.get_short_url(), topic.page_count)
    user      = request.user
    form_title = 'Add a post'

    if topic.is_locked:  # If we mistakenly allowed reply on locked topic, bail with error msg.
        messages.error(request, 'Sorry, but this topic is closed')
        return HttpResponseRedirect(topic_url)

    q = None
    if p_id:  # if there's a post id, it's a quote
        q = Post.objects.get(id=p_id)
        form_title = "Respond to post"

    form = PostForm(request.POST or None, request.FILES or None)

    if form.is_valid():
        if topic.post_set.count() % PAGINATE_BY == 0:
            topic_page = topic.page_count + 1
        else:
            topic_page = topic.page_count

        # we're going to save this inital data now,
        # rather than on the model save()
        # because we only want to bother with this stuff one time
        # and it will never update or change.

        instance = form.save(commit=False)
        instance.topic  = topic
        instance.author = user
        instance.author_name = user.preferred_name
        instance.avatar = user.avatar
        instance.topic_page = topic_page
        instance.quote = q
        instance.save()

        update_post_relations(user, topic)

        return HttpResponseRedirect('%s?new_post=%s#post-%s' % (topic_url, t_id, instance.id))
    return render(request, 'fretboard/add_edit.html', {
        'form': form,
        'form_title': form_title,
        'quote': q
    })