2020-07-12 17:34:25 +02:00
|
|
|
# ContentDB
|
2021-01-30 17:59:42 +01:00
|
|
|
# Copyright (C) 2018-21 rubenwardy
|
2018-05-17 16:18:20 +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-05-17 16:18:20 +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-05-17 16:18:20 +02:00
|
|
|
#
|
2021-01-30 17:59:42 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2018-05-17 16:18:20 +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, url_for, request, flash
|
2020-12-09 21:38:36 +01:00
|
|
|
from flask_login import current_user, login_user
|
2018-05-13 16:28:27 +02:00
|
|
|
from flask_wtf import FlaskForm
|
2022-01-26 20:12:42 +01:00
|
|
|
from wtforms import StringField, SubmitField
|
2020-12-05 22:41:53 +01:00
|
|
|
from wtforms.validators import InputRequired, Length
|
2021-07-20 00:02:20 +02:00
|
|
|
from app.utils import rank_required, addAuditLog, addNotification, get_system_user
|
2020-12-04 03:23:04 +01:00
|
|
|
from . import bp
|
2021-07-30 20:42:11 +02:00
|
|
|
from .actions import actions
|
|
|
|
from ...models import UserRank, Package, db, PackageState, User, AuditSeverity, NotificationType
|
2020-12-04 03:23:04 +01:00
|
|
|
|
2018-05-13 16:28:27 +02:00
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/admin/", methods=["GET", "POST"])
|
2018-05-13 16:28:27 +02:00
|
|
|
@rank_required(UserRank.ADMIN)
|
|
|
|
def admin_page():
|
2018-05-15 16:00:12 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
action = request.form["action"]
|
2020-07-12 04:38:14 +02:00
|
|
|
|
2021-07-30 20:42:11 +02:00
|
|
|
if action == "restore":
|
2018-05-25 19:28:24 +02:00
|
|
|
package = Package.query.get(request.form["package"])
|
|
|
|
if package is None:
|
2020-01-24 19:15:09 +01:00
|
|
|
flash("Unknown package", "danger")
|
2018-05-25 19:28:24 +02:00
|
|
|
else:
|
2020-09-16 18:51:03 +02:00
|
|
|
package.state = PackageState.READY_FOR_REVIEW
|
2018-05-25 19:28:24 +02:00
|
|
|
db.session.commit()
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("admin.admin_page"))
|
2018-05-28 00:13:13 +02:00
|
|
|
|
2021-07-30 20:42:11 +02:00
|
|
|
elif action in actions:
|
|
|
|
ret = actions[action]["func"]()
|
|
|
|
if ret:
|
|
|
|
return ret
|
2021-07-20 00:02:20 +02:00
|
|
|
|
2018-05-15 16:00:12 +02:00
|
|
|
else:
|
2020-01-24 19:15:09 +01:00
|
|
|
flash("Unknown action: " + action, "danger")
|
2018-05-15 16:00:12 +02:00
|
|
|
|
2022-01-26 20:12:42 +01:00
|
|
|
deleted_packages = Package.query.filter(Package.state == PackageState.DELETED).all()
|
2021-07-30 20:42:11 +02:00
|
|
|
return render_template("admin/list.html", deleted_packages=deleted_packages, actions=actions)
|
2018-05-13 16:28:27 +02:00
|
|
|
|
2022-01-26 20:12:42 +01:00
|
|
|
|
2018-05-13 16:28:27 +02:00
|
|
|
class SwitchUserForm(FlaskForm):
|
|
|
|
username = StringField("Username")
|
|
|
|
submit = SubmitField("Switch")
|
|
|
|
|
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/admin/switchuser/", methods=["GET", "POST"])
|
2018-05-13 16:28:27 +02:00
|
|
|
@rank_required(UserRank.ADMIN)
|
2019-11-16 00:51:42 +01:00
|
|
|
def switch_user():
|
2018-05-13 16:28:27 +02:00
|
|
|
form = SwitchUserForm(formdata=request.form)
|
2020-12-05 00:07:19 +01:00
|
|
|
if form.validate_on_submit():
|
2018-05-13 16:28:27 +02:00
|
|
|
user = User.query.filter_by(username=form["username"].data).first()
|
|
|
|
if user is None:
|
2020-01-24 19:15:09 +01:00
|
|
|
flash("Unable to find user", "danger")
|
2020-12-09 21:38:36 +01:00
|
|
|
elif login_user(user):
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("users.profile", username=current_user.username))
|
2018-05-13 16:28:27 +02:00
|
|
|
else:
|
2020-01-24 19:15:09 +01:00
|
|
|
flash("Unable to login as user", "danger")
|
2018-05-13 16:28:27 +02:00
|
|
|
|
|
|
|
# Process GET or invalid POST
|
2019-11-16 00:51:42 +01:00
|
|
|
return render_template("admin/switch_user.html", form=form)
|
2020-12-05 22:41:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SendNotificationForm(FlaskForm):
|
2022-01-26 20:12:42 +01:00
|
|
|
title = StringField("Title", [InputRequired(), Length(1, 300)])
|
|
|
|
url = StringField("URL", [InputRequired(), Length(1, 100)], default="/")
|
2020-12-05 22:41:53 +01:00
|
|
|
submit = SubmitField("Send")
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/admin/send-notification/", methods=["GET", "POST"])
|
|
|
|
@rank_required(UserRank.ADMIN)
|
|
|
|
def send_bulk_notification():
|
|
|
|
form = SendNotificationForm(request.form)
|
|
|
|
if form.validate_on_submit():
|
|
|
|
addAuditLog(AuditSeverity.MODERATION, current_user,
|
2022-01-26 20:12:42 +01:00
|
|
|
"Sent bulk notification", url_for("admin.admin_page"), None, form.title.data)
|
2020-12-05 22:41:53 +01:00
|
|
|
|
|
|
|
users = User.query.filter(User.rank >= UserRank.NEW_MEMBER).all()
|
2021-07-30 20:42:11 +02:00
|
|
|
addNotification(users, get_system_user(), NotificationType.OTHER, form.title.data, form.url.data, None)
|
2020-12-05 22:41:53 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return redirect(url_for("admin.admin_page"))
|
|
|
|
|
|
|
|
return render_template("admin/send_bulk_notification.html", form=form)
|
2021-12-21 00:52:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/admin/restore/", methods=["GET", "POST"])
|
|
|
|
@rank_required(UserRank.EDITOR)
|
|
|
|
def restore():
|
|
|
|
if request.method == "POST":
|
|
|
|
target = request.form["submit"]
|
|
|
|
if "Review" in target:
|
|
|
|
target = PackageState.READY_FOR_REVIEW
|
|
|
|
elif "Changes" in target:
|
|
|
|
target = PackageState.CHANGES_NEEDED
|
|
|
|
else:
|
|
|
|
target = PackageState.WIP
|
|
|
|
|
|
|
|
package = Package.query.get(request.form["package"])
|
|
|
|
if package is None:
|
|
|
|
flash("Unknown package", "danger")
|
|
|
|
else:
|
|
|
|
package.state = target
|
|
|
|
|
|
|
|
addAuditLog(AuditSeverity.EDITOR, current_user, f"Restored package to state {target.value}",
|
|
|
|
package.getURL("packages.view"), package)
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(package.getURL("packages.view"))
|
|
|
|
|
2022-01-26 20:12:42 +01:00
|
|
|
deleted_packages = Package.query \
|
|
|
|
.filter(Package.state == PackageState.DELETED) \
|
|
|
|
.join(Package.author) \
|
|
|
|
.order_by(db.asc(User.username), db.asc(Package.name)) \
|
|
|
|
.all()
|
|
|
|
|
|
|
|
return render_template("admin/restore.html", deleted_packages=deleted_packages)
|