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
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 .outerjoin(PackageScreenshot, Package.id==PackageScreenshot.package_id) \
40 .filter(PackageScreenshot.id==None).all()
41 for package in packages:
42 importRepoScreenshot.delay(package.id)
44 return redirect(url_for("admin_page"))
46 flash("Unknown action: " + action, "error")
48 return render_template("admin/list.html")
50 class SwitchUserForm(FlaskForm):
51 username = StringField("Username")
52 submit = SubmitField("Switch")
55 @app.route("/admin/switchuser/", methods=["GET", "POST"])
56 @rank_required(UserRank.ADMIN)
57 def switch_user_page():
58 form = SwitchUserForm(formdata=request.form)
59 if request.method == "POST" and form.validate():
60 user = User.query.filter_by(username=form["username"].data).first()
62 flash("Unable to find user", "error")
64 return redirect(url_for("user_profile_page", username=current_user.username))
66 flash("Unable to login as user", "error")
69 # Process GET or invalid POST
70 return render_template("admin/switch_user_page.html", form=form)