contentdb/app/blueprints/admin/versioneditor.py

71 lines
2.5 KiB
Python
Raw Normal View History

2020-07-12 17:34:25 +02:00
# ContentDB
2021-01-30 17:59:42 +01:00
# Copyright (C) 2018-21 rubenwardy
2019-01-29 00:49:27 +01:00
#
# This program is free software: you can redistribute it and/or modify
2021-01-30 17:59:42 +01:00
# it under the terms of the GNU Affero General Public License as published by
2019-01-29 00:49:27 +01:00
# 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
2021-01-30 17:59:42 +01:00
# GNU Affero General Public License for more details.
2019-01-29 00:49:27 +01:00
#
2021-01-30 17:59:42 +01:00
# You should have received a copy of the GNU Affero General Public License
2019-01-29 00:49:27 +01:00
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2022-01-26 20:12:42 +01:00
from flask import redirect, render_template, abort, url_for, request, flash
2023-05-12 02:03:21 +02:00
from flask_login import current_user
2019-01-29 00:49:27 +01:00
from flask_wtf import FlaskForm
2022-01-26 20:12:42 +01:00
from wtforms import StringField, IntegerField, SubmitField
from wtforms.validators import InputRequired, Length
2020-12-04 03:23:04 +01:00
2023-05-12 02:03:21 +02:00
from app.utils import rank_required, addAuditLog
2020-12-04 03:23:04 +01:00
from . import bp
2023-05-12 02:03:21 +02:00
from ...models import UserRank, MinetestRelease, db, AuditSeverity
2020-12-04 03:23:04 +01:00
2019-01-29 00:49:27 +01:00
@bp.route("/versions/")
2019-01-29 00:49:27 +01:00
@rank_required(UserRank.MODERATOR)
def version_list():
2019-01-29 00:49:27 +01:00
return render_template("admin/versions/list.html", versions=MinetestRelease.query.order_by(db.asc(MinetestRelease.id)).all())
2022-01-26 20:12:42 +01:00
2019-01-29 00:49:27 +01:00
class VersionForm(FlaskForm):
2022-01-26 20:12:42 +01:00
name = StringField("Name", [InputRequired(), Length(3, 100)])
2019-01-29 00:49:27 +01:00
protocol = IntegerField("Protocol")
2022-01-26 20:12:42 +01:00
submit = SubmitField("Save")
2019-01-29 00:49:27 +01:00
@bp.route("/versions/new/", methods=["GET", "POST"])
@bp.route("/versions/<name>/edit/", methods=["GET", "POST"])
2019-01-29 00:49:27 +01:00
@rank_required(UserRank.MODERATOR)
def create_edit_version(name=None):
2019-01-29 00:49:27 +01:00
version = None
if name is not None:
version = MinetestRelease.query.filter_by(name=name).first()
if version is None:
abort(404)
form = VersionForm(formdata=request.form, obj=version)
2020-12-05 00:07:19 +01:00
if form.validate_on_submit():
2019-01-29 00:49:27 +01:00
if version is None:
version = MinetestRelease(form.name.data)
db.session.add(version)
flash("Created version " + form.name.data, "success")
2023-05-12 02:03:21 +02:00
addAuditLog(AuditSeverity.MODERATION, current_user, f"Created version {version.name}",
url_for("admin.license_list"))
2019-01-29 00:49:27 +01:00
else:
flash("Updated version " + form.name.data, "success")
2023-05-12 02:03:21 +02:00
addAuditLog(AuditSeverity.MODERATION, current_user, f"Edited version {version.name}",
url_for("admin.version_list"))
2019-01-29 00:49:27 +01:00
form.populate_obj(version)
db.session.commit()
return redirect(url_for("admin.version_list"))
2019-01-29 00:49:27 +01:00
return render_template("admin/versions/edit.html", version=version, form=form)