def get_question_by_args(): if not request.args: questions = Question.query.all() if not questions: return jsonify({'code': 0, 'msg': 'no data'}) else: return jsonify({ 'code': 1, 'data': [question.to_json() for question in questions] }) user_id = request.args.get('user_id') keyword = request.args.get('keyword') question_draft = request.args.get('draft', 0) conditions = [] if keyword: conditions.append(Question.question_detail.like('%' + keyword + '%')) if user_id: conditions.append(Question.user_id == user_id) conditions.append(Question.question_draft == question_draft) if not conditions: return json_data(0, 'bad request') questions = Question.query.filter(and_(*conditions)).all() if questions: return json_data(1, [question.to_json() for question in questions]) else: return json_data(0, 'no data')
def get_collection_by_args(): if not request.args: collections = Collection.query.all() if not collections: return json_data(0, 'no data') else: return json_data(1, [collection.to_json() for collection in collections]) else: user_id = request.args.get('user_id') question_id = request.args.get('question_id') conditions = [] if user_id: conditions.append(Collection.user_id == user_id) if question_id: conditions.append(Collection.question_id == question_id) if not conditions: return json_data(0, 'bad request') collections = Collection.query.filter(and_(*conditions)).all() if collections: return json_data(1, [collection.to_json() for collection in collections]) else: return json_data(0, 'no data')
def get_answer_by_args(): if not request.args: answers = Answer.query.all() if not answers: return json_data(0, 'no data') else: return json_data(1, [answer.to_json() for answer in answers]) else: question_id = request.args.get('question_id') user_id = request.args.get('user_id') answer_draft = request.args.get('answer_draft', 0) conditions = [] if question_id: conditions.append(Answer.question_id == question_id) if user_id: conditions.append(Answer.user_id == user_id) conditions.append(Answer.answer_draft == answer_draft) if not conditions: return json_data(0, 'bad request') answers = Answer.query.filter(and_(*conditions)).all() if answers: return json_data(1, [answer.to_json() for answer in answers]) else: return json_data(0, 'no data')
def get_type_by_args(): if not request.args: types = Type.query.all() if not types: return json_data(0, 'no data') else: return json_data(1, [t.to_json() for t in types]) else: return json_data(0, 'bad request')
def delete_question(id): question = Question.query.get(id) if question is None: return json_data(0, 'question not exist') db.session.delete(question) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def delete_answer(id): answer = Answer.query.get(id) if answer is None: return json_data(0, 'answer not exist') db.session.delete(answer) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def delete_comment(id): comment = Comment.query.get(id) if not comment: return json_data(0, 'comment not exist') db.session.delete(comment) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def delete_collection(id): collection = Collection.query.get(id) if not collection: return json_data(0, 'collection not exist') db.session.delete(collection) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def update_comment(id): json = request.json comment_detail = json.get('comment_detail') comment = Comment.query.get(id) if not comment: return json_data(0, 'comment not exist') comment.comment_detail = comment_detail try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def get_click(): questions = db.session.query(Question).join(Click, Click.question_id == Question.question_id)\ .filter(Click.click_date.between(datetime.now() - timedelta(days=7), datetime.now()))\ .group_by(Click.question_id)\ .order_by(desc(func.sum(Click.click_count))).limit(20) return json_data(1, [question.to_json() for question in questions])
def update_user(id): json = request.json user_password = json.get('user_password') user_type = json.get('user_type') user_authentication = json.get('user_authentication', None) user_nickname = json.get('user_nickname') if user_password == '' or user_type == '': return jsonify({'code': 0, 'msg': 'bad request'}) user = User.query.get(id) if user is None: return jsonify({'code': 0, 'msg': 'user not exist'}) if user_password: user.user_password = user_password if user_type: user.user_type = user_type if user_authentication: user.user_authentication = user_authentication if user_nickname: user.user_nickname = user_nickname try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def add_answer(): json = request.json question_id = json.get('question_id') user_id = json.get('user_id') answer_detail = json.get('answer_detail') answer_draft = json.get('answer_draft', 0) if not user_id or not type(user_id) == int or not question_id or not type(question_id) == int: return json_data(0, 'bad request') answer = Answer(question_id, user_id, answer_detail, answer_draft) db.session.add(answer) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def update_answer(id): json = request.json answer_detail = json.get('answer_detail') answer_draft = json.get('answer_draft', 0) answer = Answer.query.get(id) if answer is None: return json_data(0, 'answer not exist') answer.answer_draft = answer_draft if answer_detail: answer.answer_detail = answer_detail try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def add_comment(): json = request.json user_id = json.get('user_id') question_id = json.get('question_id') comment_detail = json.get('comment_detail') if not user_id or not type(user_id) == int or not question_id or not type( question_id) == int: return json_data(0, 'bad request') comment = Comment(comment_detail, user_id, question_id) db.session.add(comment) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def add_collection(): json = request.json user_id = json.get('user_id') question_id = json.get('question_id') if not user_id or not type(user_id) == int or not question_id or not type(question_id) == int: return json_data(0, 'bad request') conditions = [(Collection.user_id == user_id), (Collection.question_id == question_id)] if Collection.query.filter(and_(*conditions)).all(): return json_data(0, 'collection existed') collection = Collection(user_id, question_id) db.session.add(collection) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def delete_user(id): user = User.query.get(id) if user is None: return jsonify({'code': 0, 'msg': 'user not exist'}) db.session.delete(user) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def get_user_by_args(): if not request.args: users = User.query.all() if not users: return jsonify({'code': 0, 'msg': 'no data'}) else: return jsonify({'code': 1, 'data': [user.to_json() for user in users]}) username = request.args.get('username') nickname = request.args.get('nickname') if username: user = User.query.filter(User.user_username == username).first() if user is None: return jsonify({'code': 0, 'msg': 'user not exist'}) else: return jsonify({'code': 1, 'data': user.to_json()}) elif nickname: users = User.query.filter(User.user_nickname == nickname).all() if users: return json_data(1, [user.to_json() for user in users]) else: return json_data(0, 'no data') else: return json_data(0, 'bad request')
def add_question(): json = request.json question_detail = json.get('question_detail') user_id = json.get('user_id') question_draft = json.get('question_draft', 0) if user_id is None or not type(user_id) == int: return jsonify({'code': 0, 'msg': 'bad request'}) question = Question(question_detail, user_id, question_draft) db.session.add(question) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def update_question(id): json = request.json question_detail = json.get('question_detail') question_draft = json.get('question_draft', 0) question = Question.query.get(id) if question is None: return jsonify({'code': 0, 'msg': 'question not exist'}) question.question_draft = question_draft if question_detail: question.question_detail = question_detail try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def get_question(id): question = Question.query.get(id) if question is None: return jsonify({'code': 0, 'msg': 'question not exist'}) else: click = Click.query.filter( and_(Click.question_id == id, Click.click_date == datetime.now().date())).first() if not click: new_click = Click(id) db.session.add(new_click) else: click.click_count += 1 try: db.session.commit() except Exception, e: return json_data(0, e.message) return jsonify({'code': 1, 'data': question.to_json()})
def add_user(): json = request.json user_username = json.get('user_username') user_password = json.get('user_password') user_type = json.get('user_type') user_authentication = json.get('user_authentication', None) user_nickname = json.get('user_nickname') if user_username is None or user_username == '' \ or user_password is None or user_password == '' \ or user_type is None or user_type == ''\ or user_nickname is None or user_nickname == '': return jsonify({'code': 0, 'msg': 'bad request'}) if User.query.filter(User.user_username == request.json.get('user_username')).all(): return jsonify({'code': 0, 'msg': 'username existed'}) user = User(user_username, user_password, user_type, user_authentication, user_nickname) db.session.add(user) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def get_comment(id): comment = Comment.query.get(id) if not comment: return json_data(0, 'comment not exist') else: return json_data(1, comment.to_json())
def get_answer(id): answer = Answer.query.get(id) if answer is None: return json_data(0, 'answer not exist') else: return json_data(1, answer.to_json())
question_id = json.get('question_id') user_id = json.get('user_id') answer_detail = json.get('answer_detail') answer_draft = json.get('answer_draft', 0) if not user_id or not type(user_id) == int or not question_id or not type(question_id) == int: return json_data(0, 'bad request') answer = Answer(question_id, user_id, answer_detail, answer_draft) db.session.add(answer) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message) return json_data(1, answer.to_json()) @api.route('/answer/<int:id>', methods=['PUT']) def update_answer(id): json = request.json answer_detail = json.get('answer_detail') answer_draft = json.get('answer_draft', 0) answer = Answer.query.get(id) if answer is None: return json_data(0, 'answer not exist') answer.answer_draft = answer_draft if answer_detail: answer.answer_detail = answer_detail
def get_collection(id): collection = Collection.query.get(id) if not collection: return json_data(0, 'collection not exist') else: return json_data(1, collection.to_json())
if not user_id or not type(user_id) == int or not question_id or not type(question_id) == int: return json_data(0, 'bad request') conditions = [(Collection.user_id == user_id), (Collection.question_id == question_id)] if Collection.query.filter(and_(*conditions)).all(): return json_data(0, 'collection existed') collection = Collection(user_id, question_id) db.session.add(collection) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message) return json_data(1, collection.to_json()) @api.route('/collection/<int:id>', methods=['DELETE']) def delete_collection(id): collection = Collection.query.get(id) if not collection: return json_data(0, 'collection not exist') db.session.delete(collection) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message)
def get_type(id): t = Type.query.get(id) if not t: return json_data(0, 'type not exist') else: return json_data(1, t.to_json())
user_id = json.get('user_id') question_id = json.get('question_id') comment_detail = json.get('comment_detail') if not user_id or not type(user_id) == int or not question_id or not type( question_id) == int: return json_data(0, 'bad request') comment = Comment(comment_detail, user_id, question_id) db.session.add(comment) try: db.session.commit() except Exception, e: db.session.rollback() return json_data(0, e.message) return json_data(1, comment.to_json()) @api.route('/comment/<int:id>', methods=['PUT']) def update_comment(id): json = request.json comment_detail = json.get('comment_detail') comment = Comment.query.get(id) if not comment: return json_data(0, 'comment not exist') comment.comment_detail = comment_detail try: db.session.commit() except Exception, e: