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(): 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">
@ -35,11 +36,11 @@
</select> </select>
</div> </div>
<div class="form-group col-md-8"> <div class="form-group col-md-8">
<label for="team-name-search" class="sr-only">Parameter</label> <label for="challenges-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 %}> <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>
<div class="form-group col-md-2"> <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> <button type="submit" class="btn btn-primary w-100"><i class="fas fa-search" aria-hidden="true"></i></button>
</div> </div>
</form> </form>