Add basic challenge searching functionality

in-house-export-serialization
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>")

View File

@ -18,6 +18,36 @@
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
{% if q and field %}
<h4 class="text-center">Searching for challenges with {{field}} matching {{q}}</h4>
{% endif %}
<form method="GET" class="form-inline">
<div class="form-group col-md-2">
<label for="sel1" class="sr-only" >Search Field</label>
<select class="form-control custom-select w-100" id="sel1" name="field">
<option value="name" {% if field == 'name' %}selected{% endif %}>Name</option>
<option value="id" {% if field == 'id' %}selected{% endif %}>ID</option>
<option value="category" {% if field == 'category' %}selected{% endif %}>Category</option>
<option value="type" {% if field == 'type' %}selected{% endif %}>Type</option>
</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 %}>
</div>
<div class="form-group col-md-2">
<label for="team-name-search" 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>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-12">
<div class="float-right pb-3">