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
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 == "importscreenshots":
38 packages = Package.query \
39 .filter_by(soft_deleted=False) \
40 .outerjoin(PackageScreenshot, Package.id==PackageScreenshot.package_id) \
41 .filter(PackageScreenshot.id==None) \
43 for package in packages:
44 importRepoScreenshot.delay(package.id)
46 return redirect(url_for("admin_page"))
47 elif action == "restore":
48 package = Package.query.get(request.form["package"])
50 flash("Unknown package", "error")
52 package.soft_deleted = False
54 return redirect(url_for("admin_page"))
55 elif action == "importdepends":
56 task = importAllDependencies.delay()
57 return redirect(url_for("check_task", id=task.id, r=url_for("admin_page")))
58 elif action == "modprovides":
59 packages = Package.query.filter_by(type=PackageType.MOD).all()
62 if len(p.provides) == 0:
63 p.provides.append(MetaPackage.GetOrCreate(p.name, mpackage_cache))
66 return redirect(url_for("admin_page"))
69 flash("Unknown action: " + action, "error")
71 deleted_packages = Package.query.filter_by(soft_deleted=True).all()
72 return render_template("admin/list.html", deleted_packages=deleted_packages)
74 class SwitchUserForm(FlaskForm):
75 username = StringField("Username")
76 submit = SubmitField("Switch")
79 @app.route("/admin/switchuser/", methods=["GET", "POST"])
80 @rank_required(UserRank.ADMIN)
81 def switch_user_page():
82 form = SwitchUserForm(formdata=request.form)
83 if request.method == "POST" and form.validate():
84 user = User.query.filter_by(username=form["username"].data).first()
86 flash("Unable to find user", "error")
88 return redirect(url_for("user_profile_page", username=current_user.username))
90 flash("Unable to login as user", "error")
93 # Process GET or invalid POST
94 return render_template("admin/switch_user_page.html", form=form)