2020-07-12 17:34:25 +02:00
|
|
|
# ContentDB
|
2021-01-30 17:59:42 +01:00
|
|
|
# Copyright (C) 2018-21 rubenwardy
|
2018-07-28 18:26:28 +02: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
|
2018-07-28 18:26:28 +02: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.
|
2018-07-28 18:26:28 +02:00
|
|
|
#
|
2021-01-30 17:59:42 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2018-07-28 18:26:28 +02: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
|
2018-07-28 18:26:28 +02:00
|
|
|
from flask_wtf import FlaskForm
|
2022-01-27 19:21:47 +01:00
|
|
|
from wtforms import StringField, BooleanField, SubmitField, URLField
|
2022-01-26 20:12:42 +01:00
|
|
|
from wtforms.validators import InputRequired, Length, Optional
|
2020-12-04 03:23:04 +01:00
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
from app.utils import rank_required, nonempty_or_none, add_audit_log
|
2020-12-04 03:23:04 +01:00
|
|
|
from . import bp
|
2023-06-19 20:32:36 +02:00
|
|
|
from app.models import UserRank, License, db, AuditSeverity
|
2020-12-04 03:23:04 +01:00
|
|
|
|
2018-07-28 18:26:28 +02:00
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/licenses/")
|
2018-07-28 18:26:28 +02:00
|
|
|
@rank_required(UserRank.MODERATOR)
|
2019-11-16 00:51:42 +01:00
|
|
|
def license_list():
|
2018-07-28 18:26:28 +02:00
|
|
|
return render_template("admin/licenses/list.html", licenses=License.query.order_by(db.asc(License.name)).all())
|
|
|
|
|
2022-01-26 20:12:42 +01:00
|
|
|
|
2018-07-28 18:26:28 +02:00
|
|
|
class LicenseForm(FlaskForm):
|
2022-01-26 20:12:42 +01:00
|
|
|
name = StringField("Name", [InputRequired(), Length(3, 100)])
|
|
|
|
is_foss = BooleanField("Is FOSS")
|
2023-06-19 22:27:49 +02:00
|
|
|
url = URLField("URL", [Optional()], filters=[nonempty_or_none])
|
2022-01-26 20:12:42 +01:00
|
|
|
submit = SubmitField("Save")
|
|
|
|
|
2018-07-28 18:26:28 +02:00
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/licenses/new/", methods=["GET", "POST"])
|
|
|
|
@bp.route("/licenses/<name>/edit/", methods=["GET", "POST"])
|
2018-07-28 18:26:28 +02:00
|
|
|
@rank_required(UserRank.MODERATOR)
|
2019-11-16 00:51:42 +01:00
|
|
|
def create_edit_license(name=None):
|
2018-07-28 18:26:28 +02: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)
|
2018-07-28 18:47:08 +02:00
|
|
|
if request.method == "GET" and license is None:
|
2018-07-28 18:26:28 +02:00
|
|
|
form.is_foss.data = True
|
2020-12-05 00:07:19 +01:00
|
|
|
elif form.validate_on_submit():
|
2018-07-28 18:26:28 +02:00
|
|
|
if license is None:
|
|
|
|
license = License(form.name.data)
|
|
|
|
db.session.add(license)
|
|
|
|
flash("Created license " + form.name.data, "success")
|
2023-05-12 02:03:21 +02:00
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
add_audit_log(AuditSeverity.MODERATION, current_user, f"Created license {license.name}",
|
|
|
|
url_for("admin.license_list"))
|
2018-07-28 18:26:28 +02:00
|
|
|
else:
|
|
|
|
flash("Updated license " + form.name.data, "success")
|
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
add_audit_log(AuditSeverity.MODERATION, current_user, f"Edited license {license.name}",
|
|
|
|
url_for("admin.license_list"))
|
2023-05-12 02:03:21 +02:00
|
|
|
|
2018-07-28 18:26:28 +02:00
|
|
|
form.populate_obj(license)
|
|
|
|
db.session.commit()
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("admin.license_list"))
|
2018-07-28 18:26:28 +02:00
|
|
|
|
|
|
|
return render_template("admin/licenses/edit.html", license=license, form=form)
|