Add option to hide non-free packages in API

This commit is contained in:
rubenwardy 2018-10-03 17:06:16 +01:00
parent 2229b32c90
commit 328d05bdf6
No known key found for this signature in database
GPG Key ID: A1E29D52FF81513C
2 changed files with 19 additions and 0 deletions

@ -170,3 +170,13 @@ def clearNotifications(url):
if current_user.is_authenticated:
Notification.query.filter_by(user=current_user, url=url).delete()
db.session.commit()
YESES = ["yes", "true", "1", "on"]
def isYes(val):
return val and val.lower() in YESES
def isNo(val):
return val and not isYes(val)

@ -51,6 +51,7 @@ class QueryBuilder:
self.types = types
self.search = request.args.get("q")
self.lucky = "lucky" in request.args
self.hide_nonfree = isNo(request.args.get("nonfree"))
self.limit = 1 if self.lucky else None
def buildPackageQuery(self):
@ -64,6 +65,10 @@ class QueryBuilder:
query = query.order_by(db.desc(Package.score))
if self.hide_nonfree:
query = query.filter(Package.license.has(License.is_foss == True))
query = query.filter(Package.media_license.has(License.is_foss == True))
if self.limit:
query = query.limit(self.limit)
@ -78,6 +83,10 @@ class QueryBuilder:
if len(self.types) > 0:
topics = topics.filter(ForumTopic.type.in_(self.types))
if self.hide_nonfree:
query = query.filter(Package.license.has(License.is_foss == True))
query = query.filter(Package.media_license.has(License.is_foss == True))
if self.limit:
topics = topics.limit(self.limit)