]> git.lizzy.rs Git - cheatdb.git/blob - app/views/admin.py
Fix user list order
[cheatdb.git] / app / views / admin.py
1 # Content DB
2 # Copyright (C) 2018  rubenwardy
3 #
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.
8 #
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.
13 #
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/>.
16
17
18 from flask import *
19 from flask_user import *
20 from flask.ext import menu
21 from app import app
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
26 from wtforms import *
27 from app.utils import loginUser, rank_required
28
29 @app.route("/admin/", methods=["GET", "POST"])
30 @rank_required(UserRank.ADMIN)
31 def admin_page():
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)
43
44                         return redirect(url_for("admin_page"))
45                 else:
46                         flash("Unknown action: " + action, "error")
47
48         return render_template("admin/list.html")
49
50 class SwitchUserForm(FlaskForm):
51         username = StringField("Username")
52         submit = SubmitField("Switch")
53
54
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()
61                 if user is None:
62                         flash("Unable to find user", "error")
63                 elif loginUser(user):
64                         return redirect(url_for("user_profile_page", username=current_user.username))
65                 else:
66                         flash("Unable to login as user", "error")
67
68
69         # Process GET or invalid POST
70         return render_template("admin/switch_user_page.html", form=form)