Exemple #1
0
def update_last_post_on_post_delete(sender, instance, **kwargs):
    """Update num_posts counts and last_post pointers when a Post is deleted.

    Reduce num_posts by 1 for the author, forum, and thread
    Set last_post for the forum
    Set last_post for the thread. If this was the only remaining post
    in the thread and it was deleted, also delete the thread.

    If the post was not moderated, don't update the values
    """
    post = instance
    delete_post_from_solr(post)
    if post.moderation_state == "OK":
        try:
            with transaction.atomic():
                post.author.profile.num_posts = F('num_posts') - 1
                post.author.profile.save()
                post.thread.forum.refresh_from_db()
                post.thread.forum.num_posts = F('num_posts') - 1
                post.thread.forum.set_last_post()
                post.thread.forum.save()
                thread_has_posts = post.thread.set_last_post()
                if thread_has_posts:
                    post.thread.num_posts = F('num_posts') - 1
                    post.thread.save()
                else:
                    post.thread.delete()
        except Thread.DoesNotExist:
            # This happens when the thread has already been deleted, for example
            # when a user is deleted through the admin interface. We don't need
            # to update the thread, but it would be nice to get to the forum object
            # somehow and update that one....
            logger.info('Tried setting last posts for thread and forum, but the thread has already been deleted?')
    invalidate_template_cache('latest_posts')
Exemple #2
0
def update_thread_on_post_delete(sender, instance, **kwargs):
    """Update num_posts counts and last_post pointers when a Post is deleted.

    Reduce num_posts by 1 for the author, forum, and thread
    Set last_post for the forum
    Set last_post for the thread. If this was the only remaining post
    in the thread and it was deleted, also delete the thread.

    If the post was not moderated, don't update num_posts counts, but if it's
    the only post in a thread, also delete the thread.
    """
    post = instance
    delete_post_from_solr(post.id)
    if post.moderation_state == "NM":
        # If the first post is NM and there are subsequent posts in this thread then
        # we won't correctly set thread.first_post. This won't happen in regular use,
        # so we ignore this case

        # Even though we call set_last_post, we just use it to see if there exist OK posts
        # on this thread so that we can delete the thread if necessary.
        thread_has_posts = post.thread.set_last_post()
        if not thread_has_posts:
            post.thread.delete()
    elif post.moderation_state == "OK":
        try:
            with transaction.atomic():
                post.author.profile.num_posts = F('num_posts') - 1
                post.author.profile.save()
                post.thread.forum.refresh_from_db()
                post.thread.forum.num_posts = F('num_posts') - 1
                post.thread.forum.set_last_post()
                post.thread.forum.save()
                thread_has_posts = post.thread.set_last_post()
                if thread_has_posts:
                    post.thread.num_posts = F('num_posts') - 1
                    post.thread.save()

                    # If this post was the first post in the thread then we should set it to the next one
                    # We leave the author of the thread as the author of the first post
                    if post.thread.first_post_id == post.id:
                        # This is unconditionally the first post, even if it's not moderated
                        post.thread.first_post = post.thread.post_set.first()
                        post.thread.save(update_fields=['first_post'])
                else:
                    post.thread.delete()
        except Thread.DoesNotExist:
            # This happens when the thread has already been deleted, for example
            # when a user is deleted through the admin interface. We don't need
            # to update the thread, but it would be nice to get to the forum object
            # somehow and update that one....
            logger.info('Tried setting last posts for thread and forum, but the thread has already been deleted?')
        except accounts.models.Profile.DoesNotExist:
            # When a user is deleted using the admin, is possible that his posts are deleted after the profile has been
            # deleted. In that case we shouldn't update the value of num_posts because the profile doesn't exists
            # anymore, here it's safe to ignore the exception since we are deleting all the objects.
            logger.info('Tried setting last posts for thread and forum, but the profile has already been deleted?')
    invalidate_template_cache('latest_posts')
