contentdb/app/blueprints/admin/licenseseditor.py

74 lines
2.6 KiB
Python
Raw Normal View History

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