Esempio n. 1
0
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)})
Esempio n. 2
0
def vote_comment(question_id, comment_id):
    c: Optional[Comment] = Comment.get(comment_id, "comment not found")
    if c.parent_id != resolve_id(question_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. 3
0
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)})
Esempio n. 4
0
def list_users(ids):
    ids = ids.split(",")
    ids = [resolve_id(id_) for id_ in ids]
    items = User.find(
        {"$or": [
            {
                "_id": {
                    "$in": ids
                }
            },
            {
                "username": {
                    "$in": ids
                }
            },
        ]})
    items = [user.to_dict(USER_FIELDS) for user in items]
    return {"data": items}
Esempio n. 5
0
def index():
    u: User = get_user_from_app_context()

    if "_sort" in request.values:
        srt = request.values["_sort"]
    else:
        srt = "rating"

    sortExpr = SORT_MAP.get(srt)
    if sortExpr is None:
        raise ApiError(f"unknown sort operator \"{srt}\"")

    if get_boolean_request_param("_mine"):
        if u:
            # only mine posts
            query = {"author_id": u._id}
        else:
            # nothing to show, user is not logged in
            query = {"_id": NilObjectId}
    else:
        # otherwise show all not deleted posts
        query = {
            "$or": [
                {
                    "deleted": False
                },
            ]
        }
        if u:
            # if user is logged in, he can view his own deleted posts
            query["$or"].append({"author_id": u._id})

    questions = Question.find(query).sort(sortExpr)
    results = paginated(
        questions, transform=default_transform(fields=QUESTION_LIST_FIELDS))
    author_ids = set()
    for q in results["data"]:
        author_ids.add(resolve_id(q["author_id"]))
    authors = User.find({"_id": {"$in": list(author_ids)}})
    return json_response({"questions": results, "authors": {"data": authors}})
Esempio n. 6
0
def restore_comment(question_id, comment_id):
    c: Optional[Comment] = Comment.get(comment_id, "comment not found")
    if c.parent_id != resolve_id(question_id):
        raise NotFound("comment not found")
    restore_post(c)
    return json_response({"data": c.api_dict(COMMENT_FIELDS)})
Esempio n. 7
0
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)})