contentdb/app/views/admin.py

82 lines
2.8 KiB
Python
Raw Normal View History

2018-05-17 16:18:20 +02:00
# Content DB
# Copyright (C) 2018 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# 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
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-05-13 16:28:27 +02:00
from flask import *
from flask_user import *
from flask.ext import menu
from app import app
from app.models import *
2018-05-15 21:05:35 +02:00
from app.tasks.importtasks import importRepoScreenshot
from app.tasks.forumtasks import importUsersFromModList
2018-05-13 16:28:27 +02:00
from flask_wtf import FlaskForm
from wtforms import *
2018-05-15 20:35:59 +02:00
from app.utils import loginUser, rank_required
2018-05-13 16:28:27 +02:00
@app.route("/admin/", methods=["GET", "POST"])
2018-05-13 16:28:27 +02:00
@rank_required(UserRank.ADMIN)
def admin_page():
if request.method == "POST":
action = request.form["action"]
if action == "importusers":
task = importUsersFromModList.delay()
return redirect(url_for("check_task", id=task.id, r=url_for("user_list_page")))
2018-05-15 21:05:35 +02:00
elif action == "importscreenshots":
packages = Package.query \
2018-05-25 19:57:30 +02:00
.filter_by(soft_deleted=False) \
2018-05-15 21:05:35 +02:00
.outerjoin(PackageScreenshot, Package.id==PackageScreenshot.package_id) \
2018-05-25 19:28:24 +02:00
.filter(PackageScreenshot.id==None) \
2018-05-25 19:57:30 +02:00
.all()
2018-05-15 21:05:35 +02:00
for package in packages:
importRepoScreenshot.delay(package.id)
return redirect(url_for("admin_page"))
2018-05-25 19:28:24 +02:00
elif action == "restore":
package = Package.query.get(request.form["package"])
if package is None:
flash("Unknown package", "error")
else:
package.soft_deleted = False
db.session.commit()
return redirect(url_for("admin_page"))
else:
flash("Unknown action: " + action, "error")
2018-05-25 19:28:24 +02:00
deleted_packages = Package.query.filter_by(soft_deleted=True).all()
return render_template("admin/list.html", deleted_packages=deleted_packages)
2018-05-13 16:28:27 +02:00
class SwitchUserForm(FlaskForm):
username = StringField("Username")
submit = SubmitField("Switch")
@app.route("/admin/switchuser/", methods=["GET", "POST"])
@rank_required(UserRank.ADMIN)
def switch_user_page():
form = SwitchUserForm(formdata=request.form)
if request.method == "POST" and form.validate():
user = User.query.filter_by(username=form["username"].data).first()
if user is None:
flash("Unable to find user", "error")
elif loginUser(user):
return redirect(url_for("user_profile_page", username=current_user.username))
else:
flash("Unable to login as user", "error")
# Process GET or invalid POST
return render_template("admin/switch_user_page.html", form=form)