contentdb/app/blueprints/api/support.py

121 lines
4.0 KiB
Python
Raw Normal View History

# ContentDB
# Copyright (C) 2018-21 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Optional
2021-02-02 22:35:29 +01:00
from flask import jsonify, abort, make_response, url_for, current_app
from app.logic.packages import do_edit_package
from app.logic.releases import LogicError, do_create_vcs_release, do_create_zip_release
2022-02-02 02:08:01 +01:00
from app.logic.screenshots import do_create_screenshot, do_order_screenshots, do_set_cover_image
2021-02-02 18:09:23 +01:00
from app.models import APIToken, Package, MinetestRelease, PackageScreenshot
2021-01-30 18:10:38 +01:00
2020-01-24 21:21:40 +01:00
def error(code: int, msg: str):
abort(make_response(jsonify({ "success": False, "error": msg }), code))
2020-01-24 21:21:40 +01:00
2023-06-19 20:32:36 +02:00
# Catches LogicErrors and aborts with JSON error
2021-02-02 18:29:03 +01:00
def guard(f):
def ret(*args, **kwargs):
try:
return f(*args, **kwargs)
except LogicError as e:
error(e.code, e.message)
return ret
2020-01-24 21:21:40 +01:00
def api_create_vcs_release(token: APIToken, package: Package, name: str, title: Optional[str], release_notes: Optional[str], ref: str,
min_v: MinetestRelease = None, max_v: MinetestRelease = None, reason="API"):
2023-06-19 20:32:36 +02:00
if not token.can_operate_on_package(package):
error(403, "API token does not have access to the package")
2020-01-24 21:21:40 +01:00
2021-02-02 18:29:03 +01:00
reason += ", token=" + token.name
rel = guard(do_create_vcs_release)(token.owner, package, name, title, release_notes, ref, min_v, max_v, reason)
2020-01-24 21:21:40 +01:00
return jsonify({
"success": True,
"task": url_for("tasks.check", id=rel.task_id),
"release": rel.as_dict()
})
2021-01-30 18:10:38 +01:00
def api_create_zip_release(token: APIToken, package: Package, name: str, title: Optional[str], release_notes: Optional[str], file,
2023-06-19 20:32:36 +02:00
min_v: MinetestRelease = None, max_v: MinetestRelease = None, reason="API", commit_hash: str = None):
if not token.can_operate_on_package(package):
error(403, "API token does not have access to the package")
2020-01-24 21:21:40 +01:00
2021-02-02 18:29:03 +01:00
reason += ", token=" + token.name
rel = guard(do_create_zip_release)(token.owner, package, name, title, release_notes, file, min_v, max_v, reason, commit_hash)
2020-01-24 21:21:40 +01:00
return jsonify({
"success": True,
"task": url_for("tasks.check", id=rel.task_id),
"release": rel.as_dict()
2020-01-24 21:21:40 +01:00
})
2021-02-02 18:09:23 +01:00
2022-02-02 02:29:14 +01:00
def api_create_screenshot(token: APIToken, package: Package, title: str, file, is_cover_image: bool, reason="API"):
2023-06-19 20:32:36 +02:00
if not token.can_operate_on_package(package):
2021-02-02 18:09:23 +01:00
error(403, "API token does not have access to the package")
2021-02-02 18:29:03 +01:00
reason += ", token=" + token.name
2022-02-02 02:29:14 +01:00
ss : PackageScreenshot = guard(do_create_screenshot)(token.owner, package, title, file, is_cover_image, reason)
2021-02-02 18:09:23 +01:00
return jsonify({
"success": True,
"screenshot": ss.as_dict()
2021-02-02 18:09:23 +01:00
})
2021-02-02 18:09:25 +01:00
def api_order_screenshots(token: APIToken, package: Package, order: [any]):
2023-06-19 20:32:36 +02:00
if not token.can_operate_on_package(package):
2021-02-02 18:09:25 +01:00
error(403, "API token does not have access to the package")
2021-02-02 18:29:03 +01:00
guard(do_order_screenshots)(token.owner, package, order)
2021-02-02 18:09:25 +01:00
return jsonify({
"success": True
})
2021-02-02 22:35:29 +01:00
2022-02-02 02:08:01 +01:00
def api_set_cover_image(token: APIToken, package: Package, cover_image):
2023-06-19 20:32:36 +02:00
if not token.can_operate_on_package(package):
2022-02-02 02:08:01 +01:00
error(403, "API token does not have access to the package")
guard(do_set_cover_image)(token.owner, package, cover_image)
return jsonify({
"success": True
})
2021-02-02 22:35:29 +01:00
def api_edit_package(token: APIToken, package: Package, data: dict, reason: str = "API"):
2023-06-19 20:32:36 +02:00
if not token.can_operate_on_package(package):
2021-02-02 22:35:29 +01:00
error(403, "API token does not have access to the package")
reason += ", token=" + token.name
package = guard(do_edit_package)(token.owner, package, False, False, data, reason)
2021-02-02 22:35:29 +01:00
return jsonify({
"success": True,
"package": package.as_dict(current_app.config["BASE_URL"])
2021-02-02 22:35:29 +01:00
})