|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from flask import request, session |
| 4 | +from flask_restx import Namespace, Resource |
| 5 | + |
| 6 | +from CTFd.api.v1.helpers.request import validate_args |
| 7 | +from CTFd.api.v1.helpers.schemas import sqlalchemy_to_pydantic |
| 8 | +from CTFd.api.v1.schemas import APIDetailedSuccessResponse, APIListSuccessResponse |
| 9 | +from CTFd.constants import RawEnum |
| 10 | +from CTFd.models import ( |
| 11 | + ChallengeComments, |
| 12 | + Comments, |
| 13 | + PageComments, |
| 14 | + TeamComments, |
| 15 | + UserComments, |
| 16 | + db, |
| 17 | +) |
| 18 | +from CTFd.schemas.comments import CommentSchema |
| 19 | +from CTFd.utils.decorators import admins_only |
| 20 | +from CTFd.utils.helpers.models import build_model_filters |
| 21 | + |
| 22 | +comments_namespace = Namespace("comments", description="Endpoint to retrieve Comments") |
| 23 | + |
| 24 | + |
| 25 | +CommentModel = sqlalchemy_to_pydantic(Comments) |
| 26 | + |
| 27 | + |
| 28 | +class CommentDetailedSuccessResponse(APIDetailedSuccessResponse): |
| 29 | + data: CommentModel |
| 30 | + |
| 31 | + |
| 32 | +class CommentListSuccessResponse(APIListSuccessResponse): |
| 33 | + data: List[CommentModel] |
| 34 | + |
| 35 | + |
| 36 | +comments_namespace.schema_model( |
| 37 | + "CommentDetailedSuccessResponse", CommentDetailedSuccessResponse.apidoc() |
| 38 | +) |
| 39 | + |
| 40 | +comments_namespace.schema_model( |
| 41 | + "CommentListSuccessResponse", CommentListSuccessResponse.apidoc() |
| 42 | +) |
| 43 | + |
| 44 | + |
| 45 | +def get_comment_model(data): |
| 46 | + model = Comments |
| 47 | + if "challenge_id" in data: |
| 48 | + model = ChallengeComments |
| 49 | + elif "user_id" in data: |
| 50 | + model = UserComments |
| 51 | + elif "team_id" in data: |
| 52 | + model = TeamComments |
| 53 | + elif "page_id" in data: |
| 54 | + model = PageComments |
| 55 | + else: |
| 56 | + model = Comments |
| 57 | + return model |
| 58 | + |
| 59 | + |
| 60 | +@comments_namespace.route("") |
| 61 | +class CommentList(Resource): |
| 62 | + @admins_only |
| 63 | + @comments_namespace.doc( |
| 64 | + description="Endpoint to list Comment objects in bulk", |
| 65 | + responses={ |
| 66 | + 200: ("Success", "CommentListSuccessResponse"), |
| 67 | + 400: ( |
| 68 | + "An error occured processing the provided or stored data", |
| 69 | + "APISimpleErrorResponse", |
| 70 | + ), |
| 71 | + }, |
| 72 | + ) |
| 73 | + @validate_args( |
| 74 | + { |
| 75 | + "challenge_id": (int, None), |
| 76 | + "user_id": (int, None), |
| 77 | + "team_id": (int, None), |
| 78 | + "page_id": (int, None), |
| 79 | + "q": (str, None), |
| 80 | + "field": (RawEnum("CommentFields", {"content": "content"}), None), |
| 81 | + }, |
| 82 | + location="query", |
| 83 | + ) |
| 84 | + def get(self, query_args): |
| 85 | + q = query_args.pop("q", None) |
| 86 | + field = str(query_args.pop("field", None)) |
| 87 | + CommentModel = get_comment_model(data=query_args) |
| 88 | + filters = build_model_filters(model=CommentModel, query=q, field=field) |
| 89 | + |
| 90 | + comments = ( |
| 91 | + CommentModel.query.filter_by(**query_args) |
| 92 | + .filter(*filters) |
| 93 | + .order_by(CommentModel.id.desc()) |
| 94 | + .paginate(max_per_page=100) |
| 95 | + ) |
| 96 | + schema = CommentSchema(many=True) |
| 97 | + response = schema.dump(comments.items) |
| 98 | + |
| 99 | + if response.errors: |
| 100 | + return {"success": False, "errors": response.errors}, 400 |
| 101 | + |
| 102 | + return { |
| 103 | + "meta": { |
| 104 | + "pagination": { |
| 105 | + "page": comments.page, |
| 106 | + "next": comments.next_num, |
| 107 | + "prev": comments.prev_num, |
| 108 | + "pages": comments.pages, |
| 109 | + "per_page": comments.per_page, |
| 110 | + "total": comments.total, |
| 111 | + } |
| 112 | + }, |
| 113 | + "success": True, |
| 114 | + "data": response.data, |
| 115 | + } |
| 116 | + |
| 117 | + @admins_only |
| 118 | + @comments_namespace.doc( |
| 119 | + description="Endpoint to create a Comment object", |
| 120 | + responses={ |
| 121 | + 200: ("Success", "CommentDetailedSuccessResponse"), |
| 122 | + 400: ( |
| 123 | + "An error occured processing the provided or stored data", |
| 124 | + "APISimpleErrorResponse", |
| 125 | + ), |
| 126 | + }, |
| 127 | + ) |
| 128 | + def post(self): |
| 129 | + req = request.get_json() |
| 130 | + # Always force author IDs to be the actual user |
| 131 | + req["author_id"] = session["id"] |
| 132 | + CommentModel = get_comment_model(data=req) |
| 133 | + |
| 134 | + m = CommentModel(**req) |
| 135 | + db.session.add(m) |
| 136 | + db.session.commit() |
| 137 | + |
| 138 | + schema = CommentSchema() |
| 139 | + |
| 140 | + response = schema.dump(m) |
| 141 | + db.session.close() |
| 142 | + |
| 143 | + return {"success": True, "data": response.data} |
| 144 | + |
| 145 | + |
| 146 | +@comments_namespace.route("/<comment_id>") |
| 147 | +class Comment(Resource): |
| 148 | + @admins_only |
| 149 | + @comments_namespace.doc( |
| 150 | + description="Endpoint to delete a specific Comment object", |
| 151 | + responses={200: ("Success", "APISimpleSuccessResponse")}, |
| 152 | + ) |
| 153 | + def delete(self, comment_id): |
| 154 | + comment = Comments.query.filter_by(id=comment_id).first_or_404() |
| 155 | + db.session.delete(comment) |
| 156 | + db.session.commit() |
| 157 | + db.session.close() |
| 158 | + |
| 159 | + return {"success": True} |
0 commit comments