예제 #1
0
def down_vote_question(id):
    user_id = request.json['user_id']

    user = User.query.get(user_id)
    question = Question.query.get(id)
    up_vote_users = question.up_vote_users.all()
    down_vote_users = question.down_vote_users.all()

    if not user:
        return 'This account have been not registed!'

    if not question:
        return 'This question is not existed!'

    if question.is_deleted:
        return 'This question was deleted!'

    try:
        if user in down_vote_users:
            question.dis_vote_down()
            question.down_vote_users.remove(user)
        else:
            question.vote_down()
            question.down_vote_users.append(user)
            if user in up_vote_users:
                question.dis_vote_up()
                question.up_vote_users.remove(user)

        db.session.commit()
    except:
        return 'Error! Please contact the admin!'

    return question_schema.jsonify(question)
예제 #2
0
def edit_question(id):
    content = request.json['content']
    user_id = request.json['user_id']

    question = Question.query.get(id)

    if question.user_id != user_id:
        return 'You are not the owner of this question!'

    if not question:
        return 'This question is not existed!'

    if question.is_deleted:
        return 'This question was deleted!'

    if question.is_closed:
        return 'This question was closed!'

    question.edit(content)
    try:
        db.session.commit()
    except:
        return 'Error! Please contact the admin!'

    return question_schema.jsonify(question)
예제 #3
0
def get_question(id):
    question = Question.query.get(id)

    if not question:
        return 'This question is not existed!'

    if question.is_deleted:
        return 'This question was deleted!'

    return question_schema.jsonify(question)
예제 #4
0
def create_question():
    user_id = request.json['user_id']
    content = request.json['content']

    user = User.query.filter_by(user_id=user_id).first()

    if not user:
        return 'This account have been not registed!'

    question = Question(content, user_id)

    try:
        db.session.add(question)
        db.session.commit()
    except:
        return 'Error! Fail to create question!'

    return question_schema.jsonify(question)
예제 #5
0
def add_tags(id):
    tag_ids = request.json['tag_ids']
    question = Question.query.get(id)

    if question.user_id != user_id:
        return 'You are not the owner of this question!'

    if question.is_deleted:
        return 'This question was deleted!'

    if question.is_closed:
        return 'This question was closed!'

    tag_ids = request.json['tag_ids']
    tags = Tag.query.filter(Tag.tag_id.in_(tag_ids)).all()

    question.add_tags(tags)

    try:
        db.session.commit()
    except:
        return 'Error! Please contact the admin!'

    return question_schema.jsonify(question)