Add basic challenge searching functionality

This commit is contained in:
Kevin Chung
2020-04-24 15:35:00 -04:00
parent 2f36c5d680
commit 449e0d8939
2 changed files with 54 additions and 3 deletions

View File

@@ -2,20 +2,41 @@ import os
import six
from flask import current_app as app
from flask import render_template, render_template_string, url_for
from flask import render_template, render_template_string, request, url_for
from CTFd.admin import admin
from CTFd.models import Challenges, Flags, Solves
from CTFd.plugins.challenges import get_chal_class
from CTFd.utils import binary_type
from CTFd.utils.decorators import admins_only
from CTFd.utils.helpers import get_errors
@admin.route("/admin/challenges")
@admins_only
def challenges_listing():
challenges = Challenges.query.all()
return render_template("admin/challenges/challenges.html", challenges=challenges)
q = request.args.get("q")
if q:
field = request.args.get("field")
challenges = []
if Challenges.__mapper__.has_property(
field
): # The field exists as an exposed column
challenges = (
Challenges.query.filter(
getattr(Challenges, field).like("%{}%".format(q))
)
.order_by(Challenges.id.asc())
.all()
)
return render_template(
"admin/challenges/challenges.html", challenges=challenges, q=q, field=field
)
else:
challenges = Challenges.query.all()
return render_template(
"admin/challenges/challenges.html", challenges=challenges
)
@admin.route("/admin/challenges/<int:challenge_id>")