]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/api/endpoints.py
Rename 'vcs' release-creation mode to 'git'
[cheatdb.git] / app / blueprints / api / endpoints.py
1 # Content DB
2 # Copyright (C) 2018  rubenwardy
3 #
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.
8 #
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.
13 #
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/>.
16
17
18 from flask import *
19 from flask_user import *
20 from . import bp
21 from .auth import is_api_authd
22 from .support import error, handleCreateRelease
23 from app import csrf
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
28
29 @bp.route("/api/packages/")
30 def packages():
31         qb    = QueryBuilder(request.args)
32         query = qb.buildPackageQuery()
33         ver   = qb.getMinetestVersion()
34
35         pkgs = [package.getAsDictionaryShort(current_app.config["BASE_URL"], version=ver) \
36                         for package in query.all()]
37         return jsonify(pkgs)
38
39
40 @bp.route("/api/packages/<author>/<name>/")
41 @is_package_page
42 def package(package):
43         return jsonify(package.getAsDictionary(current_app.config["BASE_URL"]))
44
45
46 @bp.route("/api/packages/<author>/<name>/dependencies/")
47 @is_package_page
48 def package_dependencies(package):
49         ret = []
50
51         for dep in package.dependencies:
52                 name = None
53                 fulfilled_by = None
54
55                 if dep.package:
56                         name = dep.package.name
57                         fulfilled_by = [ dep.package.getAsDictionaryKey() ]
58
59                 elif dep.meta_package:
60                         name = dep.meta_package.name
61                         fulfilled_by = [ pkg.getAsDictionaryKey() for pkg in dep.meta_package.packages]
62
63                 else:
64                         raise "Malformed dependency"
65
66                 ret.append({
67                         "name": name,
68                         "is_optional": dep.optional,
69                         "packages": fulfilled_by
70                 })
71
72         return jsonify(ret)
73
74
75 @bp.route("/api/packages/<author>/<name>/releases/")
76 @is_package_page
77 def list_releases(package):
78         releases = package.releases.filter_by(approved=True).all()
79         return jsonify([ rel.getAsDictionary() for rel in releases ])
80
81
82 @bp.route("/api/topics/")
83 def topics():
84         qb     = QueryBuilder(request.args)
85         query  = qb.buildTopicQuery(show_added=True)
86         return jsonify([t.getAsDictionary() for t in query.all()])
87
88
89 @bp.route("/api/topic_discard/", methods=["POST"])
90 @login_required
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:
95                 abort(400)
96
97         topic = ForumTopic.query.get(tid)
98         if not topic.checkPerm(current_user, Permission.TOPIC_DISCARD):
99                 abort(403)
100
101         topic.discarded = discard == "true"
102         db.session.commit()
103
104         return jsonify(topic.getAsDictionary())
105
106
107 @bp.route("/api/minetest_versions/")
108 def versions():
109         return jsonify([{ "name": rel.name, "protocol_version": rel.protocol }\
110                         for rel in MinetestRelease.query.all() if rel.getActual() is not None])
111
112
113 @bp.route("/api/whoami/")
114 @is_api_authd
115 def whoami(token):
116         if token is None:
117                 return jsonify({ "is_authenticated": False, "username": None })
118         else:
119                 return jsonify({ "is_authenticated": True, "username": token.owner.username })
120
121
122 @bp.route("/api/markdown/", methods=["POST"])
123 @csrf.exempt
124 def markdown():
125         return render_markdown(request.data.decode("utf-8"))
126
127
128 @bp.route("/api/packages/<author>/<name>/releases/new/", methods=["POST"])
129 @csrf.exempt
130 @is_package_page
131 @is_api_authd
132 def create_release(token, package):
133         json = request.json
134         if json is None:
135                 return error(400, "JSON post data is required")
136
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")
140
141
142         if json["method"].lower() != "git":
143                 return error(400, "Release-creation methods other than git are not supported")
144
145         return handleCreateRelease(token, package, json["title"], json["ref"])