Project index: rig up search

This commit is contained in:
Drew DeVault 2020-04-28 12:00:15 -04:00
parent 777db28c6d
commit 4d68b7d9d8
2 changed files with 28 additions and 5 deletions

View File

@ -1,7 +1,8 @@
from flask import Blueprint, render_template
from flask import Blueprint, render_template, request
from hubsrht.types import Project, Event, EventType, Visibility
from srht.flask import paginate_query
from srht.oauth import current_user, loginrequired
from srht.search import search_by
public = Blueprint("public", __name__)
@ -31,6 +32,21 @@ def getting_started():
def project_index():
projects = (Project.query
.filter(Project.visibility == Visibility.public))
search = request.args.get("search")
if search:
projects = search_by(projects, search,
[Project.name, Project.description])
sort = request.args.get("sort")
if sort and sort == "recently-updated":
projects = projects.order_by(Project.updated.desc())
elif sort and sort == "longest-active":
projects = projects.order_by((Project.updated - Project.created).desc())
else:
projects = projects.order_by(Project.updated.desc())
projects, pagination = paginate_query(projects)
return render_template("project-index.html",
projects=projects, **pagination)
return render_template("project-index.html", projects=projects,
search=search, sort=sort, **pagination)

View File

@ -26,7 +26,10 @@
name="sort"
id="sort-recently-updated"
value="recently-updated"
checked>
{% if sort == "recently-updated" or not sort %}
checked
{% endif %}
>
<label class="form-check-label" for="sort-recently-updated">
Recently updated
</label>
@ -37,7 +40,11 @@
type="radio"
name="sort"
id="sort-longest-active"
value="longest-active">
value="longest-active"
{% if sort == "longest-active" %}
checked
{% endif %}
>
<label class="form-check-label" for="sort-longest-active">
Longest active
</label>