(request, post, **kwargs)
return url
Toggles spam status on post based on a status
Toggles spam status on post based on a status
Toggles spam status on post based on a status
def toggle_spam(request, post, **kwargs):
    """
    Toggles spam status on post based on a status
    """

    url = post.get_absolute_url()

    # Moderators may only be suspended by admins (TODO).
    if post.author.profile.is_moderator and post.spam in (Post.DEFAULT, Post.NOT_SPAM):
        messages.warning(request, "cannot toggle spam on a post created by a moderator")
        return url

    # The user performing the action.
    user = request.user

    # Drop the cache for the post.
    delete_post_cache(post)

    # Current state of the toggle.
    if post.is_spam:
        Post.objects.filter(id=post.id).update(spam=Post.NOT_SPAM, status=Post.OPEN)
    else:
        Post.objects.filter(id=post.id).update(spam=Post.SPAM, status=Post.CLOSED)

    # Refetch up to date state of the post.
    post = Post.objects.filter(id=post.id).get()

    # Set the state for the user (only non moderators are affected)
    state = Profile.SUSPENDED if post.is_spam else Profile.NEW

    # Apply the user change.
    change_user_state(mod=user, target=post.author, state=state)

    # Generate logging messages.
    if post.is_spam:
        text = f"marked post as spam"
    else:
        text = f"restored post from spam"

        # Set indexed flag to False, so it's removed from spam index
        Post.objects.filter(id=post.id).update(indexed=False)

    # Set a logging message.
    messages.success(request, text)

    # Submit the log into the database.
    auth.db_logger(user=user, action=Log.MODERATE, target=post.author, text=text, post=post)

    url = post.get_absolute_url()

    return url
def toggle_spam ( request , post , * * kwargs ) : url = post . get_absolute_url ( ) # Moderators may only be suspended by admins (TODO). if post . author . profile . is_moderator and post . spam in ( Post . DEFAULT , Post . NOT_SPAM ) : messages . warning ( request , "cannot toggle spam on a post created by a moderator" ) return url # The user performing the action. user = request . user # Drop the cache for the post. delete_post_cache ( post ) # Current state of the toggle. if post . is_spam : Post . objects . filter ( id = post . id ) . update ( spam = Post . NOT_SPAM , status = Post . OPEN ) else : Post . objects . filter ( id = post . id ) . update ( spam = Post . SPAM , status = Post . CLOSED ) # Refetch up to date state of the post. post = Post . objects . filter ( id = post . id ) . get ( ) # Set the state for the user (only non moderators are affected) state = Profile . SUSPENDED if post . is_spam else Profile . NEW # Apply the user change. change_user_state ( mod = user , target = post . author , state = state ) # Generate logging messages. if post . is_spam : text = f"marked post as spam" else : text = f"restored post from spam" # Set indexed flag to False, so it's removed from spam index Post . objects . filter ( id = post . id ) . update ( indexed = False ) # Set a logging message. messages . success ( request , text ) # Submit the log into the database. auth . db_logger ( user = user , action = Log . MODERATE , target = post . author , text = text , post = post ) url = post . get_absolute_url ( ) return url