]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/admin/tagseditor.py
Add descriptions to tags, and show in multiselect
[cheatdb.git] / app / blueprints / admin / tagseditor.py
1 # ContentDB
2 # Copyright (C) 2018  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("/tags/")
28 @login_required
29 def tag_list():
30         if not Permission.EDIT_TAGS.check(current_user):
31                 abort(403)
32
33         return render_template("admin/tags/list.html", tags=Tag.query.order_by(db.asc(Tag.title)).all())
34
35 class TagForm(FlaskForm):
36         title       = StringField("Title", [InputRequired(), Length(3,100)])
37         description = TextAreaField("Description", [InputRequired(), Length(0, 500)])
38         name        = StringField("Name", [Optional(), Length(1, 20), Regexp("^[a-z0-9_]", 0, "Lower case letters (a-z), digits (0-9), and underscores (_) only")])
39         submit      = SubmitField("Save")
40
41 @bp.route("/tags/new/", methods=["GET", "POST"])
42 @bp.route("/tags/<name>/edit/", methods=["GET", "POST"])
43 @login_required
44 def create_edit_tag(name=None):
45         tag = None
46         if name is not None:
47                 tag = Tag.query.filter_by(name=name).first()
48                 if tag is None:
49                         abort(404)
50
51         if not Permission.checkPerm(current_user, Permission.EDIT_TAGS if tag else Permission.CREATE_TAG):
52                 abort(403)
53
54         form = TagForm(formdata=request.form, obj=tag)
55         if request.method == "POST" and form.validate():
56                 if tag is None:
57                         tag = Tag(form.title.data)
58                         tag.description = form.description.data
59                         db.session.add(tag)
60                 else:
61                         form.populate_obj(tag)
62                 db.session.commit()
63
64                 if Permission.EDIT_TAGS.check(current_user):
65                         return redirect(url_for("admin.create_edit_tag", name=tag.name))
66                 else:
67                         return redirect(url_for("homepage.home"))
68
69         return render_template("admin/tags/edit.html", tag=tag, form=form)