2020-07-12 17:34:25 +02:00
|
|
|
# ContentDB
|
2021-01-30 17:59:42 +01:00
|
|
|
# Copyright (C) 2018-21 rubenwardy
|
2018-05-17 16:18:20 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2021-01-30 17:59:42 +01:00
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
2018-05-17 16:18:20 +02:00
|
|
|
# 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
|
2021-01-30 17:59:42 +01:00
|
|
|
# GNU Affero General Public License for more details.
|
2018-05-17 16:18:20 +02:00
|
|
|
#
|
2021-01-30 17:59:42 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2018-05-17 16:18:20 +02:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2020-12-04 03:23:04 +01:00
|
|
|
import os
|
|
|
|
|
|
|
|
from celery import group
|
2018-05-13 16:28:27 +02:00
|
|
|
from flask import *
|
2020-12-09 21:38:36 +01:00
|
|
|
from flask_login import current_user, login_user
|
2018-05-13 16:28:27 +02:00
|
|
|
from flask_wtf import FlaskForm
|
2021-07-20 00:02:20 +02:00
|
|
|
from sqlalchemy import or_
|
2018-05-13 16:28:27 +02:00
|
|
|
from wtforms import *
|
2020-12-05 22:41:53 +01:00
|
|
|
from wtforms.validators import InputRequired, Length
|
2020-12-04 03:23:04 +01:00
|
|
|
|
|
|
|
from app.models import *
|
|
|
|
from app.tasks.forumtasks import importTopicList, checkAllForumAccounts
|
2021-02-01 23:42:58 +01:00
|
|
|
from app.tasks.importtasks import importRepoScreenshot, checkZipRelease, check_for_updates
|
2021-07-20 00:02:20 +02:00
|
|
|
from app.utils import rank_required, addAuditLog, addNotification, get_system_user
|
2020-12-04 03:23:04 +01:00
|
|
|
from . import bp
|
|
|
|
|
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"]
|
2020-07-12 04:38:14 +02:00
|
|
|
|
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-07-12 04:38:14 +02:00
|
|
|
|
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()
|
|
|
|
|
2020-07-12 03:22:35 +02:00
|
|
|
while not result.ready():
|
|
|
|
import time
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
2021-01-29 20:38:14 +01:00
|
|
|
return redirect(url_for("todo.view_editor"))
|
2020-07-12 04:38:14 +02:00
|
|
|
|
2020-07-12 03:22:35 +02:00
|
|
|
elif action == "reimportpackages":
|
|
|
|
tasks = []
|
2020-09-16 18:51:03 +02:00
|
|
|
for package in Package.query.filter(Package.state!=PackageState.DELETED).all():
|
2020-07-12 03:22:35 +02:00
|
|
|
release = package.releases.first()
|
|
|
|
if release:
|
|
|
|
zippath = release.url.replace("/uploads/", app.config["UPLOAD_DIR"])
|
2020-12-31 19:19:07 +01:00
|
|
|
tasks.append(checkZipRelease.s(release.id, zippath))
|
2020-07-12 03:22:35 +02:00
|
|
|
|
|
|
|
result = group(tasks).apply_async()
|
|
|
|
|
2020-01-19 02:59:00 +01:00
|
|
|
while not result.ready():
|
2020-07-14 01:28:56 +02:00
|
|
|
import time
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
2021-01-29 20:38:14 +01:00
|
|
|
return redirect(url_for("todo.view_editor"))
|
2020-07-14 01:28:56 +02:00
|
|
|
|
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")))
|
2020-07-12 04:38:14 +02:00
|
|
|
|
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")))
|
2020-07-12 04:38:14 +02:00
|
|
|
|
2018-05-15 21:05:35 +02:00
|
|
|
elif action == "importscreenshots":
|
|
|
|
packages = Package.query \
|
2020-09-16 18:51:03 +02:00
|
|
|
.filter(Package.state!=PackageState.DELETED) \
|
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"))
|
2020-07-12 04:38:14 +02:00
|
|
|
|
2018-05-25 19:28:24 +02:00
|
|
|
elif action == "restore":
|
|
|
|
package = Package.query.get(request.form["package"])
|
|
|
|
if package is None:
|
2020-01-24 19:15:09 +01:00
|
|
|
flash("Unknown package", "danger")
|
2018-05-25 19:28:24 +02:00
|
|
|
else:
|
2020-09-16 18:51:03 +02:00
|
|
|
package.state = PackageState.READY_FOR_REVIEW
|
2018-05-25 19:28:24 +02:00
|
|
|
db.session.commit()
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("admin.admin_page"))
|
2018-05-28 00:13:13 +02:00
|
|
|
|
2018-07-04 02:05:32 +02:00
|
|
|
elif action == "recalcscores":
|
|
|
|
for p in Package.query.all():
|
2020-07-09 05:32:13 +02:00
|
|
|
p.recalcScore()
|
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-05-28 00:13:13 +02:00
|
|
|
|
2020-05-30 17:48:37 +02:00
|
|
|
elif action == "cleanuploads":
|
|
|
|
upload_dir = app.config['UPLOAD_DIR']
|
|
|
|
|
|
|
|
(_, _, filenames) = next(os.walk(upload_dir))
|
|
|
|
existing_uploads = set(filenames)
|
|
|
|
|
|
|
|
if len(existing_uploads) != 0:
|
|
|
|
def getURLsFromDB(column):
|
|
|
|
results = db.session.query(column).filter(column != None, column != "").all()
|
|
|
|
return set([os.path.basename(x[0]) for x in results])
|
|
|
|
|
|
|
|
release_urls = getURLsFromDB(PackageRelease.url)
|
|
|
|
screenshot_urls = getURLsFromDB(PackageScreenshot.url)
|
|
|
|
|
|
|
|
db_urls = release_urls.union(screenshot_urls)
|
|
|
|
unreachable = existing_uploads.difference(db_urls)
|
|
|
|
|
|
|
|
import sys
|
|
|
|
print("On Disk: ", existing_uploads, file=sys.stderr)
|
|
|
|
print("In DB: ", db_urls, file=sys.stderr)
|
|
|
|
print("Unreachable: ", unreachable, file=sys.stderr)
|
|
|
|
|
|
|
|
for filename in unreachable:
|
|
|
|
os.remove(os.path.join(upload_dir, filename))
|
|
|
|
|
|
|
|
flash("Deleted " + str(len(unreachable)) + " unreachable uploads", "success")
|
|
|
|
else:
|
|
|
|
flash("No downloads to create", "danger")
|
|
|
|
|
2020-08-18 15:22:16 +02:00
|
|
|
return redirect(url_for("admin.admin_page"))
|
|
|
|
|
|
|
|
elif action == "delmetapackages":
|
|
|
|
query = MetaPackage.query.filter(~MetaPackage.dependencies.any(), ~MetaPackage.packages.any())
|
|
|
|
count = query.count()
|
|
|
|
query.delete(synchronize_session=False)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
flash("Deleted " + str(count) + " unused meta packages", "success")
|
|
|
|
return redirect(url_for("admin.admin_page"))
|
2020-12-10 12:25:52 +01:00
|
|
|
|
|
|
|
elif action == "delremovedpackages":
|
|
|
|
query = Package.query.filter_by(state=PackageState.DELETED)
|
|
|
|
count = query.count()
|
|
|
|
for pkg in query.all():
|
|
|
|
pkg.review_thread = None
|
|
|
|
db.session.delete(pkg)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
flash("Deleted {} soft deleted packages packages".format(count), "success")
|
|
|
|
return redirect(url_for("admin.admin_page"))
|
|
|
|
|
2020-12-15 23:40:49 +01:00
|
|
|
elif action == "addupdateconfig":
|
2020-12-16 16:08:37 +01:00
|
|
|
added = 0
|
2020-12-15 23:40:49 +01:00
|
|
|
for pkg in Package.query.filter(Package.repo != None, Package.releases.any(), Package.update_config == None).all():
|
|
|
|
pkg.update_config = PackageUpdateConfig()
|
2021-01-30 00:18:37 +01:00
|
|
|
pkg.update_config.auto_created = True
|
2020-12-15 23:40:49 +01:00
|
|
|
|
|
|
|
release: PackageRelease = pkg.releases.first()
|
|
|
|
if release and release.commit_hash:
|
|
|
|
pkg.update_config.last_commit = release.commit_hash
|
|
|
|
|
|
|
|
db.session.add(pkg.update_config)
|
2020-12-16 16:08:37 +01:00
|
|
|
added += 1
|
2020-12-15 23:40:49 +01:00
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-12-16 16:08:37 +01:00
|
|
|
flash("Added {} update configs".format(added), "success")
|
2020-12-15 23:40:49 +01:00
|
|
|
return redirect(url_for("admin.admin_page"))
|
|
|
|
|
|
|
|
elif action == "runupdateconfig":
|
|
|
|
check_for_updates.delay()
|
|
|
|
|
|
|
|
flash("Started update configs", "success")
|
|
|
|
return redirect(url_for("admin.admin_page"))
|
|
|
|
|
2021-07-20 00:02:20 +02:00
|
|
|
elif action == "remindwip":
|
|
|
|
users = User.query.filter(User.packages.any(or_(Package.state==PackageState.WIP, Package.state==PackageState.CHANGES_NEEDED)))
|
|
|
|
system_user = get_system_user()
|
|
|
|
for user in users:
|
|
|
|
packages = db.session.query(Package.title).filter(
|
|
|
|
Package.author_id==user.id,
|
|
|
|
or_(Package.state==PackageState.WIP, Package.state==PackageState.CHANGES_NEEDED)) \
|
|
|
|
.all()
|
|
|
|
|
|
|
|
# Who needs translations?
|
|
|
|
packages = [pkg[0] for pkg in packages]
|
|
|
|
if len(packages) >= 3:
|
|
|
|
packages[len(packages) - 1] = "and " + packages[len(packages) - 1]
|
|
|
|
packages_list = ", ".join(packages)
|
|
|
|
else:
|
|
|
|
packages_list = "and ".join(packages)
|
|
|
|
|
|
|
|
havent = "haven't" if len(packages) > 1 else "hasn't"
|
2021-07-20 00:11:02 +02:00
|
|
|
if len(packages_list) + 54 > 100:
|
|
|
|
packages_list = packages_list[0:(100-54-1)] + "…"
|
2021-07-20 00:02:20 +02:00
|
|
|
|
|
|
|
addNotification(user, system_user, NotificationType.PACKAGE_APPROVAL,
|
2021-07-20 00:11:02 +02:00
|
|
|
f"Did you forget? {packages_list} {havent} been submitted for review yet",
|
2021-07-20 00:02:20 +02:00
|
|
|
url_for('todo.view_user', username=user.username))
|
|
|
|
db.session.commit()
|
|
|
|
|
2018-05-15 16:00:12 +02:00
|
|
|
else:
|
2020-01-24 19:15:09 +01:00
|
|
|
flash("Unknown action: " + action, "danger")
|
2018-05-15 16:00:12 +02:00
|
|
|
|
2020-09-16 18:51:03 +02:00
|
|
|
deleted_packages = Package.query.filter(Package.state==PackageState.DELETED).all()
|
2018-05-25 19:28:24 +02:00
|
|
|
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)
|
2020-12-05 00:07:19 +01:00
|
|
|
if form.validate_on_submit():
|
2018-05-13 16:28:27 +02:00
|
|
|
user = User.query.filter_by(username=form["username"].data).first()
|
|
|
|
if user is None:
|
2020-01-24 19:15:09 +01:00
|
|
|
flash("Unable to find user", "danger")
|
2020-12-09 21:38:36 +01:00
|
|
|
elif login_user(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:
|
2020-01-24 19:15:09 +01:00
|
|
|
flash("Unable to login as user", "danger")
|
2018-05-13 16:28:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Process GET or invalid POST
|
2019-11-16 00:51:42 +01:00
|
|
|
return render_template("admin/switch_user.html", form=form)
|
2020-12-05 22:41:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SendNotificationForm(FlaskForm):
|
|
|
|
title = StringField("Title", [InputRequired(), Length(1, 300)])
|
|
|
|
url = StringField("URL", [InputRequired(), Length(1, 100)], default="/")
|
|
|
|
submit = SubmitField("Send")
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/admin/send-notification/", methods=["GET", "POST"])
|
|
|
|
@rank_required(UserRank.ADMIN)
|
|
|
|
def send_bulk_notification():
|
|
|
|
form = SendNotificationForm(request.form)
|
|
|
|
if form.validate_on_submit():
|
|
|
|
addAuditLog(AuditSeverity.MODERATION, current_user,
|
|
|
|
"Sent bulk notification", None, None, form.title.data)
|
|
|
|
|
|
|
|
users = User.query.filter(User.rank >= UserRank.NEW_MEMBER).all()
|
|
|
|
addNotification(users, current_user, NotificationType.OTHER, form.title.data, form.url.data, None)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return redirect(url_for("admin.admin_page"))
|
|
|
|
|
|
|
|
return render_template("admin/send_bulk_notification.html", form=form)
|