Make admin team searching use a pagination object

admin-teams-pagination
Kevin Chung 2020-04-27 16:27:06 -04:00
parent b8b14a568e
commit 35737e9eea
2 changed files with 34 additions and 57 deletions

View File

@ -1,4 +1,4 @@
from flask import render_template, request
from flask import render_template, request, url_for
from sqlalchemy.sql import not_
from CTFd.admin import admin
@ -10,57 +10,34 @@ from CTFd.utils.helpers import get_errors
@admin.route("/admin/teams")
@admins_only
def teams_listing():
page = abs(request.args.get("page", 1, type=int))
q = request.args.get("q")
if q:
field = request.args.get("field")
teams = []
errors = get_errors()
if field == "id":
if q.isnumeric():
teams = Teams.query.filter(Teams.id == q).order_by(Teams.id.asc()).all()
else:
teams = []
errors.append("Your ID search term is not numeric")
elif field == "name":
page = abs(request.args.get("page", 1, type=int))
filters = []
if q:
# The field exists as an exposed column
if Teams.__mapper__.has_property(field):
filters.append(getattr(Teams, field).like("%{}%".format(q)))
teams = (
Teams.query.filter(Teams.name.like("%{}%".format(q)))
Teams.query.filter(*filters)
.order_by(Teams.id.asc())
.all()
)
elif field == "email":
teams = (
Teams.query.filter(Teams.email.like("%{}%".format(q)))
.order_by(Teams.id.asc())
.all()
)
elif field == "affiliation":
teams = (
Teams.query.filter(Teams.affiliation.like("%{}%".format(q)))
.order_by(Teams.id.asc())
.all()
.paginate(page=page, per_page=50)
)
args = dict(request.args)
args.pop("page", 1)
return render_template(
"admin/teams/teams.html",
teams=teams,
pages=0,
curr_page=None,
prev_page=url_for(request.endpoint, page=teams.prev_num, **args),
next_page=url_for(request.endpoint, page=teams.next_num, **args),
q=q,
field=field,
)
page = abs(int(page))
results_per_page = 50
page_start = results_per_page * (page - 1)
page_end = results_per_page * (page - 1) + results_per_page
teams = Teams.query.order_by(Teams.id.asc()).slice(page_start, page_end).all()
count = db.session.query(db.func.count(Teams.id)).first()[0]
pages = int(count / results_per_page) + (count % results_per_page > 0)
return render_template(
"admin/teams/teams.html", teams=teams, pages=pages, curr_page=page
)
@admin.route("/admin/teams/new")
@admins_only

View File

@ -19,8 +19,9 @@
<div class="container">
<div class="row">
<div class="col-md-12">
{% if q and field%}
<h4 class="text-center">Searching for teams with {{field}} matching {{q}}</h4>
{% if q and field %}
<h5 class="text-muted text-center">Searching for teams with <strong>{{ field }}</strong> matching <strong>{{ q }}</strong></h5>
<h6 class="text-muted text-center pb-3">Page {{ teams.page }} of {{ teams.total }} results</h6>
{% endif %}
<form method="GET" class="form-inline">
@ -29,13 +30,12 @@
<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="email" {% if field == 'email' %}selected{% endif %}>Email</option>
<option value="affiliation" {% if field == 'affiliation' %}selected{% endif %}>Affiliation</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 team names" {% if q %}value="{{q}}"{% endif %}>
<input type="text" class="form-control w-100" id="team-name-search" name="q" placeholder="Search for matching teams" {% if q %}value="{{q}}"{% endif %}>
</div>
<div class="form-group col-md-2">
<label for="team-name-search" class="sr-only">Search</label>
@ -81,7 +81,7 @@
</tr>
</thead>
<tbody>
{% for team in teams %}
{% for team in teams.items %}
<tr name="{{ team.id }}" data-href="{{ url_for('admin.teams_detail', team_id=team.id) }}">
<td class="border-right" data-checkbox>
<div class="form-check text-center">
@ -139,19 +139,19 @@
{% endfor %}
</tbody>
</table>
{% if pages > 1 %}
{% if teams.pages > 1 %}
<div class="text-center">Page
<br>
{% if curr_page != 1 %}
<a href="{{ url_for('admin.teams_listing', page=curr_page - 1) }}">&lt;&lt;&lt;</a>
{% if teams.page != 1 %}
<a href="{{ prev_page }}">&lt;&lt;&lt;</a>
{% endif %}
<select class="page-select">
{% for page in range(1, pages + 1) %}
<option {% if curr_page == page %}selected{% endif %}>{{ page }}</option>
{% for page in range(1, teams.pages + 1) %}
<option {% if teams.page == page %}selected{% endif %}>{{ page }}</option>
{% endfor %}
</select>
{% if curr_page != pages %}
<a href="{{ url_for('admin.teams_listing', page=curr_page + 1) }}">&gt;&gt;&gt;</a>
{% if teams.next_num %}
<a href="{{ next_page }}">&gt;&gt;&gt;</a>
{% endif %}
</div>
{% endif %}