Show search results on challenge search

This commit is contained in:
Kevin Chung
2020-04-27 16:40:58 -04:00
parent b8b14a568e
commit 839a767c8c
2 changed files with 18 additions and 15 deletions

View File

@@ -3,6 +3,7 @@ import os
import six
from flask import current_app as app
from flask import render_template, render_template_string, request, url_for
from flask_sqlalchemy import Pagination
from CTFd.admin import admin
from CTFd.models import Challenges, Flags, Solves
@@ -16,22 +17,23 @@ from CTFd.utils.decorators import admins_only
def challenges_listing():
q = request.args.get("q")
field = request.args.get("field")
filters = []
if q:
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()
)
else:
challenges = Challenges.query.all()
# The field exists as an exposed column
if Challenges.__mapper__.has_property(field):
filters.append(getattr(Challenges, field).like("%{}%".format(q)))
query = Challenges.query.filter(*filters).order_by(Challenges.id.asc())
challenges = query.all()
total = query.count()
return render_template(
"admin/challenges/challenges.html", challenges=challenges, q=q, field=field
"admin/challenges/challenges.html",
challenges=challenges,
total=total,
q=q,
field=field,
)