]> git.lizzy.rs Git - cheatdb.git/blob - app/views/api.py
Add Minetest version checking to packages API
[cheatdb.git] / app / views / api.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 app import app
21 from app.models import *
22 from app.utils import is_package_page
23 from app.querybuilder import QueryBuilder
24
25 @app.route("/api/packages/")
26 def api_packages_page():
27         qb    = QueryBuilder(request.args)
28         query = qb.buildPackageQuery()
29
30         pkgs = [package.getAsDictionaryShort(app.config["BASE_URL"]) \
31                         for package in query.all() if package.getDownloadRelease() is not None]
32         return jsonify(pkgs)
33
34 @app.route("/api/packages/<author>/<name>/")
35 @is_package_page
36 def api_package_page(package):
37         return jsonify(package.getAsDictionary(app.config["BASE_URL"]))
38
39
40 @app.route("/api/topics/")
41 def api_topics_page():
42         query = ForumTopic.query \
43                         .order_by(db.asc(ForumTopic.wip), db.asc(ForumTopic.name), db.asc(ForumTopic.title))
44         pkgs = [t.getAsDictionary() for t in query.all()]
45         return jsonify(pkgs)
46
47
48 @app.route("/api/topic_discard/", methods=["POST"])
49 @login_required
50 def topic_set_discard():
51         tid = request.args.get("tid")
52         discard = request.args.get("discard")
53         if tid is None or discard is None:
54                 abort(400)
55
56         topic = ForumTopic.query.get(tid)
57         if not topic.checkPerm(current_user, Permission.TOPIC_DISCARD):
58                 abort(403)
59
60         topic.discarded = discard == "true"
61         db.session.commit()
62
63         return jsonify(topic.getAsDictionary())
64
65
66 @app.route("/api/minetest_versions/")
67 def api_minetest_versions_page():
68         return jsonify([{ "name": rel.name, "protocol_version": rel.protocol }\
69                         for rel in MinetestRelease.query.all() if rel.getActual() is not None])