Skip to content

Commit ec587b8

Browse files
committed
added fields api from user
1 parent c1b7c60 commit ec587b8

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

__init__.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from flask import send_file, jsonify
1+
from flask import send_file, jsonify, request
22
from werkzeug.utils import safe_join
33
from CTFd.models import Challenges, Solves, Pages, db
4+
from CTFd.models import TeamFields, UserFields
45
from CTFd.schemas.pages import PageSchema
56
from CTFd.utils import get_config
67
from CTFd.utils.modes import get_model
@@ -54,6 +55,44 @@ def rules():
5455

5556
return {"success": True, "data": response.data}
5657

58+
@app.route("/api/v1/fields", methods=["GET"])
59+
def list_fields():
60+
t = (request.args.get("type") or "").lower()
61+
62+
if t in ("team", "teams"):
63+
q = TeamFields.query
64+
elif t in ("user", "users"):
65+
q = UserFields.query
66+
else:
67+
return (
68+
jsonify(
69+
{
70+
"success": False,
71+
"data": None,
72+
"errors": {
73+
"type": "Missing or invalid ?type (team|user)"
74+
},
75+
}
76+
),
77+
400,
78+
)
79+
80+
fields = q.all()
81+
82+
response = []
83+
for f in fields:
84+
response.append(
85+
{
86+
"id": f.id,
87+
"name": getattr(f, "name", None),
88+
"field_type": getattr(f, "field_type", None),
89+
"description": getattr(f, "description", None),
90+
"required": bool(getattr(f, "required", False)),
91+
}
92+
)
93+
94+
return jsonify({"success": True, "data": response})
95+
5796
@app.route("/api/v1/challenges/solves", methods=["GET"])
5897
@during_ctf_time_only
5998
def solves():

0 commit comments

Comments
 (0)