2020-07-17 21:48:51 +02:00
|
|
|
# ContentDB
|
|
|
|
# Copyright (C) 2020 rubenwardy
|
|
|
|
#
|
|
|
|
# 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
|
2020-07-17 21:48:51 +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.
|
2020-07-17 21:48:51 +02:00
|
|
|
#
|
2021-01-30 17:59:42 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2020-07-17 21:48:51 +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
|
2020-07-17 21:48:51 +02:00
|
|
|
from flask_wtf import FlaskForm
|
2022-01-26 20:12:42 +01:00
|
|
|
from wtforms import StringField, TextAreaField, SubmitField
|
|
|
|
from wtforms.validators import InputRequired, Length, Optional, Regexp
|
2020-12-04 03:23:04 +01:00
|
|
|
|
2020-07-17 21:48:51 +02:00
|
|
|
from app.utils import rank_required
|
2020-12-04 03:23:04 +01:00
|
|
|
from . import bp
|
2022-01-26 20:12:42 +01:00
|
|
|
from ...models import UserRank, ContentWarning, db
|
2020-12-04 03:23:04 +01:00
|
|
|
|
2020-07-17 21:48:51 +02:00
|
|
|
|
|
|
|
@bp.route("/admin/warnings/")
|
|
|
|
@rank_required(UserRank.ADMIN)
|
|
|
|
def warning_list():
|
|
|
|
return render_template("admin/warnings/list.html", warnings=ContentWarning.query.order_by(db.asc(ContentWarning.title)).all())
|
|
|
|
|
2022-01-26 20:12:42 +01:00
|
|
|
|
2020-07-17 21:48:51 +02:00
|
|
|
class WarningForm(FlaskForm):
|
2022-01-26 20:12:42 +01:00
|
|
|
title = StringField("Title", [InputRequired(), Length(3, 100)])
|
2020-07-17 23:29:02 +02:00
|
|
|
description = TextAreaField("Description", [Optional(), Length(0, 500)])
|
2022-01-26 20:12:42 +01:00
|
|
|
name = StringField("Name", [Optional(), Length(1, 20),
|
|
|
|
Regexp("^[a-z0-9_]", 0, "Lower case letters (a-z), digits (0-9), and underscores (_) only")])
|
|
|
|
submit = SubmitField("Save")
|
|
|
|
|
2020-07-17 21:48:51 +02:00
|
|
|
|
|
|
|
@bp.route("/admin/warnings/new/", methods=["GET", "POST"])
|
|
|
|
@bp.route("/admin/warnings/<name>/edit/", methods=["GET", "POST"])
|
|
|
|
@rank_required(UserRank.ADMIN)
|
|
|
|
def create_edit_warning(name=None):
|
|
|
|
warning = None
|
|
|
|
if name is not None:
|
|
|
|
warning = ContentWarning.query.filter_by(name=name).first()
|
|
|
|
if warning is None:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
form = WarningForm(formdata=request.form, obj=warning)
|
2020-12-05 00:07:19 +01:00
|
|
|
if form.validate_on_submit():
|
2020-07-17 21:48:51 +02:00
|
|
|
if warning is None:
|
|
|
|
warning = ContentWarning(form.title.data, form.description.data)
|
|
|
|
db.session.add(warning)
|
|
|
|
else:
|
|
|
|
form.populate_obj(warning)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return redirect(url_for("admin.warning_list"))
|
|
|
|
|
|
|
|
return render_template("admin/warnings/edit.html", warning=warning, form=form)
|