2 # Copyright (C) 2018 rubenwardy
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.
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.
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/>.
19 from flask_user import *
20 from flask.ext import menu
22 from app.models import *
23 from app.tasks.importtasks import importRepoScreenshot, importAllDependencies
24 from app.tasks.forumtasks import importUsersFromModList, importKrocksModList
25 from flask_wtf import FlaskForm
27 from app.utils import loginUser, rank_required
29 @app.route("/admin/", methods=["GET", "POST"])
30 @rank_required(UserRank.ADMIN)
32 if request.method == "POST":
33 action = request.form["action"]
34 if action == "importusers":
35 task = importUsersFromModList.delay()
36 return redirect(url_for("check_task", id=task.id, r=url_for("user_list_page")))
37 elif action == "importmodlist":
38 task = importKrocksModList.delay()
39 return redirect(url_for("check_task", id=task.id, r=url_for("todo_topics_page")))
40 elif action == "importscreenshots":
41 packages = Package.query \
42 .filter_by(soft_deleted=False) \
43 .outerjoin(PackageScreenshot, Package.id==PackageScreenshot.package_id) \
44 .filter(PackageScreenshot.id==None) \
46 for package in packages:
47 importRepoScreenshot.delay(package.id)
49 return redirect(url_for("admin_page"))
50 elif action == "restore":
51 package = Package.query.get(request.form["package"])
53 flash("Unknown package", "error")
55 package.soft_deleted = False
57 return redirect(url_for("admin_page"))
58 elif action == "importdepends":
59 task = importAllDependencies.delay()
60 return redirect(url_for("check_task", id=task.id, r=url_for("admin_page")))
61 elif action == "modprovides":
62 packages = Package.query.filter_by(type=PackageType.MOD).all()
65 if len(p.provides) == 0:
66 p.provides.append(MetaPackage.GetOrCreate(p.name, mpackage_cache))
69 return redirect(url_for("admin_page"))
72 flash("Unknown action: " + action, "error")
74 deleted_packages = Package.query.filter_by(soft_deleted=True).all()
75 return render_template("admin/list.html", deleted_packages=deleted_packages)
77 class SwitchUserForm(FlaskForm):
78 username = StringField("Username")
79 submit = SubmitField("Switch")
82 @app.route("/admin/switchuser/", methods=["GET", "POST"])
83 @rank_required(UserRank.ADMIN)
84 def switch_user_page():
85 form = SwitchUserForm(formdata=request.form)
86 if request.method == "POST" and form.validate():
87 user = User.query.filter_by(username=form["username"].data).first()
89 flash("Unable to find user", "error")
91 return redirect(url_for("user_profile_page", username=current_user.username))
93 flash("Unable to login as user", "error")
96 # Process GET or invalid POST
97 return render_template("admin/switch_user_page.html", form=form)