def send_picture(cls, card_id): if not isinstance(card_id, get_obj_id()): try: card_id = get_obj_id()(card_id) except: return abort(404) file_obj, content_type = get_img_file(card_id) # No picture => 404 if file_obj is None: return "", 404 return send_file(file_obj, mimetype=content_type)
def disable_question(qid): if qid is None: return abort(400) success = disable_question_in_list(get_obj_id(qid)) if success: return "", 200 else: return "", 400
def reorder_questions(): new_list = request.get_json()["new_list"] new_new_list = [] for x in new_list: new_new_list.append(get_obj_id(x)) if None in new_new_list: return abort(400) reorder_question_list(new_new_list) return "", 200
def enable_question(): qid = request.form.get("qid", None) idx = request.form.get("idx", None) if qid is None or idx is None: return abort(400) q = get_question_by_id(get_obj_id(qid)) if q is None: return abort(400) insert_question(q, idx=int(idx)) return "", 200
def insert_question(question, idx=-1): dq = _dq() if abs(idx) > len(dq): raise IndexError("Out of deque size") if "_id" not in question: question["_id"] = get_obj_id()() dq.rotate((-idx) if idx >= 0 else -(idx + 1)) question["prev"] = dq[-1]["_id"] question["next"] = dq[0]["_id"] dq[-1]["next"] = question["_id"] dq[0]["prev"] = question["_id"] dq.appendleft(question) dq.rotate(idx)
def answer_question_list(qid, answer): qid = get_obj_id(qid) question = get_question_by_id(qid) if question is None: return False if answer not in question["history"]: return False question["history"][answer] += 1 question["response_count"] += 1 get_db().questions.update_one({"_id": qid}, {"$set": question}) return True
def _q_by_id(id): if id is None: me = get_obj_id()() return {"_id": me, "next": me, "prev": me} return get_db().questions.find_one({"_id": id})
def check_resource_id(id): if id is None: return None return get_db().resources.find_one({"_id": get_obj_id(id)})