def restore_answer_comment(question_id, answer_id, comment_id): c: Optional[Comment] = Comment.get(comment_id, "comment not found") a: Optional[Answer] = Answer.get(answer_id, "comment not found") q: Optional[Question] = Question.get(question_id, "comment not found") if a.parent_id != q._id or c.parent_id != a._id: raise NotFound("comment not found") restore_post(c) return json_response({"data": c.api_dict(COMMENT_FIELDS)})
def accept_answer(question_id, answer_id): u: User = get_user_from_app_context() q: Optional[Question] = Question.get(question_id, "answer not found") a: Optional[Answer] = Answer.get(answer_id, "answer not found") if a.parent_id != q._id: raise NotFound("answer not found") if q.author_id != u._id: raise Forbidden("only question's author can accept answers") q.set_accepted_answer(a) return json_response({"data": a.api_dict(ANSWER_FIELDS)})
def create_answer_comment(question_id, answer_id): a: Optional[Answer] = Answer.get(answer_id, "answer not found") if a.parent_id != resolve_id(question_id): raise NotFound("answer not found") user: User = get_user_from_app_context() attrs = request.json if "body" not in attrs: raise ApiError("body is missing") c = a.create_comment({"body": attrs["body"], "author_id": user._id}) c.save() return json_response({"data": c.api_dict(COMMENT_FIELDS)})
def revoke_answer(question_id, answer_id): u: User = get_user_from_app_context() q: Optional[Question] = Question.get(question_id, "answer not found") a: Optional[Answer] = Answer.get(answer_id, "answer not found") if a.parent_id != q._id: raise NotFound("answer not found") if q.author_id != u._id: raise Forbidden("only question's author can revoke answers") if not a.accepted: raise NotAccepted("answer is not accepted so can't be revoked") q.set_accepted_answer(None) a.reload() return json_response({"data": a.api_dict(ANSWER_FIELDS)})
def vote_answer(question_id, answer_id): a: Optional[Answer] = Answer.get(answer_id, "answer not found") if a.parent_id != resolve_id(question_id): raise NotFound("answer not found") u: User = get_user_from_app_context() if a.author_id == u._id: raise Forbidden("you can't vote for your own answers") attrs = request.json if "value" not in attrs: raise ApiError("value field is mandatory") Vote.vote(a._id, u._id, attrs["value"]) a.reload() return json_response({"data": a.api_dict(fields=ANSWER_FIELDS)})
def vote_answer_comment(question_id, answer_id, comment_id): c: Optional[Comment] = Comment.get(comment_id, "comment not found") a: Optional[Answer] = Answer.get(answer_id, "comment not found") q: Optional[Question] = Question.get(question_id, "comment not found") if a.parent_id != q._id or c.parent_id != a._id: raise NotFound("comment not found") u: User = get_user_from_app_context() if c.author_id == u._id: raise Forbidden("you can't vote for your own comments") attrs = request.json if "value" not in attrs: raise ApiError("value field is mandatory") Vote.vote(c._id, u._id, attrs["value"]) c.reload() return json_response({"data": c.api_dict(fields=COMMENT_FIELDS)})
def restore_answer(question_id, answer_id): a: Optional[Answer] = Answer.get(answer_id, "answer not found") if a.parent_id != resolve_id(question_id): raise NotFound("answer not found") restore_post(a) return json_response({"data": a.api_dict(ANSWER_FIELDS)})