Fix release being null in API when release is unapproved

Fixes #129
This commit is contained in:
rubenwardy 2019-03-13 14:37:20 +00:00
parent 9c2c8c21f1
commit 4c109d6bd3
3 changed files with 26 additions and 18 deletions

@ -417,21 +417,23 @@ class Package(db.Model):
for e in PackagePropertyKey:
setattr(self, e.name, getattr(package, e.name))
def getAsDictionaryShort(self, base_url, protonum=None):
def getAsDictionaryShort(self, base_url, version=None, protonum=None):
tnurl = self.getThumbnailURL(1)
release = self.getDownloadRelease(version=version, protonum=protonum)
return {
"name": self.name,
"title": self.title,
"author": self.author.display_name,
"short_description": self.short_desc,
"type": self.type.toName(),
"release": self.getDownloadRelease(protonum).id if self.getDownloadRelease(protonum) is not None else None,
"release": release and release.id,
"thumbnail": (base_url + tnurl) if tnurl is not None else None,
"score": round(self.score * 10) / 10
}
def getAsDictionary(self, base_url, protonum=None):
def getAsDictionary(self, base_url, version=None, protonum=None):
tnurl = self.getThumbnailURL(1)
release = self.getDownloadRelease(version=version, protonum=protonum)
return {
"author": self.author.display_name,
"name": self.name,
@ -454,7 +456,7 @@ class Package(db.Model):
"screenshots": [base_url + ss.url for ss in self.screenshots],
"url": base_url + self.getDownloadURL(),
"release": self.getDownloadRelease(protonum).id if self.getDownloadRelease(protonum) is not None else None,
"release": release and release.id,
"score": round(self.score * 10) / 10
}
@ -503,9 +505,8 @@ class Package(db.Model):
return url_for("package_download_page",
author=self.author.username, name=self.name)
def getDownloadRelease(self, protonum=None):
version = None
if protonum is not None:
def getDownloadRelease(self, version=None, protonum=None):
if version is None and protonum is not None:
version = MinetestRelease.query.filter(MinetestRelease.protocol >= int(protonum)).first()
if version is not None:
version = version.id
@ -514,7 +515,7 @@ class Package(db.Model):
for rel in self.releases:
if rel.approved and (protonum is None or
if rel.approved and (version is None or
((rel.min_rel is None or rel.min_rel_id <= version) and \
(rel.max_rel is None or rel.max_rel_id >= version))):
return rel

@ -35,6 +35,17 @@ class QueryBuilder:
if self.search is not None and self.search.strip() == "":
self.search = None
def getMinetestVersion(self):
if not self.protocol_version:
return None
self.protocol_version = int(self.protocol_version)
version = MinetestRelease.query.filter(MinetestRelease.protocol>=self.protocol_version).first()
if version is not None:
return version.id
else:
return 10000000
def buildPackageQuery(self):
query = Package.query.filter_by(soft_deleted=False, approved=True)
@ -69,14 +80,9 @@ class QueryBuilder:
query = query.filter(Package.media_license.has(License.is_foss == True))
if self.protocol_version:
self.protocol_version = int(self.protocol_version)
version = MinetestRelease.query.filter(MinetestRelease.protocol>=self.protocol_version).first()
if version is not None:
version = version.id
else:
version = 10000000
version = self.getMinetestVersion()
query = query.join(Package.releases) \
.filter(PackageRelease.approved==True) \
.filter(or_(PackageRelease.min_rel_id==None, PackageRelease.min_rel_id<=version)) \
.filter(or_(PackageRelease.max_rel_id==None, PackageRelease.max_rel_id>=version))

@ -26,9 +26,10 @@ from app.querybuilder import QueryBuilder
def api_packages_page():
qb = QueryBuilder(request.args)
query = qb.buildPackageQuery()
ver = qb.getMinetestVersion()
pkgs = [package.getAsDictionaryShort(app.config["BASE_URL"], request.args.get("protocol_version")) \
for package in query.all() if package.getDownloadRelease() is not None]
pkgs = [package.getAsDictionaryShort(app.config["BASE_URL"], version=ver) \
for package in query.all()]
return jsonify(pkgs)
@app.route("/api/packages/<author>/<name>/")