Merge pull request #1358 from CTFd/admin-challenges-pagination

* Show total searched results for Admin Panel searching. This doesn't use a Pagination object b/c Admin Panel challenges doesn't paginate.
remove-get-config-from-models
Kevin Chung 2020-04-27 20:54:40 -04:00 committed by GitHub
commit 4cfc536b41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 18 deletions

View File

@ -16,22 +16,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,
)

View File

@ -21,7 +21,8 @@
<div class="row">
<div class="col-md-12">
{% 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 %}
<form method="GET" class="form-inline">
@ -35,11 +36,11 @@
</select>
</div>
<div class="form-group col-md-8">
<label for="team-name-search" class="sr-only">Parameter</label>
<input type="text" class="form-control w-100" id="team-name-search" name="q" placeholder="Search for matching challenge" {% if q %}value="{{q}}"{% endif %}>
<label for="challenges-search" class="sr-only">Parameter</label>
<input type="text" class="form-control w-100" id="challenges-search" name="q" placeholder="Search for matching challenge" {% if q %}value="{{q}}"{% endif %}>
</div>
<div class="form-group col-md-2">
<label for="team-name-search" class="sr-only">Search</label>
<label class="sr-only">Search</label>
<button type="submit" class="btn btn-primary w-100"><i class="fas fa-search" aria-hidden="true"></i></button>
</div>
</form>