Add fmt option to include VCS repo URL

Fixes #514
This commit is contained in:
rubenwardy 2024-06-22 17:14:52 +01:00
parent 232e3199fd
commit 3f12a89764
4 changed files with 14 additions and 6 deletions

@ -74,10 +74,12 @@ def packages():
qb = QueryBuilder(request.args, lang=lang)
query = qb.build_package_query()
if request.args.get("fmt") == "keys":
fmt = request.args.get("fmt")
if fmt == "keys":
return jsonify([pkg.as_key_dict() for pkg in query.all()])
pkgs = qb.convert_to_dictionary(query.all())
include_vcs = fmt == "vcs"
pkgs = qb.convert_to_dictionary(query.all(), include_vcs)
if "engine_version" in request.args or "protocol_version" in request.args:
pkgs = [pkg for pkg in pkgs if pkg.get("release")]
@ -88,7 +90,8 @@ def packages():
"limit" not in request.args:
featured_lut = set()
featured = qb.convert_to_dictionary(query.filter(
Package.collections.any(and_(Collection.name == "featured", Collection.author.has(username="ContentDB")))).all())
Package.collections.any(and_(Collection.name == "featured", Collection.author.has(username="ContentDB")))).all(),
include_vcs)
for pkg in featured:
featured_lut.add(f"{pkg['author']}/{pkg['name']}")
pkg["short_description"] = gettext("Featured") + ". " + pkg["short_description"]

@ -213,6 +213,7 @@ Format query parameters:
* `fmt`: How the response is formatted.
* `keys`: author/name only.
* `short`: stuff needed for the Minetest client.
* `vcs`: `short` but with `repo`.
### Releases

@ -611,7 +611,7 @@ class Package(db.Model):
"type": self.type.to_name(),
}
def as_short_dict(self, base_url, version=None, release_id=None, no_load=False, lang="en"):
def as_short_dict(self, base_url, version=None, release_id=None, no_load=False, lang="en", include_vcs=False):
tnurl = self.get_thumb_url(1, format="png")
if release_id is None and no_load == False:
@ -637,6 +637,9 @@ class Package(db.Model):
if not ret["aliases"]:
del ret["aliases"]
if include_vcs:
ret["repo"] = self.repo
return ret
def as_dict(self, base_url, version=None, lang="en", screenshots_dict=False):

@ -213,14 +213,15 @@ class QueryBuilder:
return releases_query.all()
def convert_to_dictionary(self, packages):
def convert_to_dictionary(self, packages, include_vcs: bool):
releases = {}
for [package_id, release_id] in self.get_releases():
releases[package_id] = release_id
def to_json(package: Package):
release_id = releases.get(package.id)
return package.as_short_dict(current_app.config["BASE_URL"], release_id=release_id, no_load=True, lang=self.lang)
return package.as_short_dict(current_app.config["BASE_URL"], release_id=release_id, no_load=True,
lang=self.lang, include_vcs=include_vcs)
return [to_json(pkg) for pkg in packages]