Add sort and order query params to package list

This commit is contained in:
rubenwardy 2018-12-22 21:09:29 +00:00
parent 5f40d68441
commit fd901726b0
2 changed files with 20 additions and 3 deletions

@ -29,14 +29,14 @@ Welcome
{{ render_pkggrid(new) }}
<a href="{{ url_for('packages_page') }}" class="btn btn-secondary float-right">
<a href="{{ url_for('packages_page', sort='score', order='desc') }}" class="btn btn-secondary float-right">
See more
</a>
<h2 class="my-3">Popular</h2>
{{ render_pkggrid(popular) }}
<a href="{{ url_for('packages_page', type='txp') }}" class="btn btn-secondary float-right">
<a href="{{ url_for('packages_page', type='txp', sort='score', order='desc') }}" class="btn btn-secondary float-right">
See more
</a>
<h2 class="my-3">Top Texture Packs</h2>

@ -53,6 +53,8 @@ class QueryBuilder:
self.lucky = "lucky" in request.args
self.hide_nonfree = isNo(request.args.get("nonfree"))
self.limit = 1 if self.lucky else None
self.order_by = request.args.get("sort") or "score"
self.order_dir = request.args.get("order") or "desc"
def buildPackageQuery(self):
query = Package.query.filter_by(soft_deleted=False, approved=True)
@ -63,7 +65,22 @@ class QueryBuilder:
if self.search is not None and self.search.strip() != "":
query = query.filter(Package.title.ilike('%' + self.search + '%'))
query = query.order_by(db.desc(Package.score))
to_order = None
if self.order_by == "score":
to_order = Package.score
elif self.order_by == "created_at":
to_order = Package.created_at
else:
abort(400)
if self.order_dir == "asc":
to_order = db.asc(to_order)
elif self.order_dir == "desc":
to_order = db.desc(to_order)
else:
abort(400)
query = query.order_by(to_order)
if self.hide_nonfree:
query = query.filter(Package.license.has(License.is_foss == True))