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 *
|
2018-12-23 00:03:38 +01:00
|
|
|
import flask_menu as menu
|
2019-11-16 00:51:42 +01:00
|
|
|
from . import bp
|
2018-05-13 16:28:27 +02:00
|
|
|
from app.models import *
|
2020-01-19 02:59:00 +01:00
|
|
|
from celery import uuid, group
|
|
|
|
from app.tasks.importtasks import importRepoScreenshot, importAllDependencies, makeVCSRelease, checkZipRelease
|
2018-12-25 20:49:17 +01:00
|
|
|
from app.tasks.forumtasks import importTopicList, checkAllForumAccounts
|
2018-05-13 16:28:27 +02:00
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import *
|
2018-07-08 18:28:39 +02:00
|
|
|
from app.utils import loginUser, rank_required, triggerNotif
|
|
|
|
import datetime
|
2018-05-13 16:28:27 +02:00
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/admin/", methods=["GET", "POST"])
|
2018-05-13 16:28:27 +02:00
|
|
|
@rank_required(UserRank.ADMIN)
|
|
|
|
def admin_page():
|
2018-05-15 16:00:12 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
action = request.form["action"]
|
2019-11-15 00:38:55 +01:00
|
|
|
if action == "delstuckreleases":
|
|
|
|
PackageRelease.query.filter(PackageRelease.task_id != None).delete()
|
|
|
|
db.session.commit()
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("admin.admin_page"))
|
2020-01-19 02:59:00 +01:00
|
|
|
elif action == "checkreleases":
|
|
|
|
releases = PackageRelease.query.filter(PackageRelease.url.like("/uploads/%")).all()
|
|
|
|
|
|
|
|
tasks = []
|
|
|
|
for release in releases:
|
|
|
|
zippath = release.url.replace("/uploads/", app.config["UPLOAD_DIR"])
|
|
|
|
tasks.append(checkZipRelease.s(release.id, zippath))
|
|
|
|
|
|
|
|
result = group(tasks).apply_async()
|
|
|
|
|
|
|
|
while not result.ready():
|
|
|
|
import time
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
return redirect(url_for("todo.view"))
|
2019-11-15 00:38:55 +01:00
|
|
|
elif action == "importmodlist":
|
2018-07-04 01:14:37 +02:00
|
|
|
task = importTopicList.delay()
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("tasks.check", id=task.id, r=url_for("todo.topics")))
|
2018-12-25 20:49:17 +01:00
|
|
|
elif action == "checkusers":
|
|
|
|
task = checkAllForumAccounts.delay()
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("tasks.check", id=task.id, r=url_for("admin.admin_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)
|
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("admin.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()
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("admin.admin_page"))
|
2018-05-27 23:34:24 +02:00
|
|
|
elif action == "importdepends":
|
|
|
|
task = importAllDependencies.delay()
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("tasks.check", id=task.id, r=url_for("admin.admin_page")))
|
2018-05-28 00:13:13 +02:00
|
|
|
elif action == "modprovides":
|
|
|
|
packages = Package.query.filter_by(type=PackageType.MOD).all()
|
|
|
|
mpackage_cache = {}
|
|
|
|
for p in packages:
|
|
|
|
if len(p.provides) == 0:
|
|
|
|
p.provides.append(MetaPackage.GetOrCreate(p.name, mpackage_cache))
|
|
|
|
|
|
|
|
db.session.commit()
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("admin.admin_page"))
|
2018-07-04 02:05:32 +02:00
|
|
|
elif action == "recalcscores":
|
|
|
|
for p in Package.query.all():
|
2019-11-21 23:16:35 +01:00
|
|
|
p.setStartScore()
|
2018-07-04 02:05:32 +02:00
|
|
|
|
|
|
|
db.session.commit()
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("admin.admin_page"))
|
2018-07-08 18:28:39 +02:00
|
|
|
elif action == "vcsrelease":
|
|
|
|
for package in Package.query.filter(Package.repo.isnot(None)).all():
|
|
|
|
if package.releases.count() != 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
rel = PackageRelease()
|
|
|
|
rel.package = package
|
2018-11-14 01:56:28 +01:00
|
|
|
rel.title = datetime.date.today().isoformat()
|
|
|
|
rel.url = ""
|
2018-07-08 18:28:39 +02:00
|
|
|
rel.task_id = uuid()
|
|
|
|
rel.approved = True
|
|
|
|
db.session.add(rel)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
makeVCSRelease.apply_async((rel.id, "master"), task_id=rel.task_id)
|
|
|
|
|
|
|
|
msg = "{}: Release {} created".format(package.title, rel.title)
|
|
|
|
triggerNotif(package.author, current_user, msg, rel.getEditURL())
|
|
|
|
db.session.commit()
|
2018-05-28 00:13:13 +02:00
|
|
|
|
2018-05-15 16:00:12 +02:00
|
|
|
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")
|
|
|
|
|
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/admin/switchuser/", methods=["GET", "POST"])
|
2018-05-13 16:28:27 +02:00
|
|
|
@rank_required(UserRank.ADMIN)
|
2019-11-16 00:51:42 +01:00
|
|
|
def switch_user():
|
2018-05-13 16:28:27 +02:00
|
|
|
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):
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("users.profile", username=current_user.username))
|
2018-05-13 16:28:27 +02:00
|
|
|
else:
|
|
|
|
flash("Unable to login as user", "error")
|
|
|
|
|
|
|
|
|
|
|
|
# Process GET or invalid POST
|
2019-11-16 00:51:42 +01:00
|
|
|
return render_template("admin/switch_user.html", form=form)
|