2020-07-12 17:34:25 +02:00
|
|
|
# ContentDB
|
2021-01-30 17:59:42 +01:00
|
|
|
# Copyright (C) 2018-21 rubenwardy
|
2019-01-08 18:17:36 +01: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
|
2019-01-08 18:17:36 +01: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.
|
2019-01-08 18:17:36 +01:00
|
|
|
#
|
2021-01-30 17:59:42 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2019-01-08 18:17:36 +01:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2023-06-18 23:21:37 +02:00
|
|
|
|
2023-06-19 20:32:36 +02:00
|
|
|
import datetime
|
|
|
|
import typing
|
2020-12-04 03:23:04 +01:00
|
|
|
from urllib.parse import quote as urlescape
|
2019-11-16 00:51:42 +01:00
|
|
|
|
2022-06-25 03:27:51 +02:00
|
|
|
from celery import uuid
|
2023-06-19 20:32:36 +02:00
|
|
|
from flask import render_template, make_response, request, redirect, flash, url_for, abort
|
|
|
|
from flask_babel import gettext, lazy_gettext
|
|
|
|
from flask_login import login_required, current_user
|
2023-06-18 23:21:37 +02:00
|
|
|
from flask_wtf import FlaskForm
|
2023-04-23 22:20:45 +02:00
|
|
|
from jinja2.utils import markupsafe
|
2023-06-19 20:32:36 +02:00
|
|
|
from sqlalchemy import func, or_, and_
|
2020-04-21 21:35:05 +02:00
|
|
|
from sqlalchemy.orm import joinedload, subqueryload
|
2023-06-19 20:32:36 +02:00
|
|
|
from wtforms import SelectField, StringField, TextAreaField, IntegerField, SubmitField, BooleanField
|
|
|
|
from wtforms.validators import InputRequired, Length, Regexp, DataRequired, Optional, URL, NumberRange, ValidationError
|
2023-06-18 23:21:37 +02:00
|
|
|
from wtforms_sqlalchemy.fields import QuerySelectField, QuerySelectMultipleField
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2023-06-18 23:21:37 +02:00
|
|
|
from app.logic.LogicError import LogicError
|
|
|
|
from app.logic.packages import do_edit_package
|
2020-12-04 03:23:04 +01:00
|
|
|
from app.querybuilder import QueryBuilder
|
2024-01-22 19:09:26 +01:00
|
|
|
from app.rediscache import has_key, set_temp_key
|
2024-03-27 20:03:48 +01:00
|
|
|
from app.tasks.importtasks import import_repo_screenshot, check_zip_release, remove_package_game_support, \
|
|
|
|
update_package_game_support
|
2024-07-04 22:29:56 +02:00
|
|
|
from app.tasks.pkgtasks import check_package_on_submit
|
2023-06-18 23:21:37 +02:00
|
|
|
from app.tasks.webhooktasks import post_discord_webhook
|
2023-06-19 20:32:36 +02:00
|
|
|
|
2021-05-04 04:15:26 +02:00
|
|
|
from . import bp, get_package_tabs
|
2023-06-19 20:32:36 +02:00
|
|
|
from app.models import Package, Tag, db, User, Tags, PackageState, Permission, PackageType, MetaPackage, ForumTopic, \
|
|
|
|
Dependency, Thread, UserRank, PackageReview, PackageDevState, ContentWarning, License, AuditSeverity, \
|
|
|
|
PackageScreenshot, NotificationType, AuditLogEntry, PackageAlias, PackageProvides, PackageGameSupport, \
|
2023-08-14 22:48:50 +02:00
|
|
|
PackageDailyStats, Collection
|
2023-06-19 22:27:49 +02:00
|
|
|
from app.utils import is_user_bot, get_int_or_abort, is_package_page, abs_url_for, add_audit_log, get_package_by_info, \
|
2024-06-22 14:22:37 +02:00
|
|
|
add_notification, get_system_user, rank_required, get_games_from_csv, get_daterange_options, \
|
|
|
|
post_to_approval_thread, normalize_line_endings
|
2024-03-23 17:17:26 +01:00
|
|
|
from app.logic.package_approval import validate_package_for_approval, can_move_to_state
|
2024-03-27 20:03:48 +01:00
|
|
|
from app.logic.game_support import game_support_set
|
2020-07-12 03:22:35 +02:00
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/packages/")
|
|
|
|
def list_all():
|
2023-12-30 19:07:00 +01:00
|
|
|
qb = QueryBuilder(request.args, cookies=True)
|
2023-06-19 22:27:49 +02:00
|
|
|
query = qb.build_package_query()
|
2019-01-08 18:17:36 +01:00
|
|
|
title = qb.title
|
|
|
|
|
2020-12-04 03:23:04 +01:00
|
|
|
query = query.options(
|
|
|
|
joinedload(Package.license),
|
|
|
|
joinedload(Package.media_license),
|
2020-04-21 21:35:05 +02:00
|
|
|
subqueryload(Package.tags))
|
|
|
|
|
2020-07-16 14:52:18 +02:00
|
|
|
ip = request.headers.get("X-Forwarded-For") or request.remote_addr
|
2020-07-16 15:35:12 +02:00
|
|
|
if ip is not None and not is_user_bot():
|
2020-07-16 14:52:18 +02:00
|
|
|
edited = False
|
|
|
|
for tag in qb.tags:
|
|
|
|
edited = True
|
|
|
|
key = "tag/{}/{}".format(ip, tag.name)
|
|
|
|
if not has_key(key):
|
2024-01-22 19:09:26 +01:00
|
|
|
set_temp_key(key, "true")
|
2020-07-16 14:52:18 +02:00
|
|
|
Tag.query.filter_by(id=tag.id).update({
|
|
|
|
"views": Tag.views + 1
|
|
|
|
})
|
|
|
|
|
|
|
|
if edited:
|
|
|
|
db.session.commit()
|
2020-07-15 20:06:00 +02:00
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
if qb.lucky:
|
|
|
|
package = query.first()
|
|
|
|
if package:
|
2023-06-18 22:56:19 +02:00
|
|
|
return redirect(package.get_url("packages.view"))
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
topic = qb.build_topic_query().first()
|
2019-01-08 18:17:36 +01:00
|
|
|
if qb.search and topic:
|
2024-06-22 11:45:14 +02:00
|
|
|
return redirect(topic.url)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2019-11-17 22:40:32 +01:00
|
|
|
page = get_int_or_abort(request.args.get("page"), 1)
|
|
|
|
num = min(40, get_int_or_abort(request.args.get("n"), 100))
|
2023-04-23 22:49:53 +02:00
|
|
|
query = query.paginate(page=page, per_page=num)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
search = request.args.get("q")
|
|
|
|
type_name = request.args.get("type")
|
|
|
|
|
2020-03-28 19:09:20 +01:00
|
|
|
authors = []
|
|
|
|
if search:
|
|
|
|
authors = User.query \
|
|
|
|
.filter(or_(*[func.lower(User.username) == name.lower().strip() for name in search.split(" ")])) \
|
|
|
|
.all()
|
|
|
|
|
2020-03-28 19:15:15 +01:00
|
|
|
authors = [(author.username, search.lower().replace(author.username.lower(), "")) for author in authors]
|
2020-03-28 19:09:20 +01:00
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
topics = None
|
|
|
|
if qb.search and not query.has_next:
|
2023-06-19 22:27:49 +02:00
|
|
|
topics = qb.build_topic_query().all()
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2020-07-18 04:14:56 +02:00
|
|
|
tags_query = db.session.query(func.count(Tags.c.tag_id), Tag) \
|
2023-05-13 16:34:20 +02:00
|
|
|
.select_from(Tag).join(Tags).join(Package).filter(Package.state==PackageState.APPROVED) \
|
|
|
|
.group_by(Tag.id).order_by(db.asc(Tag.title))
|
2023-06-19 22:27:49 +02:00
|
|
|
tags = qb.filter_package_query(tags_query).all()
|
2020-07-12 21:10:19 +02:00
|
|
|
|
|
|
|
selected_tags = set(qb.tags)
|
|
|
|
|
2020-12-04 03:23:04 +01:00
|
|
|
return render_template("packages/list.html",
|
2024-06-08 11:46:47 +02:00
|
|
|
query_hint=qb.query_hint, packages=query.items, pagination=query,
|
2020-12-04 03:23:04 +01:00
|
|
|
query=search, tags=tags, selected_tags=selected_tags, type=type_name,
|
2023-04-24 01:17:26 +02:00
|
|
|
authors=authors, packages_count=query.total, topics=topics, noindex=qb.noindex)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
def get_releases(package):
|
2023-06-18 22:56:19 +02:00
|
|
|
if package.check_perm(current_user, Permission.MAKE_RELEASE):
|
2019-03-29 22:01:19 +01:00
|
|
|
return package.releases.limit(5)
|
2019-01-08 18:17:36 +01:00
|
|
|
else:
|
2019-03-29 22:01:19 +01:00
|
|
|
return package.releases.filter_by(approved=True).limit(5)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
|
2023-07-15 20:08:36 +02:00
|
|
|
@bp.route("/packages/<author>/")
|
|
|
|
def user_redirect(author):
|
|
|
|
return redirect(url_for("users.profile", username=author))
|
|
|
|
|
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/packages/<author>/<name>/")
|
2019-01-08 18:17:36 +01:00
|
|
|
@is_package_page
|
2019-11-16 00:51:42 +01:00
|
|
|
def view(package):
|
2023-08-16 02:00:51 +02:00
|
|
|
if not package.check_perm(current_user, Permission.VIEW_PACKAGE):
|
2023-01-02 18:20:08 +01:00
|
|
|
return render_template("packages/gone.html", package=package), 403
|
2023-01-01 21:39:20 +01:00
|
|
|
|
2021-07-25 04:54:08 +02:00
|
|
|
packages_uses = None
|
|
|
|
if package.type == PackageType.MOD:
|
|
|
|
packages_uses = Package.query.filter(
|
|
|
|
Package.type == PackageType.MOD,
|
|
|
|
Package.id != package.id,
|
2022-01-21 23:49:12 +01:00
|
|
|
Package.state == PackageState.APPROVED,
|
2021-07-25 04:54:08 +02:00
|
|
|
Package.dependencies.any(
|
|
|
|
Dependency.meta_package_id.in_([p.id for p in package.provides]))) \
|
|
|
|
.order_by(db.desc(Package.score)).limit(6).all()
|
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
releases = get_releases(package)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
review_thread = package.review_thread
|
2023-06-18 22:56:19 +02:00
|
|
|
if review_thread is not None and not review_thread.check_perm(current_user, Permission.SEE_THREAD):
|
2019-01-08 18:17:36 +01:00
|
|
|
review_thread = None
|
|
|
|
|
2020-07-10 20:05:35 +02:00
|
|
|
threads = Thread.query.filter_by(package_id=package.id, review_id=None)
|
2019-01-08 18:17:36 +01:00
|
|
|
if not current_user.is_authenticated:
|
|
|
|
threads = threads.filter_by(private=False)
|
2023-06-19 22:27:49 +02:00
|
|
|
elif not current_user.rank.at_least(UserRank.APPROVER) and not current_user == package.author:
|
2019-01-08 18:17:36 +01:00
|
|
|
threads = threads.filter(or_(Thread.private == False, Thread.author == current_user))
|
|
|
|
|
2023-06-19 20:32:36 +02:00
|
|
|
has_review = current_user.is_authenticated and \
|
|
|
|
PackageReview.query.filter_by(package=package, author=current_user).count() > 0
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2024-03-23 17:17:26 +01:00
|
|
|
validation = None
|
|
|
|
if package.state != PackageState.APPROVED:
|
|
|
|
validation = validate_package_for_approval(package)
|
|
|
|
|
2024-03-30 18:27:08 +01:00
|
|
|
favorites_count = Collection.query.filter(
|
|
|
|
Collection.packages.contains(package),
|
|
|
|
Collection.name == "favorites").count()
|
|
|
|
|
2024-03-30 18:37:43 +01:00
|
|
|
public_collection_count = Collection.query.filter(
|
|
|
|
Collection.packages.contains(package),
|
|
|
|
Collection.private == False).count()
|
|
|
|
|
2023-08-14 22:48:50 +02:00
|
|
|
is_favorited = current_user.is_authenticated and \
|
|
|
|
Collection.query.filter(
|
|
|
|
Collection.author == current_user,
|
|
|
|
Collection.packages.contains(package),
|
|
|
|
Collection.name == "favorites").count() > 0
|
|
|
|
|
2020-12-04 03:23:04 +01:00
|
|
|
return render_template("packages/view.html",
|
2021-07-25 04:54:08 +02:00
|
|
|
package=package, releases=releases, packages_uses=packages_uses,
|
2024-03-23 17:17:26 +01:00
|
|
|
review_thread=review_thread, threads=threads.all(), validation=validation,
|
2024-03-30 18:37:43 +01:00
|
|
|
has_review=has_review, favorites_count=favorites_count, is_favorited=is_favorited,
|
|
|
|
public_collection_count=public_collection_count)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
|
2020-08-02 18:41:06 +02:00
|
|
|
@bp.route("/packages/<author>/<name>/shields/<type>/")
|
|
|
|
@is_package_page
|
|
|
|
def shield(package, type):
|
|
|
|
if type == "title":
|
2021-06-24 00:57:26 +02:00
|
|
|
url = "https://img.shields.io/static/v1?label=ContentDB&message={}&color={}" \
|
2020-08-02 18:41:06 +02:00
|
|
|
.format(urlescape(package.title), urlescape("#375a7f"))
|
|
|
|
elif type == "downloads":
|
2023-06-19 20:32:36 +02:00
|
|
|
api_url = abs_url_for("api.package_view", author=package.author.username, name=package.name)
|
2020-08-02 18:41:06 +02:00
|
|
|
url = "https://img.shields.io/badge/dynamic/json?color={}&label=ContentDB&query=downloads&suffix=+downloads&url={}" \
|
|
|
|
.format(urlescape("#375a7f"), urlescape(api_url))
|
|
|
|
else:
|
2023-06-19 20:32:36 +02:00
|
|
|
from flask import abort
|
2020-08-02 18:41:06 +02:00
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return redirect(url)
|
|
|
|
|
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/packages/<author>/<name>/download/")
|
2019-01-08 18:17:36 +01:00
|
|
|
@is_package_page
|
2019-11-16 00:51:42 +01:00
|
|
|
def download(package):
|
2023-06-18 22:56:19 +02:00
|
|
|
release = package.get_download_release()
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
if release is None:
|
|
|
|
if "application/zip" in request.accept_mimetypes and \
|
2023-06-19 20:32:36 +02:00
|
|
|
"text/html" not in request.accept_mimetypes:
|
2019-01-08 18:17:36 +01:00
|
|
|
return "", 204
|
|
|
|
else:
|
2022-01-07 22:46:16 +01:00
|
|
|
flash(gettext("No download available."), "danger")
|
2023-06-18 22:56:19 +02:00
|
|
|
return redirect(package.get_url("packages.view"))
|
2019-01-08 18:17:36 +01:00
|
|
|
else:
|
2023-06-18 22:56:19 +02:00
|
|
|
return redirect(release.get_download_url())
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
|
2024-06-07 06:28:57 +02:00
|
|
|
def make_label(obj: Tag | ContentWarning):
|
|
|
|
translated = obj.get_translated()
|
|
|
|
if translated["description"]:
|
|
|
|
return "{}: {}".format(translated["title"], translated["description"])
|
2020-07-17 23:08:34 +02:00
|
|
|
else:
|
2024-06-07 06:28:57 +02:00
|
|
|
return translated["title"]
|
2020-07-17 23:08:34 +02:00
|
|
|
|
2021-12-20 22:07:12 +01:00
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
class PackageForm(FlaskForm):
|
2022-01-07 22:18:34 +01:00
|
|
|
type = SelectField(lazy_gettext("Type"), [InputRequired()], choices=PackageType.choices(), coerce=PackageType.coerce, default=PackageType.MOD)
|
|
|
|
title = StringField(lazy_gettext("Title (Human-readable)"), [InputRequired(), Length(1, 100)])
|
|
|
|
name = StringField(lazy_gettext("Name (Technical)"), [InputRequired(), Length(1, 100), Regexp("^[a-z0-9_]+$", 0, lazy_gettext("Lower case letters (a-z), digits (0-9), and underscores (_) only"))])
|
|
|
|
short_desc = StringField(lazy_gettext("Short Description (Plaintext)"), [InputRequired(), Length(1,200)])
|
2021-02-02 23:47:46 +01:00
|
|
|
|
2022-01-07 22:18:34 +01:00
|
|
|
dev_state = SelectField(lazy_gettext("Maintenance State"), [InputRequired()], choices=PackageDevState.choices(with_none=True), coerce=PackageDevState.coerce)
|
2021-12-20 22:07:12 +01:00
|
|
|
|
2024-06-07 06:28:57 +02:00
|
|
|
tags = QuerySelectMultipleField(lazy_gettext('Tags'), query_factory=lambda: Tag.query.order_by(db.asc(Tag.name)), get_pk=lambda a: a.id, get_label=make_label)
|
|
|
|
content_warnings = QuerySelectMultipleField(lazy_gettext('Content Warnings'), query_factory=lambda: ContentWarning.query.order_by(db.asc(ContentWarning.name)), get_pk=lambda a: a.id, get_label=make_label)
|
2022-01-07 22:18:34 +01:00
|
|
|
license = QuerySelectField(lazy_gettext("License"), [DataRequired()], allow_blank=True, query_factory=lambda: License.query.order_by(db.asc(License.name)), get_pk=lambda a: a.id, get_label=lambda a: a.name)
|
|
|
|
media_license = QuerySelectField(lazy_gettext("Media License"), [DataRequired()], allow_blank=True, query_factory=lambda: License.query.order_by(db.asc(License.name)), get_pk=lambda a: a.id, get_label=lambda a: a.name)
|
2021-02-02 23:47:46 +01:00
|
|
|
|
2024-06-22 14:22:37 +02:00
|
|
|
desc = TextAreaField(lazy_gettext("Long Description (Markdown)"), [Optional(), Length(0,10000)], filters=[normalize_line_endings])
|
2021-02-02 23:47:46 +01:00
|
|
|
|
2022-01-07 22:18:34 +01:00
|
|
|
repo = StringField(lazy_gettext("VCS Repository URL"), [Optional(), URL()], filters = [lambda x: x or None])
|
|
|
|
website = StringField(lazy_gettext("Website URL"), [Optional(), URL()], filters = [lambda x: x or None])
|
|
|
|
issueTracker = StringField(lazy_gettext("Issue Tracker URL"), [Optional(), URL()], filters = [lambda x: x or None])
|
2023-06-19 20:32:36 +02:00
|
|
|
forums = IntegerField(lazy_gettext("Forum Topic ID"), [Optional(), NumberRange(0, 999999)])
|
2023-03-05 19:13:07 +01:00
|
|
|
video_url = StringField(lazy_gettext("Video URL"), [Optional(), URL()], filters=[lambda x: x or None])
|
|
|
|
donate_url = StringField(lazy_gettext("Donate URL"), [Optional(), URL()], filters=[lambda x: x or None])
|
2024-02-25 19:05:29 +01:00
|
|
|
translation_url = StringField(lazy_gettext("Translation URL"), [Optional(), URL()], filters=[lambda x: x or None])
|
2021-02-02 23:47:46 +01:00
|
|
|
|
2022-01-07 22:18:34 +01:00
|
|
|
submit = SubmitField(lazy_gettext("Save"))
|
2020-07-17 21:48:51 +02:00
|
|
|
|
2023-06-18 23:21:37 +02:00
|
|
|
def validate_name(self, field):
|
2022-06-25 04:44:25 +02:00
|
|
|
if field.data == "_game":
|
|
|
|
raise ValidationError(lazy_gettext("_game is not an allowed name"))
|
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2022-06-01 18:29:56 +02:00
|
|
|
def handle_create_edit(package: typing.Optional[Package], form: PackageForm, author: User):
|
|
|
|
wasNew = False
|
|
|
|
if package is None:
|
2023-08-16 02:03:54 +02:00
|
|
|
package = Package.query.filter_by(name=form.name.data, author_id=author.id).first()
|
2022-06-01 18:29:56 +02:00
|
|
|
if package is not None:
|
|
|
|
if package.state == PackageState.DELETED:
|
2022-11-23 20:13:03 +01:00
|
|
|
flash(
|
|
|
|
gettext("Package already exists, but is removed. Please contact ContentDB staff to restore the package"),
|
|
|
|
"danger")
|
2024-08-26 01:17:19 +02:00
|
|
|
return redirect(url_for("report.report", url=package.get_url("packages.view")))
|
2022-06-01 18:29:56 +02:00
|
|
|
else:
|
2023-04-23 22:20:45 +02:00
|
|
|
flash(markupsafe.Markup(
|
2023-08-22 20:58:43 +02:00
|
|
|
f"<a class='btn btn-sm btn-danger float-end' href='{package.get_url('packages.view')}'>View</a>" +
|
2022-06-01 18:29:56 +02:00
|
|
|
gettext("Package already exists")), "danger")
|
2022-11-23 20:13:03 +01:00
|
|
|
return None
|
2022-06-01 18:29:56 +02:00
|
|
|
|
2023-08-16 02:03:54 +02:00
|
|
|
if Collection.query \
|
|
|
|
.filter(Collection.name == form.name.data, Collection.author == author) \
|
|
|
|
.count() > 0:
|
|
|
|
flash(gettext("A collection with a similar name already exists"), "danger")
|
|
|
|
return
|
|
|
|
|
2022-06-01 18:29:56 +02:00
|
|
|
package = Package()
|
2023-04-23 22:49:53 +02:00
|
|
|
db.session.add(package)
|
2022-06-01 18:29:56 +02:00
|
|
|
package.author = author
|
|
|
|
package.maintainers.append(author)
|
|
|
|
wasNew = True
|
|
|
|
|
|
|
|
try:
|
|
|
|
do_edit_package(current_user, package, wasNew, True, {
|
|
|
|
"type": form.type.data,
|
|
|
|
"title": form.title.data,
|
|
|
|
"name": form.name.data,
|
|
|
|
"short_desc": form.short_desc.data,
|
|
|
|
"dev_state": form.dev_state.data,
|
|
|
|
"tags": form.tags.raw_data,
|
|
|
|
"content_warnings": form.content_warnings.raw_data,
|
|
|
|
"license": form.license.data,
|
|
|
|
"media_license": form.media_license.data,
|
|
|
|
"desc": form.desc.data,
|
|
|
|
"repo": form.repo.data,
|
|
|
|
"website": form.website.data,
|
|
|
|
"issueTracker": form.issueTracker.data,
|
|
|
|
"forums": form.forums.data,
|
|
|
|
"video_url": form.video_url.data,
|
2023-03-05 19:13:07 +01:00
|
|
|
"donate_url": form.donate_url.data,
|
2024-02-25 19:05:29 +01:00
|
|
|
"translation_url": form.translation_url.data,
|
2022-06-01 18:29:56 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if wasNew:
|
|
|
|
msg = f"Created package {author.username}/{form.name.data}"
|
2023-06-19 22:27:49 +02:00
|
|
|
add_audit_log(AuditSeverity.NORMAL, current_user, msg, package.get_url("packages.view"), package)
|
2022-06-01 18:29:56 +02:00
|
|
|
|
|
|
|
if wasNew and package.repo is not None:
|
2023-06-19 22:27:49 +02:00
|
|
|
import_repo_screenshot.delay(package.id)
|
2022-06-01 18:29:56 +02:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
next_url = package.get_url("packages.view")
|
2022-06-01 18:29:56 +02:00
|
|
|
if wasNew and ("WTFPL" in package.license.name or "WTFPL" in package.media_license.name):
|
|
|
|
next_url = url_for("flatpage", path="help/wtfpl", r=next_url)
|
|
|
|
elif wasNew:
|
2023-06-18 22:56:19 +02:00
|
|
|
next_url = package.get_url("packages.setup_releases")
|
2022-06-01 18:29:56 +02:00
|
|
|
|
|
|
|
return redirect(next_url)
|
|
|
|
except LogicError as e:
|
|
|
|
flash(e.message, "danger")
|
|
|
|
|
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/packages/new/", methods=["GET", "POST"])
|
|
|
|
@bp.route("/packages/<author>/<name>/edit/", methods=["GET", "POST"])
|
2019-01-08 18:17:36 +01:00
|
|
|
@login_required
|
2019-11-16 00:51:42 +01:00
|
|
|
def create_edit(author=None, name=None):
|
2024-03-05 18:53:47 +01:00
|
|
|
if current_user.email is None:
|
|
|
|
flash(gettext("You must add an email address to your account and confirm it before you can manage packages"), "danger")
|
|
|
|
return redirect(url_for("users.email_notifications"))
|
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
package = None
|
|
|
|
if author is None:
|
|
|
|
form = PackageForm(formdata=request.form)
|
2024-03-27 18:47:15 +01:00
|
|
|
form.submit.label.text = lazy_gettext("Save draft")
|
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
author = request.args.get("author")
|
|
|
|
if author is None or author == current_user.username:
|
|
|
|
author = current_user
|
|
|
|
else:
|
|
|
|
author = User.query.filter_by(username=author).first()
|
|
|
|
if author is None:
|
2022-01-07 22:46:16 +01:00
|
|
|
flash(gettext("Unable to find that user"), "danger")
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("packages.create_edit"))
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
if not author.check_perm(current_user, Permission.CHANGE_AUTHOR):
|
2022-01-07 22:46:16 +01:00
|
|
|
flash(gettext("Permission denied"), "danger")
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("packages.create_edit"))
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
else:
|
2023-06-19 22:27:49 +02:00
|
|
|
package = get_package_by_info(author, name)
|
2020-08-02 19:03:44 +02:00
|
|
|
if package is None:
|
|
|
|
abort(404)
|
2023-06-18 22:56:19 +02:00
|
|
|
if not package.check_perm(current_user, Permission.EDIT_PACKAGE):
|
2024-03-27 18:47:15 +01:00
|
|
|
abort(403)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
author = package.author
|
|
|
|
|
|
|
|
form = PackageForm(formdata=request.form, obj=package)
|
|
|
|
|
|
|
|
# Initial form class from post data and default data
|
|
|
|
if request.method == "GET":
|
|
|
|
if package is None:
|
2022-01-27 20:20:45 +01:00
|
|
|
form.name.data = request.args.get("bname")
|
|
|
|
form.title.data = request.args.get("title")
|
|
|
|
form.repo.data = request.args.get("repo")
|
2019-01-08 18:17:36 +01:00
|
|
|
form.forums.data = request.args.get("forums")
|
2020-02-23 21:40:14 +01:00
|
|
|
form.license.data = None
|
|
|
|
form.media_license.data = None
|
2019-01-08 18:17:36 +01:00
|
|
|
else:
|
2022-01-27 20:20:45 +01:00
|
|
|
form.tags.data = package.tags
|
|
|
|
form.content_warnings.data = package.content_warnings
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2020-09-22 22:53:04 +02:00
|
|
|
if request.method == "POST" and form.type.data == PackageType.TXP:
|
|
|
|
form.license.data = form.media_license.data
|
|
|
|
|
2020-12-05 00:07:19 +01:00
|
|
|
if form.validate_on_submit():
|
2022-06-01 18:29:56 +02:00
|
|
|
ret = handle_create_edit(package, form, author)
|
|
|
|
if ret:
|
|
|
|
return ret
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2020-09-16 18:51:03 +02:00
|
|
|
package_query = Package.query.filter_by(state=PackageState.APPROVED)
|
2019-01-08 18:17:36 +01:00
|
|
|
if package is not None:
|
|
|
|
package_query = package_query.filter(Package.id != package.id)
|
|
|
|
|
|
|
|
enableWizard = name is None and request.method != "POST"
|
2020-12-04 03:23:04 +01:00
|
|
|
return render_template("packages/create_edit.html", package=package,
|
|
|
|
form=form, author=author, enable_wizard=enableWizard,
|
|
|
|
packages=package_query.all(),
|
2022-09-01 23:45:53 +02:00
|
|
|
modnames=MetaPackage.query.order_by(db.asc(MetaPackage.name)).all(),
|
2021-05-04 04:15:26 +02:00
|
|
|
tabs=get_package_tabs(current_user, package), current_tab="edit")
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2020-09-16 18:51:03 +02:00
|
|
|
|
|
|
|
@bp.route("/packages/<author>/<name>/state/", methods=["POST"])
|
2019-01-08 18:17:36 +01:00
|
|
|
@login_required
|
|
|
|
@is_package_page
|
2020-09-16 18:51:03 +02:00
|
|
|
def move_to_state(package):
|
|
|
|
state = PackageState.get(request.args.get("state"))
|
|
|
|
if state is None:
|
|
|
|
abort(400)
|
|
|
|
|
2024-06-23 10:44:15 +02:00
|
|
|
if package.state == state:
|
|
|
|
return redirect(package.get_url("packages.view"))
|
|
|
|
|
2024-03-23 17:17:26 +01:00
|
|
|
if not can_move_to_state(package, current_user, state):
|
2022-01-07 22:46:16 +01:00
|
|
|
flash(gettext("You don't have permission to do that"), "danger")
|
2023-06-18 22:56:19 +02:00
|
|
|
return redirect(package.get_url("packages.view"))
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2024-01-10 01:47:42 +01:00
|
|
|
old_state = package.state
|
2020-09-16 18:51:03 +02:00
|
|
|
package.state = state
|
|
|
|
msg = "Marked {} as {}".format(package.title, state.value)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2020-09-16 18:51:03 +02:00
|
|
|
if state == PackageState.APPROVED:
|
2020-07-18 02:48:21 +02:00
|
|
|
if not package.approved_at:
|
2023-07-22 13:45:50 +02:00
|
|
|
post_discord_webhook.delay(package.author.display_name,
|
2023-06-18 22:56:19 +02:00
|
|
|
"New package {}".format(package.get_url("packages.view", absolute=True)), False,
|
2023-11-10 19:57:49 +01:00
|
|
|
package.title, package.short_desc, package.get_thumb_url(2, True, "png"))
|
2020-07-18 02:48:21 +02:00
|
|
|
package.approved_at = datetime.datetime.now()
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
screenshots = PackageScreenshot.query.filter_by(package=package, approved=False).all()
|
|
|
|
for s in screenshots:
|
|
|
|
s.approved = True
|
|
|
|
|
2020-07-11 03:32:17 +02:00
|
|
|
msg = "Approved {}".format(package.title)
|
2024-03-27 20:03:48 +01:00
|
|
|
update_package_game_support.delay(package.id)
|
2021-08-17 22:16:43 +02:00
|
|
|
elif state == PackageState.READY_FOR_REVIEW:
|
2023-07-22 13:45:50 +02:00
|
|
|
post_discord_webhook.delay(package.author.display_name,
|
2023-06-18 22:56:19 +02:00
|
|
|
"Ready for Review: {}".format(package.get_url("packages.view", absolute=True)), True,
|
2023-11-10 19:57:49 +01:00
|
|
|
package.title, package.short_desc, package.get_thumb_url(2, True, "png"))
|
2020-09-16 18:51:03 +02:00
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
add_notification(package.maintainers, current_user, NotificationType.PACKAGE_APPROVAL, msg, package.get_url("packages.view"), package)
|
2020-09-16 18:51:03 +02:00
|
|
|
severity = AuditSeverity.NORMAL if current_user in package.maintainers else AuditSeverity.EDITOR
|
2023-06-19 22:27:49 +02:00
|
|
|
add_audit_log(severity, current_user, msg, package.get_url("packages.view"), package)
|
2024-01-10 01:48:15 +01:00
|
|
|
post_to_approval_thread(package, current_user, msg, True)
|
2020-09-16 18:51:03 +02:00
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
2024-07-04 22:29:56 +02:00
|
|
|
check_package_on_submit.delay(package.id)
|
|
|
|
|
2020-09-16 18:51:03 +02:00
|
|
|
if package.state == PackageState.CHANGES_NEEDED:
|
2022-01-20 02:18:10 +01:00
|
|
|
flash(gettext("Please comment what changes are needed in the approval thread"), "warning")
|
2020-09-16 18:51:03 +02:00
|
|
|
if package.review_thread:
|
2023-06-18 22:56:19 +02:00
|
|
|
return redirect(package.review_thread.get_view_url())
|
2020-09-16 18:51:03 +02:00
|
|
|
else:
|
|
|
|
return redirect(url_for('threads.new', pid=package.id, title='Package approval comments'))
|
2024-01-10 01:47:42 +01:00
|
|
|
elif (package.review_thread and
|
|
|
|
old_state == PackageState.CHANGES_NEEDED and package.state == PackageState.READY_FOR_REVIEW):
|
|
|
|
flash(gettext("Please comment in the approval thread so editors know what you have changed"), "warning")
|
|
|
|
return redirect(package.review_thread.get_view_url())
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
return redirect(package.get_url("packages.view"))
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
|
2024-02-25 18:50:33 +01:00
|
|
|
@bp.route("/packages/<author>/<name>/translation/")
|
|
|
|
@login_required
|
|
|
|
@is_package_page
|
|
|
|
def translation(package):
|
|
|
|
return render_template("packages/translation.html", package=package,
|
2024-07-18 08:25:32 +02:00
|
|
|
has_content_translations=any([x.title or x.short_desc for x in package.translations.all()]),
|
2024-02-25 18:50:33 +01:00
|
|
|
tabs=get_package_tabs(current_user, package), current_tab="translation")
|
|
|
|
|
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/packages/<author>/<name>/remove/", methods=["GET", "POST"])
|
2019-01-08 18:17:36 +01:00
|
|
|
@login_required
|
|
|
|
@is_package_page
|
2019-11-16 00:51:42 +01:00
|
|
|
def remove(package):
|
2024-06-07 23:57:55 +02:00
|
|
|
if not package.check_perm(current_user, Permission.EDIT_PACKAGE):
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
states = [PackageDevState.AS_IS, PackageDevState.DEPRECATED, PackageDevState.LOOKING_FOR_MAINTAINER]
|
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
if request.method == "GET":
|
2023-12-03 14:47:52 +01:00
|
|
|
# Find packages that will having missing hard deps after this action
|
|
|
|
broken_meta = MetaPackage.query.filter(MetaPackage.packages.contains(package),
|
|
|
|
~MetaPackage.packages.any(and_(Package.id != package.id, Package.state == PackageState.APPROVED)))
|
|
|
|
hard_deps = Package.query.filter(
|
|
|
|
Package.state == PackageState.APPROVED,
|
|
|
|
Package.dependencies.any(
|
2024-06-02 13:33:40 +02:00
|
|
|
and_(Dependency.meta_package_id.in_([x.id for x in broken_meta]), Dependency.optional == False))).all()
|
2023-12-03 14:47:52 +01:00
|
|
|
|
2024-06-07 23:57:55 +02:00
|
|
|
return render_template("packages/remove.html",
|
|
|
|
package=package, hard_deps=hard_deps, states=states,
|
2021-05-04 04:15:26 +02:00
|
|
|
tabs=get_package_tabs(current_user, package), current_tab="remove")
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2024-06-07 23:57:55 +02:00
|
|
|
for state in states:
|
|
|
|
if state.name in request.form:
|
|
|
|
flash(gettext("Set state to %(state)s", state=state.title), "success")
|
|
|
|
package.dev_state = state
|
|
|
|
msg = "Set dev state of {} to {}".format(package.title, state.title)
|
|
|
|
add_audit_log(AuditSeverity.NORMAL, current_user, msg, package.get_url("packages.view"), package)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(package.get_url("packages.view"))
|
|
|
|
|
2022-01-09 22:12:29 +01:00
|
|
|
reason = request.form.get("reason") or "?"
|
2024-06-02 13:33:40 +02:00
|
|
|
if len(reason) > 500:
|
|
|
|
abort(400)
|
2022-01-09 22:12:29 +01:00
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
if "delete" in request.form:
|
2023-06-18 22:56:19 +02:00
|
|
|
if not package.check_perm(current_user, Permission.DELETE_PACKAGE):
|
2022-06-05 01:54:09 +02:00
|
|
|
flash(gettext("You don't have permission to do that"), "danger")
|
2023-06-18 22:56:19 +02:00
|
|
|
return redirect(package.get_url("packages.view"))
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2020-09-16 18:51:03 +02:00
|
|
|
package.state = PackageState.DELETED
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
url = url_for("users.profile", username=package.author.username)
|
2022-01-09 22:12:29 +01:00
|
|
|
msg = "Deleted {}, reason={}".format(package.title, reason)
|
2023-06-19 22:27:49 +02:00
|
|
|
add_notification(package.maintainers, current_user, NotificationType.PACKAGE_EDIT, msg, url, package)
|
|
|
|
add_audit_log(AuditSeverity.EDITOR, current_user, msg, url, package)
|
2019-01-08 18:17:36 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
2023-10-02 00:41:30 +02:00
|
|
|
post_discord_webhook.delay(current_user.username,
|
|
|
|
f"Deleted package {package.author.username}/{package.name} with reason '{reason}'",
|
2023-11-10 19:57:49 +01:00
|
|
|
True, package.title, package.short_desc, package.get_thumb_url(2, True, "png"))
|
2023-10-02 00:41:30 +02:00
|
|
|
|
2024-03-27 20:03:48 +01:00
|
|
|
remove_package_game_support.delay(package.id)
|
|
|
|
|
2022-01-07 22:46:16 +01:00
|
|
|
flash(gettext("Deleted package"), "success")
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
return redirect(url)
|
|
|
|
elif "unapprove" in request.form:
|
2023-06-18 22:56:19 +02:00
|
|
|
if not package.check_perm(current_user, Permission.UNAPPROVE_PACKAGE):
|
2022-06-05 01:54:09 +02:00
|
|
|
flash(gettext("You don't have permission to do that"), "danger")
|
2023-06-18 22:56:19 +02:00
|
|
|
return redirect(package.get_url("packages.view"))
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2020-09-16 19:05:37 +02:00
|
|
|
package.state = PackageState.WIP
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2022-01-09 22:12:29 +01:00
|
|
|
msg = "Unapproved {}, reason={}".format(package.title, reason)
|
2023-06-19 22:27:49 +02:00
|
|
|
add_notification(package.maintainers, current_user, NotificationType.PACKAGE_APPROVAL, msg, package.get_url("packages.view"), package)
|
|
|
|
add_audit_log(AuditSeverity.EDITOR, current_user, msg, package.get_url("packages.view"), package)
|
2020-07-11 03:32:17 +02:00
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
2023-10-02 00:41:30 +02:00
|
|
|
post_discord_webhook.delay(current_user.username,
|
|
|
|
"Unapproved package with reason {}\n\n{}".format(reason, package.get_url("packages.view", absolute=True)), True,
|
2023-11-10 19:57:49 +01:00
|
|
|
package.title, package.short_desc, package.get_thumb_url(2, True, "png"))
|
2023-10-02 00:41:30 +02:00
|
|
|
|
2024-03-27 20:03:48 +01:00
|
|
|
remove_package_game_support.delay(package.id)
|
|
|
|
|
2022-01-07 22:46:16 +01:00
|
|
|
flash(gettext("Unapproved package"), "success")
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
return redirect(package.get_url("packages.view"))
|
2019-01-08 18:17:36 +01:00
|
|
|
else:
|
|
|
|
abort(400)
|
2020-07-08 23:45:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PackageMaintainersForm(FlaskForm):
|
2022-01-07 22:55:33 +01:00
|
|
|
maintainers_str = StringField(lazy_gettext("Maintainers (Comma-separated)"), [Optional()])
|
|
|
|
submit = SubmitField(lazy_gettext("Save"))
|
2020-07-08 23:45:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/packages/<author>/<name>/edit-maintainers/", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
@is_package_page
|
|
|
|
def edit_maintainers(package):
|
2023-06-18 22:56:19 +02:00
|
|
|
if not package.check_perm(current_user, Permission.EDIT_MAINTAINERS):
|
2022-06-05 01:54:09 +02:00
|
|
|
flash(gettext("You don't have permission to edit maintainers"), "danger")
|
2023-06-18 22:56:19 +02:00
|
|
|
return redirect(package.get_url("packages.view"))
|
2020-07-08 23:45:24 +02:00
|
|
|
|
|
|
|
form = PackageMaintainersForm(formdata=request.form)
|
|
|
|
if request.method == "GET":
|
2020-07-09 00:52:36 +02:00
|
|
|
form.maintainers_str.data = ", ".join([ x.username for x in package.maintainers if x != package.author ])
|
2020-07-08 23:45:24 +02:00
|
|
|
|
2020-12-05 00:07:19 +01:00
|
|
|
if form.validate_on_submit():
|
2020-07-09 00:00:45 +02:00
|
|
|
usernames = [x.strip().lower() for x in form.maintainers_str.data.split(",")]
|
2020-07-08 23:45:24 +02:00
|
|
|
users = User.query.filter(func.lower(User.username).in_(usernames)).all()
|
2020-07-09 00:20:29 +02:00
|
|
|
|
2021-07-20 00:49:29 +02:00
|
|
|
thread = package.threads.filter_by(author=get_system_user()).first()
|
|
|
|
|
2020-07-09 00:20:29 +02:00
|
|
|
for user in users:
|
|
|
|
if not user in package.maintainers:
|
2021-07-20 00:49:29 +02:00
|
|
|
if thread:
|
|
|
|
thread.watchers.append(user)
|
2023-06-19 22:27:49 +02:00
|
|
|
add_notification(user, current_user, NotificationType.MAINTAINER,
|
2023-06-18 22:56:19 +02:00
|
|
|
"Added you as a maintainer of {}".format(package.title), package.get_url("packages.view"), package)
|
2020-07-09 00:20:29 +02:00
|
|
|
|
|
|
|
for user in package.maintainers:
|
2020-07-09 00:52:36 +02:00
|
|
|
if user != package.author and not user in users:
|
2023-06-19 22:27:49 +02:00
|
|
|
add_notification(user, current_user, NotificationType.MAINTAINER,
|
2023-06-18 22:56:19 +02:00
|
|
|
"Removed you as a maintainer of {}".format(package.title), package.get_url("packages.view"), package)
|
2020-07-09 00:20:29 +02:00
|
|
|
|
2020-07-08 23:45:24 +02:00
|
|
|
package.maintainers.clear()
|
|
|
|
package.maintainers.extend(users)
|
2020-07-11 17:56:36 +02:00
|
|
|
if package.author not in package.maintainers:
|
|
|
|
package.maintainers.append(package.author)
|
2020-07-09 00:20:29 +02:00
|
|
|
|
2020-07-11 03:32:17 +02:00
|
|
|
msg = "Edited {} maintainers".format(package.title)
|
2023-06-19 22:27:49 +02:00
|
|
|
add_notification(package.author, current_user, NotificationType.MAINTAINER, msg, package.get_url("packages.view"), package)
|
2020-07-11 03:32:17 +02:00
|
|
|
severity = AuditSeverity.NORMAL if current_user == package.author else AuditSeverity.MODERATION
|
2023-06-19 22:27:49 +02:00
|
|
|
add_audit_log(severity, current_user, msg, package.get_url("packages.view"), package)
|
2020-07-09 00:20:29 +02:00
|
|
|
|
2020-07-08 23:45:24 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
return redirect(package.get_url("packages.view"))
|
2020-07-08 23:45:24 +02:00
|
|
|
|
|
|
|
users = User.query.filter(User.rank >= UserRank.NEW_MEMBER).order_by(db.asc(User.username)).all()
|
|
|
|
|
2021-05-04 04:15:26 +02:00
|
|
|
return render_template("packages/edit_maintainers.html", package=package, form=form,
|
|
|
|
users=users, tabs=get_package_tabs(current_user, package), current_tab="maintainers")
|
2020-07-09 00:42:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/packages/<author>/<name>/remove-self-maintainer/", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
@is_package_page
|
|
|
|
def remove_self_maintainers(package):
|
|
|
|
if not current_user in package.maintainers:
|
2022-01-07 22:46:16 +01:00
|
|
|
flash(gettext("You are not a maintainer"), "danger")
|
2020-07-09 00:42:30 +02:00
|
|
|
|
|
|
|
elif current_user == package.author:
|
2022-01-07 22:46:16 +01:00
|
|
|
flash(gettext("Package owners cannot remove themselves as maintainers"), "danger")
|
2020-07-09 00:42:30 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
package.maintainers.remove(current_user)
|
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
add_notification(package.author, current_user, NotificationType.MAINTAINER,
|
2023-06-18 22:56:19 +02:00
|
|
|
"Removed themself as a maintainer of {}".format(package.title), package.get_url("packages.view"), package)
|
2020-07-09 00:42:30 +02:00
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
return redirect(package.get_url("packages.view"))
|
2020-07-12 03:22:35 +02:00
|
|
|
|
|
|
|
|
2021-02-08 01:46:39 +01:00
|
|
|
@bp.route("/packages/<author>/<name>/audit/")
|
|
|
|
@login_required
|
|
|
|
@is_package_page
|
|
|
|
def audit(package):
|
2023-06-18 22:56:19 +02:00
|
|
|
if not (package.check_perm(current_user, Permission.EDIT_PACKAGE) or
|
|
|
|
package.check_perm(current_user, Permission.APPROVE_NEW)):
|
2021-02-08 01:46:39 +01:00
|
|
|
abort(403)
|
|
|
|
|
|
|
|
page = get_int_or_abort(request.args.get("page"), 1)
|
|
|
|
num = min(40, get_int_or_abort(request.args.get("n"), 100))
|
|
|
|
|
|
|
|
query = package.audit_log_entries.order_by(db.desc(AuditLogEntry.created_at))
|
|
|
|
|
2023-04-23 22:49:53 +02:00
|
|
|
pagination = query.paginate(page=page, per_page=num)
|
2021-07-24 04:56:43 +02:00
|
|
|
return render_template("packages/audit.html", log=pagination.items, pagination=pagination,
|
|
|
|
package=package, tabs=get_package_tabs(current_user, package), current_tab="audit")
|
2021-07-24 03:30:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PackageAliasForm(FlaskForm):
|
2022-01-07 22:55:33 +01:00
|
|
|
author = StringField(lazy_gettext("Author Name"), [InputRequired(), Length(1, 50)])
|
|
|
|
name = StringField(lazy_gettext("Name (Technical)"), [InputRequired(), Length(1, 100),
|
|
|
|
Regexp("^[a-z0-9_]+$", 0, lazy_gettext("Lower case letters (a-z), digits (0-9), and underscores (_) only"))])
|
|
|
|
submit = SubmitField(lazy_gettext("Save"))
|
2021-07-24 03:30:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/packages/<author>/<name>/aliases/")
|
2023-07-29 22:34:23 +02:00
|
|
|
@rank_required(UserRank.ADMIN)
|
2021-07-24 03:30:43 +02:00
|
|
|
@is_package_page
|
|
|
|
def alias_list(package: Package):
|
|
|
|
return render_template("packages/alias_list.html", package=package)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/packages/<author>/<name>/aliases/new/", methods=["GET", "POST"])
|
|
|
|
@bp.route("/packages/<author>/<name>/aliases/<int:alias_id>/", methods=["GET", "POST"])
|
2023-07-29 22:34:23 +02:00
|
|
|
@rank_required(UserRank.ADMIN)
|
2021-07-24 03:30:43 +02:00
|
|
|
@is_package_page
|
|
|
|
def alias_create_edit(package: Package, alias_id: int = None):
|
|
|
|
alias = None
|
|
|
|
if alias_id:
|
|
|
|
alias = PackageAlias.query.get(alias_id)
|
|
|
|
if alias is None or alias.package != package:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
form = PackageAliasForm(request.form, obj=alias)
|
|
|
|
if form.validate_on_submit():
|
|
|
|
if alias is None:
|
|
|
|
alias = PackageAlias()
|
|
|
|
alias.package = package
|
|
|
|
db.session.add(alias)
|
|
|
|
|
|
|
|
form.populate_obj(alias)
|
|
|
|
db.session.commit()
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
return redirect(package.get_url("packages.alias_list"))
|
2021-07-24 03:30:43 +02:00
|
|
|
|
|
|
|
return render_template("packages/alias_create_edit.html", package=package, form=form)
|
2021-07-24 04:56:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/packages/<author>/<name>/share/")
|
|
|
|
@login_required
|
|
|
|
@is_package_page
|
|
|
|
def share(package):
|
|
|
|
return render_template("packages/share.html", package=package,
|
|
|
|
tabs=get_package_tabs(current_user, package), current_tab="share")
|
2021-07-29 20:34:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/packages/<author>/<name>/similar/")
|
|
|
|
@is_package_page
|
|
|
|
def similar(package):
|
|
|
|
packages_modnames = {}
|
2022-09-01 23:45:53 +02:00
|
|
|
for mname in package.provides:
|
|
|
|
packages_modnames[mname] = Package.query.filter(Package.id != package.id,
|
2021-07-29 20:34:47 +02:00
|
|
|
Package.state != PackageState.DELETED) \
|
2022-09-01 23:45:53 +02:00
|
|
|
.filter(Package.provides.any(PackageProvides.c.metapackage_id == mname.id)) \
|
2021-07-29 20:34:47 +02:00
|
|
|
.order_by(db.desc(Package.score)) \
|
|
|
|
.all()
|
|
|
|
|
|
|
|
similar_topics = ForumTopic.query \
|
|
|
|
.filter_by(name=package.name) \
|
|
|
|
.filter(ForumTopic.topic_id != package.forums) \
|
|
|
|
.filter(~ db.exists().where(Package.forums == ForumTopic.topic_id)) \
|
|
|
|
.order_by(db.asc(ForumTopic.name), db.asc(ForumTopic.title)) \
|
|
|
|
.all()
|
|
|
|
|
|
|
|
return render_template("packages/similar.html", package=package,
|
|
|
|
packages_modnames=packages_modnames, similar_topics=similar_topics)
|
2022-06-25 01:32:32 +02:00
|
|
|
|
|
|
|
|
2024-03-27 23:06:52 +01:00
|
|
|
def csv_games_check(_form, field):
|
|
|
|
game_names = [name.strip() for name in field.data.split(",")]
|
|
|
|
if len(game_names) == 0 or (len(game_names) == 1 and game_names[0] == ""):
|
|
|
|
return
|
|
|
|
|
|
|
|
missing = set()
|
|
|
|
for game_name in game_names:
|
|
|
|
if game_name.endswith("_game"):
|
|
|
|
game_name = game_name[:-5]
|
|
|
|
if Package.query.filter(and_(Package.state==PackageState.APPROVED, Package.type==PackageType.GAME,
|
|
|
|
or_(Package.name==game_name, Package.name==game_name + "_game"))).count() == 0:
|
|
|
|
missing.add(game_name)
|
|
|
|
|
|
|
|
if len(missing) > 0:
|
|
|
|
raise ValidationError(f"Unable to find game {','.join(missing)}")
|
|
|
|
|
|
|
|
|
2022-06-25 02:16:22 +02:00
|
|
|
class GameSupportForm(FlaskForm):
|
2022-06-25 03:51:29 +02:00
|
|
|
enable_support_detection = BooleanField(lazy_gettext("Enable support detection based on dependencies (recommended)"), [Optional()])
|
2024-03-27 23:06:52 +01:00
|
|
|
supported = StringField(lazy_gettext("Supported games"), [Optional(), csv_games_check])
|
|
|
|
unsupported = StringField(lazy_gettext("Unsupported games"), [Optional(), csv_games_check])
|
2023-06-19 23:35:26 +02:00
|
|
|
supports_all_games = BooleanField(lazy_gettext("Supports all games (unless stated) / is game independent"), [Optional()])
|
2022-06-25 02:16:22 +02:00
|
|
|
submit = SubmitField(lazy_gettext("Save"))
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/packages/<author>/<name>/support/", methods=["GET", "POST"])
|
2022-06-25 01:32:32 +02:00
|
|
|
@login_required
|
|
|
|
@is_package_page
|
|
|
|
def game_support(package):
|
2023-01-12 20:48:18 +01:00
|
|
|
if package.type != PackageType.MOD and package.type != PackageType.TXP:
|
2022-06-25 01:32:32 +02:00
|
|
|
abort(404)
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
can_edit = package.check_perm(current_user, Permission.EDIT_PACKAGE)
|
|
|
|
if not (can_edit or package.check_perm(current_user, Permission.APPROVE_NEW)):
|
2022-06-25 01:32:32 +02:00
|
|
|
abort(403)
|
|
|
|
|
2023-09-02 22:37:20 +02:00
|
|
|
if package.releases.count() == 0:
|
|
|
|
flash(gettext("You need at least one release before you can edit game support"), "danger")
|
|
|
|
return redirect(package.get_url('packages.create_release' if package.update_config else 'packages.setup_releases'))
|
|
|
|
|
|
|
|
if package.type == PackageType.MOD and len(package.provides) == 0:
|
|
|
|
flash(gettext("Mod(pack) needs to contain at least one mod. Please create a new release"), "danger")
|
|
|
|
return redirect(package.get_url('packages.list_releases'))
|
|
|
|
|
2022-06-25 03:51:29 +02:00
|
|
|
force_game_detection = package.supported_games.filter(and_(
|
|
|
|
PackageGameSupport.confidence > 1, PackageGameSupport.supports == True)).count() == 0
|
|
|
|
|
2023-06-27 22:40:31 +02:00
|
|
|
can_support_all_games = package.type != PackageType.TXP and \
|
|
|
|
package.supported_games.filter(and_(
|
|
|
|
PackageGameSupport.confidence == 1, PackageGameSupport.supports == True)).count() == 0
|
2023-06-19 23:22:55 +02:00
|
|
|
|
2023-06-19 23:35:26 +02:00
|
|
|
can_override = can_edit
|
2023-06-18 22:11:17 +02:00
|
|
|
|
2022-06-25 02:16:22 +02:00
|
|
|
form = GameSupportForm() if can_edit else None
|
2022-06-25 04:13:36 +02:00
|
|
|
if form and request.method == "GET":
|
2022-06-25 03:27:51 +02:00
|
|
|
form.enable_support_detection.data = package.enable_game_support_detection
|
2023-09-02 22:37:20 +02:00
|
|
|
form.supports_all_games.data = package.supports_all_games and can_support_all_games
|
2023-06-18 22:11:17 +02:00
|
|
|
|
|
|
|
if can_override:
|
|
|
|
manual_supported_games = package.supported_games.filter_by(confidence=11).all()
|
|
|
|
form.supported.data = ", ".join([x.game.name for x in manual_supported_games if x.supports])
|
|
|
|
form.unsupported.data = ", ".join([x.game.name for x in manual_supported_games if not x.supports])
|
|
|
|
else:
|
|
|
|
form.supported = None
|
|
|
|
form.unsupported = None
|
2022-06-25 02:16:22 +02:00
|
|
|
|
|
|
|
if form and form.validate_on_submit():
|
2022-06-25 04:13:36 +02:00
|
|
|
detect_update_needed = False
|
|
|
|
|
2023-06-18 22:11:17 +02:00
|
|
|
if can_override:
|
2023-01-02 20:19:33 +01:00
|
|
|
try:
|
|
|
|
game_is_supported = {}
|
|
|
|
for game in get_games_from_csv(db.session, form.supported.data or ""):
|
|
|
|
game_is_supported[game.id] = True
|
|
|
|
for game in get_games_from_csv(db.session, form.unsupported.data or ""):
|
|
|
|
game_is_supported[game.id] = False
|
2024-03-27 20:03:48 +01:00
|
|
|
game_support_set(db.session, package, game_is_supported, 11)
|
2023-01-02 20:19:33 +01:00
|
|
|
detect_update_needed = True
|
|
|
|
except LogicError as e:
|
|
|
|
flash(e.message, "danger")
|
2022-06-25 02:16:22 +02:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
next_url = package.get_url("packages.game_support")
|
2022-06-25 03:27:51 +02:00
|
|
|
|
2022-06-25 03:51:29 +02:00
|
|
|
enable_support_detection = form.enable_support_detection.data or force_game_detection
|
|
|
|
if enable_support_detection != package.enable_game_support_detection:
|
|
|
|
package.enable_game_support_detection = enable_support_detection
|
2022-06-25 03:27:51 +02:00
|
|
|
if package.enable_game_support_detection:
|
2022-06-25 04:13:36 +02:00
|
|
|
detect_update_needed = True
|
2022-06-25 03:27:51 +02:00
|
|
|
else:
|
|
|
|
package.supported_games.filter_by(confidence=1).delete()
|
2022-06-25 04:13:36 +02:00
|
|
|
|
2023-06-19 23:22:55 +02:00
|
|
|
if can_support_all_games:
|
|
|
|
package.supports_all_games = form.supports_all_games.data
|
2023-06-18 22:11:17 +02:00
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
add_audit_log(AuditSeverity.NORMAL, current_user, "Edited game support", package.get_url("packages.game_support"), package)
|
2023-06-18 22:34:53 +02:00
|
|
|
|
2022-06-25 04:13:36 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
if detect_update_needed:
|
|
|
|
release = package.releases.first()
|
|
|
|
if release:
|
|
|
|
task_id = uuid()
|
2023-06-19 22:27:49 +02:00
|
|
|
check_zip_release.apply_async((release.id, release.file_path), task_id=task_id)
|
2022-06-25 04:13:36 +02:00
|
|
|
next_url = url_for("tasks.check", id=task_id, r=next_url)
|
2022-06-25 03:27:51 +02:00
|
|
|
|
|
|
|
return redirect(next_url)
|
2022-06-25 02:16:22 +02:00
|
|
|
|
2022-06-25 04:13:36 +02:00
|
|
|
all_game_support = package.supported_games.all()
|
|
|
|
all_game_support.sort(key=lambda x: -x.game.score)
|
2023-06-19 20:32:36 +02:00
|
|
|
supported_games_list: typing.List[str] = [x.game.name for x in all_game_support if x.supports]
|
2023-06-18 22:11:17 +02:00
|
|
|
if package.supports_all_games:
|
|
|
|
supported_games_list.insert(0, "*")
|
|
|
|
supported_games = ", ".join(supported_games_list)
|
2022-06-25 04:13:36 +02:00
|
|
|
unsupported_games = ", ".join([x.game.name for x in all_game_support if not x.supports])
|
|
|
|
|
|
|
|
mod_conf_lines = ""
|
|
|
|
if supported_games:
|
|
|
|
mod_conf_lines += f"supported_games = {supported_games}"
|
|
|
|
if unsupported_games:
|
|
|
|
mod_conf_lines += f"\nunsupported_games = {unsupported_games}"
|
|
|
|
|
2022-06-25 02:16:22 +02:00
|
|
|
return render_template("packages/game_support.html", package=package, form=form,
|
2022-06-25 04:13:36 +02:00
|
|
|
mod_conf_lines=mod_conf_lines, force_game_detection=force_game_detection,
|
2023-06-19 23:22:55 +02:00
|
|
|
can_support_all_games=can_support_all_games, tabs=get_package_tabs(current_user, package),
|
|
|
|
current_tab="game_support")
|
2022-11-06 18:58:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/packages/<author>/<name>/stats/")
|
|
|
|
@is_package_page
|
2022-11-08 19:47:28 +01:00
|
|
|
def statistics(package):
|
2023-06-14 23:47:08 +02:00
|
|
|
start = request.args.get("start")
|
|
|
|
end = request.args.get("end")
|
|
|
|
|
2022-11-06 18:58:35 +01:00
|
|
|
return render_template("packages/stats.html",
|
2023-06-14 23:47:08 +02:00
|
|
|
package=package, tabs=get_package_tabs(current_user, package), current_tab="stats",
|
|
|
|
start=start, end=end, options=get_daterange_options(), noindex=start or end)
|
2022-11-08 19:47:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/packages/<author>/<name>/stats.csv")
|
|
|
|
@is_package_page
|
|
|
|
def stats_csv(package):
|
2023-06-19 20:32:36 +02:00
|
|
|
stats: typing.List[PackageDailyStats] = package.daily_stats.order_by(db.asc(PackageDailyStats.date)).all()
|
2022-11-08 19:47:28 +01:00
|
|
|
|
|
|
|
columns = ["platform_minetest", "platform_other", "reason_new",
|
|
|
|
"reason_dependency", "reason_update"]
|
|
|
|
|
|
|
|
result = "Date, " + ", ".join(columns) + "\n"
|
|
|
|
|
|
|
|
for stat in stats:
|
|
|
|
stat: PackageDailyStats
|
|
|
|
result += stat.date.isoformat()
|
|
|
|
for i, key in enumerate(columns):
|
|
|
|
result += ", " + str(getattr(stat, key))
|
|
|
|
result += "\n"
|
|
|
|
|
|
|
|
date = datetime.datetime.utcnow().date()
|
|
|
|
|
|
|
|
res = make_response(result, 200)
|
|
|
|
res.headers["Content-Disposition"] = f"attachment; filename={package.author.username}_{package.name}_stats_{date.isoformat()}.csv"
|
|
|
|
res.headers["Content-type"] = "text/csv"
|
|
|
|
return res
|