def recommend(): stdId = request.args.get('stdid') user = UserOp.query(stdId) user.view = "1\t2" posting_arr = user.posting.split("\t") follow_arr = user.follow.split("\t") view_arr = user.view.split("\t") que_arr = posting_arr + follow_arr + view_arr q_list = [] for q in que_arr: if int(q) not in q_list: q_list.append(int(q)) list1, list2 = QAOp.get_recommend(q_list) # get more questions to show que_count = QAOp.get_que_count() else_q_list = [] print(que_count) print(else_q_list) for i in range(1, que_count + 1): if i not in q_list: else_q_list.append(i) if len(else_q_list) > 0: _list1, _list2 = QAOp.get_recommend(else_q_list) ret = get_homepage(list1 + _list1, list2 + _list2) else: ret = get_homepage(list1, list2) # ret = get_homepage(list1, list2) return jsonify(ret)
def add_question(): # get parameters...... # questioner, title, label, description, isPublished data = request.get_data() j_data = json.loads(data) q_id = QAOp.insertQuestion(j_data["questioner"], j_data["title"], j_data["label"], j_data["description"], j_data["isPublished"]) UserOp.add_posting(j_data["questioner"], q_id) QAOp.printQuestions() UserOp.printf() return ms.ADD_QUESTION_OK
def answers_list(): ret = {} stdId = request.args.get('stdid') q_id = request.args.get('queid') que = QAOp.getQ(q_id) ret.setdefault("questioner", UserOp.query(que.questioner).nickname) ret.setdefault("title", que.title) ret.setdefault("label", que.label) ret.setdefault("description", que.description) ret.setdefault("numFollowers", que.numFollowers) ret.setdefault("numAnswers", que.numAnswers) ret.setdefault("date", que.date) QAOp.printQuestions() QAOp.printAnswers() UserOp.add_view(stdId, q_id) UserOp.printf() list = QAOp.showAnswers(q_id) ret.setdefault("num", len(list)) print(len(list)) for i in range(0, len(list)): dict = {} a = list[i] dict.setdefault("answerId", a[0]) dict.setdefault("answerer", a[1]) dict.setdefault("answerer_name", UserOp.query(a[1]).nickname) dict.setdefault("content", a[2]) dict.setdefault("numAgree", a[3]) dict.setdefault("numCollect", a[4]) dict.setdefault("date", a[5]) ret.setdefault("answer" + str(i), dict) return jsonify(ret)
def answer_detail(): ret = {} stdId = request.args.get('stdid') a_id = request.args.get('ansid') ans = QAOp.show_answer_detail(a_id) ret.setdefault("answerer", ans.answerer) ret.setdefault("answerer_name", UserOp.query(ans.answerer).nickname) ret.setdefault("questionID", ans.questionID) ret.setdefault("content", ans.content) ret.setdefault("isPublished", ans.isPublished) ret.setdefault("numAgree", ans.numAgree) ret.setdefault("numCollect", ans.numCollect) ret.setdefault("date", ans.date) return jsonify(ret)
def add_answer(): # get parameters...... # answerer, questionID, content, isPublished data = request.get_data() j_data = json.loads(data) a_id = QAOp.insertAnswer(j_data["answerer"], j_data["questionID"], j_data["content"], j_data["isPublished"]) QAOp.add_num_answers(j_data["questionID"]) UserOp.add_answer(j_data["answerer"], a_id) QAOp.printQuestions() QAOp.printAnswers() UserOp.printf() return ms.ADD_ANSWER_OK
def collection(): stdId = request.args.get('stdid') q_usr = UserOp.query(stdId) collec = q_usr.collection ret = {} split = collec.split("\t") num = len(split) ret.setdefault("answer_number", num) if num == 0: return jsonify(ret) else: for i in range(num): dict = {} answerId = int(split[i]) ans = QAOp.show_answer_detail(answerId) que = QAOp.show_question_detail(ans.questionID) dict.setdefault("question_id", str(ans.questionID)) dict.setdefault("question", que.title) dict.setdefault("answer_id", answerId) dict.setdefault("answer", ans.content) dict.setdefault("num_followers", que.numFollowers) dict.setdefault("num_answers", que.numAnswers) ret.setdefault("answer" + str(i), dict) return jsonify(ret)
def answer(): stdId = request.args.get('stdid') q_usr = UserOp.query(stdId) ans = q_usr.answer ret = {} split = ans.split("\t") num = len(split) ret.setdefault("answer_number", num) if num == 0: return jsonify(ret) else: for i in range(num): dict = {} answerId = int(split[i]) ans = QAOp.show_answer_detail(answerId) que = QAOp.show_question_detail(ans.questionID) dict.setdefault("question_id", str(ans.questionID)) dict.setdefault("question", que.title) dict.setdefault("answer_id", answerId) dict.setdefault("answer", ans.content) dict.setdefault("date", ans.date) dict.setdefault("is_draft", ans.isPublished) ret.setdefault("answer" + str(i), dict) return jsonify(ret)
def get_follow_and_view(f_or_v): ret = {} split = f_or_v.split("\t") num = len(split) ret.setdefault("question_number", num) if num == 0: return jsonify(ret) else: for i in range(num): if split[i] is not None: dict = {} questionId = int(split[i]) que = QAOp.show_question_detail(questionId) dict.setdefault("question_id", questionId) dict.setdefault("question", que.title) dict.setdefault("num_followers", que.numFollowers) dict.setdefault("num_answers", que.numAnswers) ret.setdefault("question" + str(i), dict) return jsonify(ret)
def posting(): stdId = request.args.get('stdid') ret = {} q_usr = UserOp.query(stdId) posting = q_usr.posting split = posting.split("\t") num = len(split) ret.setdefault("question_number", num) if num == 0: return jsonify(ret) else: for i in range(num): dict = {} questionId = int(split[i]) que = QAOp.show_question_detail(questionId) dict.setdefault("question_id", questionId) dict.setdefault("question", que.title) dict.setdefault("question_description", que.description) dict.setdefault("date", que.date) dict.setdefault("is_draft", que.isPublished) ret.setdefault("question" + str(i), dict) return jsonify(ret)
def add_collection(): stdId = request.args.get('stdid') ansId = request.args.get('ansid') QAOp.add_num_collect(ansId) UserOp.add_collection(stdId, ansId) return ms.ADD_OK
def add_agree(): stdId = request.args.get('stdid') ansId = request.args.get('ansid') QAOp.add_num_collect(ansId) return ms.ADD_OK
def add_follow(): stdId = request.args.get('stdid') queId = request.args.get('queid') QAOp.add_num_followers(queId) UserOp.add_follow(stdId, queId) return ms.ADD_OK
def search(): key = request.args.get('keyword') list1, list2 = QAOp.get_search(key) ret = get_homepage(list1, list2) return jsonify(ret)
def home_page(): list1, list2 = QAOp.showQuestions() ret = get_homepage(list1, list2) return jsonify(ret)