From 328d05bdf647effac1cdb444e36a0debb5d93a02 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 3 Oct 2018 17:06:16 +0100 Subject: [PATCH] Add option to hide non-free packages in API --- app/utils.py | 10 ++++++++++ app/views/packages/__init__.py | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/app/utils.py b/app/utils.py index dc657367..cb6a315a 100644 --- a/app/utils.py +++ b/app/utils.py @@ -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) diff --git a/app/views/packages/__init__.py b/app/views/packages/__init__.py index 7181b5bf..8ac5a83d 100644 --- a/app/views/packages/__init__.py +++ b/app/views/packages/__init__.py @@ -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)