コード例 #1
0
ファイル: topic.py プロジェクト: adid1297/MyForum
def get_topics():
    user_id = get_jwt_identity()
    count = request.args.get('count')
    offset = request.args.get('offset')
    topics = TopicHandler.get_topics_alphabetically(count=count, offset=offset)
    out = TopicSchema().dump(topics, many=True)

    return jsonify({"data": out}), 200
コード例 #2
0
ファイル: topic.py プロジェクト: adid1297/MyForum
def get_topic(topic_id):
    user_id = get_jwt_identity()

    try:
        topic = TopicHandler.get_topic(topic_id)
    except TopicNotFoundException:
        return {"error": f"Topic #{topic_id} not found"}, 404

    out = TopicSchema().dump(topic)
    return jsonify(out), 200
コード例 #3
0
ファイル: topic.py プロジェクト: adid1297/MyForum
def delete_topic(topic_id):
    try:
        user_id = get_jwt_identity()
        topic = TopicHandler.delete_topic(topic_id=topic_id, user_id=user_id)
    except UnauthorizedTopicEditException:
        return jsonify(
            error="Insufficient permissions for current action"), 403
    except TopicNotFoundException:
        return jsonify(error=f"Topic #{topic_id} not found"), 404

    return jsonify(message=f"Deleted topic #{topic.topic_id}"), 201
コード例 #4
0
ファイル: topic.py プロジェクト: adid1297/MyForum
def get_topic_messages(topic_id):
    user_id = get_jwt_identity()
    count = request.args.get('count')
    offset = request.args.get('offset')

    try:
        messages = TopicHandler.get_topic_messages(topic_id, count, offset)
    except TopicNotFoundException:
        return jsonify(error=f"Topic #{topic_id} not found"), 404

    out = TopicMessageSchema().dump(messages, many=True)

    return jsonify({"data": out}), 200
コード例 #5
0
ファイル: topic.py プロジェクト: adid1297/MyForum
def create_topic():
    try:
        payload = TopicInputSchema().load(request.json)
    except ValidationError as error:
        return error.messages, 422

    user_id = get_jwt_identity()
    new_topic = TopicHandler.create_topic(
        user_id=user_id,
        subject=payload.get('subject'),
        description=payload.get('description'))
    out = TopicSchema().dump(new_topic)

    return jsonify(out), 201
コード例 #6
0
ファイル: topic.py プロジェクト: adid1297/MyForum
def create_topic_message(topic_id):
    try:
        payload = TopicMessageInputSchema().load(request.json)
        user_id = get_jwt_identity()
        new_topic_message = TopicHandler.create_topic_message(
            topic_id=topic_id,
            user_id=user_id,
            topic_message=payload.get('message'),
        )
    except ValidationError as error:
        return jsonify(error.messages), 422
    except UnauthorizedTopicEditException:
        return jsonify(
            error="Insufficient permissions for current action"), 403
    except TopicNotFoundException:
        return jsonify(error=f"Topic #{topic_id} not found"), 404

    out = TopicMessageSchema().dump(new_topic_message)

    return jsonify(out), 201
コード例 #7
0
ファイル: topic.py プロジェクト: adid1297/MyForum
def update_topic(topic_id):
    try:
        payload = TopicInputSchema().load(request.json)
        user_id = get_jwt_identity()
        topic = TopicHandler.update_topic(
            topic_id=topic_id,
            user_id=user_id,
            updated_subject=payload.get('subject'),
            updated_desc=payload.get('description'),
        )
    except ValidationError as error:
        return error.messages, 422
    except UnauthorizedTopicEditException:
        return jsonify(
            error="Insufficient permissions for current action"), 403
    except TopicNotFoundException:
        return {"error": f"Topic #{topic_id} not found"}, 404

    out = TopicSchema().dump(topic)

    return jsonify(out), 201