Esempio n. 1
0
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)})
Esempio n. 2
0
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)})
Esempio n. 3
0
def create_comment(question_id):
    q = Question.get(question_id, "question not found")
    user: User = get_user_from_app_context()
    attrs = request.json

    if "body" not in attrs:
        raise ApiError("body is missing")

    c = q.create_comment({"body": attrs["body"], "author_id": user._id})
    c.save()
    return json_response({"data": c.api_dict(COMMENT_FIELDS)})
Esempio n. 4
0
def create_answer(question_id):
    q: Optional[Question] = Question.get(question_id, "question not found")
    user: User = get_user_from_app_context()
    attrs = request.json

    if "body" not in attrs:
        raise ApiError("body is missing")

    a = q.create_answer({"author_id": user._id, "body": attrs["body"]})
    a.save()

    return json_response({"data": a.api_dict()})
Esempio n. 5
0
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)})
Esempio n. 6
0
def vote_question(question_id):
    q: Optional[Question] = Question.get(question_id, "question not found")
    u: User = get_user_from_app_context()
    if q.author_id == u._id:
        raise Forbidden("you can't vote for your own question")

    attrs = request.json

    if "value" not in attrs:
        raise ApiError("value field is mandatory")

    Vote.vote(q._id, u._id, attrs["value"])
    q.reload()
    return json_response({"data": q.api_dict(fields=QUESTION_FIELDS)})
Esempio n. 7
0
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)})
Esempio n. 8
0
    def test_create_question(self):
        attrs = {
            "title": "a real question",
            "body": "what is the meaning of life and everything",
            "tags": ["guide"]
        }
        resp = self.post(f"/api/v1/questions/", json=attrs)
        self.assertEqual(resp.status_code, 401)

        resp = self.post(f"/api/v1/questions/", json=attrs, user=self.user1)
        self.assertEqual(resp.status_code, 200)
        self.run_tasks()

        data = resp.json
        q = Question.get(data["data"]["_id"])
        self.assertEqual(q.body, attrs["body"])
        self.assertEqual(q.title, attrs["title"])
        self.assertCountEqual(q.tags, attrs["tags"])

        t: Tag = Tag.get("guide")
        self.assertIsNotNone(t)
        self.assertEqual(t.questions_count, 1)
Esempio n. 9
0
def restore(question_id):
    q: Optional[Question] = Question.get(question_id, "question not found")
    restore_post(q)
    return json_response({"data": q.api_dict(QUESTION_FIELDS)})
Esempio n. 10
0
def show(question_id):
    q: Optional[Question] = Question.get(question_id, "question not found")
    q.inc_views()
    return json_response({"data": q.everything()})