]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/admin/licenseseditor.py
c6fca02d4052260d89c08f7b5e9d67ada1e0446a
[cheatdb.git] / app / blueprints / admin / licenseseditor.py
1 # Content DB
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("/licenses/")
28 @rank_required(UserRank.MODERATOR)
29 def license_list():
30         return render_template("admin/licenses/list.html", licenses=License.query.order_by(db.asc(License.name)).all())
31
32 class LicenseForm(FlaskForm):
33         name     = StringField("Name", [InputRequired(), Length(3,100)])
34         is_foss  = BooleanField("Is FOSS")
35         submit   = SubmitField("Save")
36
37 @bp.route("/licenses/new/", methods=["GET", "POST"])
38 @bp.route("/licenses/<name>/edit/", methods=["GET", "POST"])
39 @rank_required(UserRank.MODERATOR)
40 def create_edit_license(name=None):
41         license = None
42         if name is not None:
43                 license = License.query.filter_by(name=name).first()
44                 if license is None:
45                         abort(404)
46
47         form = LicenseForm(formdata=request.form, obj=license)
48         if request.method == "GET" and license is None:
49                 form.is_foss.data = True
50         elif request.method == "POST" and form.validate():
51                 if license is None:
52                         license = License(form.name.data)
53                         db.session.add(license)
54                         flash("Created license " + form.name.data, "success")
55                 else:
56                         flash("Updated license " + form.name.data, "success")
57
58                 form.populate_obj(license)
59                 db.session.commit()
60                 return redirect(url_for("admin.license_list"))
61
62         return render_template("admin/licenses/edit.html", license=license, form=form)