import json from flask import Flask from flask_cors import CORS from flask import request from initial_learning import get_initial_path from review_learning import get_review_path from pre_exam_learning import get_pre_exam_path app = Flask(__name__) CORS(app, resources = r'/*') @app.route('/get_study_path', methods=["POST"]) def get_study_path(): ''' 以RESTful的方式获取初始学习路径推荐结果 传入参数为: course_id,student_id,method :param course_id: 教学课堂ID :param student_id: 学生ID :param method: 学习路径场景 0: 初始学习路径 1: 复习学习路径 2: 考前学习路径 :return 以json格式返回学习路径 ''' result = {} course_id = request.form.get('course_id', type=str, default='') student_id = request.form.get('student_id', type=str, default='') method = request.form.get('method', type=str, default='0') platform = request.form.get('platform', type=str, default='0') # course_id为空时,参数错误 if course_id.strip() == '': result = { "course_id": str('False'), "error_msg": str('参数错误: 缺少course_id') } return json.dumps(result, ensure_ascii=False) #用户ID为空,推荐初始学习路径 if student_id.strip() == '': recommend_results=get_initial_path(course_id, platform) result = { "course_id": str(course_id), "student_id": str(student_id), "recommend_count": str(len(recommend_results)), "recommend_results": recommend_results } return json.dumps(result, ensure_ascii=False) # 0:初始学习路径推荐 if method == '0': recommend_results=get_initial_path(course_id, platform) result = { "course_id": str(course_id), "student_id": str(student_id), "recommend_count": str(len(recommend_results)), "recommend_results": recommend_results } return json.dumps(result, ensure_ascii=False) # 1:复习学习路径推荐 if method == '1': recommend_results=get_review_path(course_id, platform=platform) result = { "course_id": str(course_id), "student_id": str(student_id), "recommend_count": str(len(recommend_results)), "recommend_results": recommend_results } return json.dumps(result, ensure_ascii=False) # 2:考前学习路径推荐 if method == '2': recommend_results=get_pre_exam_path(course_id, platform=platform) result = { "course_id": str(course_id), "student_id": str(student_id), "recommend_count": str(len(recommend_results)), "recommend_results": recommend_results } return json.dumps(result, ensure_ascii=False) # method不在取值范围中,参数错误 if method not in ['0','1','2']: result = { "method": str('False'), "error_msg": str("参数错误: method不属于['0','1','2']的应用场景") } return json.dumps(result, ensure_ascii=False) # python -m flask run if __name__ == '__main__': app.run(host='0.0.0.0', port=8087, debug=True, use_reloader=False)