Example #1
0
def delete_topic(topic_id=None, slug=None):
    topic = Topic.query.filter_by(id=topic_id).first_or_404()

    if not can_delete_topic(user=current_user, topic=topic):
        flash(_("You do not have the permissions to delete this topic."),
              "danger")
        return redirect(topic.forum.url)

    involved_users = User.query.filter(Post.topic_id == topic.id,
                                       User.id == Post.user_id).all()
    topic.delete(users=involved_users)
    return redirect(url_for("forum.view_forum", forum_id=topic.forum_id))
Example #2
0
def do_topic_action(topics, user, action, reverse):
    """Executes a specific action for topics. Returns a list with the modified
    topic objects.

    :param topics: A iterable with ``Topic`` objects.
    :param user: The user object which wants to perform the action.
    :param action: One of the following actions: locked, important and delete.
    :param reverse: If the action should be done in a reversed way.
                    For example, to unlock a topic, ``reverse`` should be
                    set to ``True``.
    """
    from openspending.auth.forum import can_moderate, can_delete_topic
    from openspending.model.account import Account as User
    from openspending.forum.forum.models import Post

    modified_topics = 0
    if action != "delete":
        for topic in topics:
            if not can_moderate(user, topic.forum):
                flash(_("You do not have the permissions to execute this "
                        "action."), "danger")
                return False

            if getattr(topic, action) and not reverse:
                continue

            setattr(topic, action, not reverse)
            modified_topics += 1
            topic.save()
    elif action == "delete":
        for topic in topics:
            if not can_delete_topic(user, topic):
                flash(_("You do not have the permissions to delete this "
                        "topic."), "danger")
                return False

            involved_users = User.query.filter(Post.topic_id == topic.id,
                                               User.id == Post.user_id).all()
            modified_topics += 1
            topic.delete(involved_users)

    return modified_topics