]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/admin/warningseditor.py
Add Content Warnings
[cheatdb.git] / app / blueprints / admin / warningseditor.py
1 # ContentDB
2 # Copyright (C) 2020  rubenwardy
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17
18 from flask import *
19 from flask_user import *
20 from . import bp
21 from app.models import *
22 from flask_wtf import FlaskForm
23 from wtforms import *
24 from wtforms.validators import *
25 from app.utils import rank_required
26
27 @bp.route("/admin/warnings/")
28 @rank_required(UserRank.ADMIN)
29 def warning_list():
30         return render_template("admin/warnings/list.html", warnings=ContentWarning.query.order_by(db.asc(ContentWarning.title)).all())
31
32 class WarningForm(FlaskForm):
33         title       = StringField("Title", [InputRequired(), Length(3,100)])
34         name        = StringField("Name", [Optional(), Length(1, 20), Regexp("^[a-z0-9_]", 0, "Lower case letters (a-z), digits (0-9), and underscores (_) only")])
35         description = TextAreaField("Description", [InputRequired(), Length(0, 500)])
36         submit      = SubmitField("Save")
37
38 @bp.route("/admin/warnings/new/", methods=["GET", "POST"])
39 @bp.route("/admin/warnings/<name>/edit/", methods=["GET", "POST"])
40 @rank_required(UserRank.ADMIN)
41 def create_edit_warning(name=None):
42         warning = None
43         if name is not None:
44                 warning = ContentWarning.query.filter_by(name=name).first()
45                 if warning is None:
46                         abort(404)
47
48         form = WarningForm(formdata=request.form, obj=warning)
49         if request.method == "POST" and form.validate():
50                 if warning is None:
51                         warning = ContentWarning(form.title.data, form.description.data)
52                         db.session.add(warning)
53                 else:
54                         form.populate_obj(warning)
55                 db.session.commit()
56
57                 return redirect(url_for("admin.warning_list"))
58
59         return render_template("admin/warnings/edit.html", warning=warning, form=form)