Exemple #3
0
def update_last_post_on_post_delete(**kwargs):
    post = kwargs['instance']
    delete_post_from_solr(post)
    try:
        post.thread.set_last_post()
    except Thread.DoesNotExist:
        # This happens when the thread has already been deleted, for example
        # when a user is deleted through the admin interface. We don't need
        # to update the thread, but it would be nice to get to the forum object
        # somehow and update that one....
        logger.info(
            'Tried setting last posts for thread and forum, but the thread has already been deleted?'
        )
    invalidate_template_cache('latest_posts')
Exemple #4
0
def post_edit(request, post_id):
    post = get_object_or_404(Post, id=post_id)
    if post.author == request.user or request.user.has_perm('forum.change_post'):
        if request.method == 'POST':
            form = PostReplyForm(request, '', request.POST)
            if form.is_valid():
                delete_post_from_solr(post)
                post.body = form.cleaned_data['body']
                post.save()
                add_post_to_solr(post)  # Update post in solr
                return HttpResponseRedirect(
                    reverse('forums-post', args=[post.thread.forum.name_slug, post.thread.id, post.id]))
        else:
            form = PostReplyForm(request, '', {'body': post.body})
        return render(request, 'forum/post_edit.html', locals())
    else:
        raise Http404
Exemple #5
0
def update_last_post_on_post_delete(sender, instance, **kwargs):
    """Update num_posts counts and last_post pointers when a Post is deleted.

    Reduce num_posts by 1 for the author, forum, and thread
    Set last_post for the forum
    Set last_post for the thread. If this was the only remaining post
    in the thread and it was deleted, also delete the thread.

    If the post was not moderated, don't update the values
    """
    post = instance
    delete_post_from_solr(post)
    if post.moderation_state == "OK":
        try:
            with transaction.atomic():
                post.author.profile.num_posts = F('num_posts') - 1
                post.author.profile.save()
                post.thread.forum.refresh_from_db()
                post.thread.forum.num_posts = F('num_posts') - 1
                post.thread.forum.set_last_post()
                post.thread.forum.save()
                thread_has_posts = post.thread.set_last_post()
                if thread_has_posts:
                    post.thread.num_posts = F('num_posts') - 1
                    post.thread.save()

                    # If this post was the first post in the thread then we should set it to the next one
                    # We leave the author of the thread as the author of the first post
                    if post.thread.first_post_id == post.id:
                        # This is unconditionally the first post, even if it's not moderated
                        post.thread.first_post = post.thread.post_set.first()
                        post.thread.save(update_fields=['first_post'])
                else:
                    post.thread.delete()
        except Thread.DoesNotExist:
            # This happens when the thread has already been deleted, for example
            # when a user is deleted through the admin interface. We don't need
            # to update the thread, but it would be nice to get to the forum object
            # somehow and update that one....
            logger.info('Tried setting last posts for thread and forum, but the thread has already been deleted?')
        except accounts.models.Profile.DoesNotExist:
            # When a user is deleted using the admin, is possible that his posts are deleted after the profile has been
            # deleted. In that case we shouldn't update the value of num_posts because the profile doesn't exists
            # anymore, here it's safe to ignore the exception since we are deleting all the objects.
            logger.info('Tried setting last posts for thread and forum, but the profile has already been deleted?')
    invalidate_template_cache('latest_posts')
Exemple #6
0
def post_edit(request, post_id):
    post = get_object_or_404(Post, id=post_id)
    if post.author == request.user or request.user.has_perm('forum.change_post'):
        if request.method == 'POST':
            form = PostReplyForm(request, '', request.POST)
            if form.is_valid():
                delete_post_from_solr(post)
                post.body = form.cleaned_data['body']
                post.save()
                add_post_to_solr(post)  # Update post in solr
                return HttpResponseRedirect(
                    reverse('forums-post', args=[post.thread.forum.name_slug, post.thread.id, post.id]))
        else:
            form = PostReplyForm(request, '', {'body': post.body})
        tvars = {'form': form}
        return render(request, 'forum/post_edit.html', tvars)
    else:
        raise Http404