From 43f4d4a7f44e66526fae03f9974ce070ce265816 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 27 Mar 2024 22:06:52 +0000 Subject: [PATCH] Validate game names given to the support_game field --- app/blueprints/packages/packages.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/app/blueprints/packages/packages.py b/app/blueprints/packages/packages.py index 88f7f93b..9d56a90b 100644 --- a/app/blueprints/packages/packages.py +++ b/app/blueprints/packages/packages.py @@ -676,10 +676,27 @@ def similar(package): packages_modnames=packages_modnames, similar_topics=similar_topics) +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)}") + + class GameSupportForm(FlaskForm): enable_support_detection = BooleanField(lazy_gettext("Enable support detection based on dependencies (recommended)"), [Optional()]) - supported = StringField(lazy_gettext("Supported games"), [Optional()]) - unsupported = StringField(lazy_gettext("Unsupported games"), [Optional()]) + supported = StringField(lazy_gettext("Supported games"), [Optional(), csv_games_check]) + unsupported = StringField(lazy_gettext("Unsupported games"), [Optional(), csv_games_check]) supports_all_games = BooleanField(lazy_gettext("Supports all games (unless stated) / is game independent"), [Optional()]) submit = SubmitField(lazy_gettext("Save"))