Add pagination and search to topics

This commit is contained in:
rubenwardy 2018-12-23 18:03:23 +00:00
parent 63969529ad
commit b8ca5d24c5
2 changed files with 42 additions and 5 deletions

@ -8,10 +8,32 @@ Topics to be Added
<h1>Topics to be Added</h1>
<p>
{{ total - (topics | count) }} / {{ total }} packages have been added.
{{ total - (topic_count) }} / {{ total }} packages have been added.
{{ topics | count }} remaining.
</p>
<form method="GET" action="{{ url_for('todo_topics_page') }}" class="my-4">
<input class="" name="q" type="text" placeholder="Search topics" value="{{ query or ''}}">
<input class="btn btn-secondary my-2 my-sm-0 mr-sm-2" type="submit" value="Search" />
</form>
{% from "macros/topics.html" import render_topics_table %}
{{ render_topics_table(topics) }}
<ul class="pagination mt-4">
<li class="page-item {% if not prev_url %}disabled{% endif %}">
<a class="page-link" {% if prev_url %}href="{{ prev_url }}"{% endif %}>&laquo;</a>
</li>
{% for n in range(1, page_max+1) %}
<li class="page-item {% if n == page %}active{% endif %}">
<a class="page-link"
href="{{ url_for('todo_topics_page', page=n) }}">
{{ n }}
</a>
</li>
{% endfor %}
<li class="page-item {% if not next_url %}disabled{% endif %}">
<a class="page-link" {% if next_url %}href="{{ next_url }}"{% endif %}>&raquo;</a>
</li>
</ul>
{% endblock %}

@ -56,9 +56,24 @@ def todo_page():
def todo_topics_page():
total = ForumTopic.query.count()
topics = ForumTopic.query \
query = ForumTopic.query \
.filter(~ db.exists().where(Package.forums==ForumTopic.topic_id)) \
.order_by(db.asc(ForumTopic.wip), db.asc(ForumTopic.name), db.asc(ForumTopic.title)) \
.all()
.order_by(db.asc(ForumTopic.wip), db.asc(ForumTopic.name), db.asc(ForumTopic.title))
return render_template("todo/topics.html", topics=topics, total=total)
topic_count = query.count()
search = request.args.get("q")
if search is not None and search.strip() != "":
query = query.filter(ForumTopic.title.ilike('%' + search + '%'))
page = int(request.args.get("page") or 1)
num = min(100, int(request.args.get("n") or 100))
query = query.paginate(page, num, True)
next_url = url_for("todo_topics_page", page=query.next_num) \
if query.has_next else None
prev_url = url_for("todo_topics_page", page=query.prev_num) \
if query.has_prev else None
return render_template("todo/topics.html", topics=query.items, total=total, \
topic_count=topic_count, query=search, \
next_url=next_url, prev_url=prev_url, page=page, page_max=query.pages)