2 # Copyright (C) 2018 rubenwardy
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 from flask_user import *
21 from .auth import is_api_authd
22 from .support import error, handleCreateRelease
24 from app.models import *
25 from app.utils import is_package_page
26 from app.markdown import render_markdown
27 from app.querybuilder import QueryBuilder
29 @bp.route("/api/packages/")
31 qb = QueryBuilder(request.args)
32 query = qb.buildPackageQuery()
33 ver = qb.getMinetestVersion()
35 pkgs = [package.getAsDictionaryShort(current_app.config["BASE_URL"], version=ver) \
36 for package in query.all()]
40 @bp.route("/api/packages/<author>/<name>/")
43 return jsonify(package.getAsDictionary(current_app.config["BASE_URL"]))
46 @bp.route("/api/packages/<author>/<name>/dependencies/")
48 def package_dependencies(package):
51 for dep in package.dependencies:
56 name = dep.package.name
57 fulfilled_by = [ dep.package.getAsDictionaryKey() ]
59 elif dep.meta_package:
60 name = dep.meta_package.name
61 fulfilled_by = [ pkg.getAsDictionaryKey() for pkg in dep.meta_package.packages]
64 raise "Malformed dependency"
68 "is_optional": dep.optional,
69 "packages": fulfilled_by
75 @bp.route("/api/packages/<author>/<name>/releases/")
77 def list_releases(package):
78 releases = package.releases.filter_by(approved=True).all()
79 return jsonify([ rel.getAsDictionary() for rel in releases ])
82 @bp.route("/api/topics/")
84 qb = QueryBuilder(request.args)
85 query = qb.buildTopicQuery(show_added=True)
86 return jsonify([t.getAsDictionary() for t in query.all()])
89 @bp.route("/api/topic_discard/", methods=["POST"])
91 def topic_set_discard():
92 tid = request.args.get("tid")
93 discard = request.args.get("discard")
94 if tid is None or discard is None:
97 topic = ForumTopic.query.get(tid)
98 if not topic.checkPerm(current_user, Permission.TOPIC_DISCARD):
101 topic.discarded = discard == "true"
104 return jsonify(topic.getAsDictionary())
107 @bp.route("/api/minetest_versions/")
109 return jsonify([{ "name": rel.name, "protocol_version": rel.protocol }\
110 for rel in MinetestRelease.query.all() if rel.getActual() is not None])
113 @bp.route("/api/whoami/")
117 return jsonify({ "is_authenticated": False, "username": None })
119 return jsonify({ "is_authenticated": True, "username": token.owner.username })
122 @bp.route("/api/markdown/", methods=["POST"])
125 return render_markdown(request.data.decode("utf-8"))
128 @bp.route("/api/packages/<author>/<name>/releases/new/", methods=["POST"])
132 def create_release(token, package):
135 return error(400, "JSON post data is required")
137 for option in ["method", "title", "ref"]:
138 if json.get(option) is None:
139 return error(400, option + " is required in the POST data")
142 if json["method"].lower() != "git":
143 return error(400, "Release-creation methods other than git are not supported")
145 return handleCreateRelease(token, package, json["title"], json["ref"])