contentdb/app/blueprints/admin/licenseseditor.py

68 lines
2.3 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
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
2018-07-28 18:26:28 +02:00
from flask_wtf import FlaskForm
2022-01-26 20:12:42 +01:00
from wtforms import StringField, BooleanField, SubmitField
2021-07-31 22:03:45 +02:00
from wtforms.fields.html5 import URLField
2022-01-26 20:12:42 +01:00
from wtforms.validators import InputRequired, Length, Optional
2020-12-04 03:23:04 +01:00
2021-07-31 22:03:45 +02:00
from app.utils import rank_required, nonEmptyOrNone
2020-12-04 03:23:04 +01:00
from . import bp
2022-01-26 20:12:42 +01:00
from ...models import UserRank, License, db
2020-12-04 03:23:04 +01:00
2018-07-28 18:26:28 +02:00
@bp.route("/licenses/")
2018-07-28 18:26:28 +02:00
@rank_required(UserRank.MODERATOR)
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")
url = URLField("URL", [Optional], filters=[nonEmptyOrNone])
submit = SubmitField("Save")
2018-07-28 18:26:28 +02: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)
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)
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")
else:
flash("Updated license " + form.name.data, "success")
form.populate_obj(license)
db.session.commit()
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)