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

View File

@@ -21,7 +21,8 @@
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
{% if q and field %} {% if q and field %}
<h4 class="text-center">Searching for challenges with {{field}} matching {{q}}</h4> <h5 class="text-muted text-center">Searching for challenges with <strong>{{ field }}</strong> matching <strong>{{ q }}</strong></h5>
<h6 class="text-muted text-center pb-3">{{ total }} results</h6>
{% endif %} {% endif %}
<form method="GET" class="form-inline"> <form method="GET" class="form-inline">