From 95c78fcc595ae225ffd7a278d27e4a7d42a2414b Mon Sep 17 00:00:00 2001 From: Joseph Min Date: Sat, 9 Dec 2023 11:48:54 -0500 Subject: [PATCH 1/3] set input initial from url query parameters --- wooey/tests/mixins.py | 6 +++ wooey/tests/test_views.py | 108 ++++++++++++++++++++++++++++++++++++++ wooey/views/views.py | 11 ++++ 3 files changed, 125 insertions(+) diff --git a/wooey/tests/mixins.py b/wooey/tests/mixins.py index 541ea56..f1eb095 100644 --- a/wooey/tests/mixins.py +++ b/wooey/tests/mixins.py @@ -59,6 +59,12 @@ class ScriptTearDown(object): class ScriptFactoryMixin(ScriptTearDown, object): def setUp(self): + self.command_order_script_path = os.path.join( + config.WOOEY_TEST_SCRIPTS, "command_order.py" + ) + self.command_order_script = factories.generate_script( + self.command_order_script_path + ) self.translate_script_path = os.path.join( config.WOOEY_TEST_SCRIPTS, "translate.py" ) diff --git a/wooey/tests/test_views.py b/wooey/tests/test_views.py index bcfcfb1..a757e93 100644 --- a/wooey/tests/test_views.py +++ b/wooey/tests/test_views.py @@ -278,6 +278,114 @@ class WooeyViews(mixins.ScriptFactoryMixin, mixins.FileCleanupMixin, TestCase): d = load_JSON_dict(response.content) self.assertTrue(d["valid"], d) + def test_url_parameters_positional(self): + script_version = self.command_order_script + url = reverse("wooey:wooey_script", kwargs={"slug": script_version.script.slug}) + request = self.factory.get( + url, + data={ + "link": "abc", + }, + ) + request.user = AnonymousUser() + response = self.script_view_func( + request, + pk=script_version.script.pk, + ) + self.assertEqual(response.status_code, 200) + + context = response.resolve_context(response.context_data) + parser = list(context["form"]["parsers"].keys())[0] + self.assertEqual( + context["form"]["parsers"][parser][0]["form"] + .fields[f"{parser[0]}-link"] + .initial, + "abc", + ) + + def test_url_parameters_optional(self): + script_version = self.translate_script + url = reverse("wooey:wooey_script", kwargs={"slug": script_version.script.slug}) + request = self.factory.get( + url, + data={ + "sequence": "abc", + }, + ) + request.user = AnonymousUser() + response = self.script_view_func( + request, + pk=script_version.script.pk, + ) + self.assertEqual(response.status_code, 200) + + context = response.resolve_context(response.context_data) + parser = list(context["form"]["parsers"].keys())[0] + self.assertEqual( + context["form"]["parsers"][parser][0]["form"] + .fields[f"{parser[0]}-sequence"] + .initial, + "abc", + ) + + def test_url_parameters_multi_choice(self): + script_version = self.choice_script + url = reverse("wooey:wooey_script", kwargs={"slug": script_version.script.slug}) + request = self.factory.get( + url, + data={ + "one_choice": "0", + "two_choices": ["0", "1"], + }, + ) + request.user = AnonymousUser() + response = self.script_view_func( + request, + pk=script_version.script.pk, + ) + self.assertEqual(response.status_code, 200) + + context = response.resolve_context(response.context_data) + parser = list(context["form"]["parsers"].keys())[0] + self.assertEqual( + context["form"]["parsers"][parser][1]["form"] + .fields[f"{parser[0]}-one_choice"] + .initial, + "0", + ) + self.assertEqual( + context["form"]["parsers"][parser][1]["form"] + .fields[f"{parser[0]}-two_choices"] + .initial, + ["0", "1"], + ) + + def test_url_parameters_subparser(self): + script_version = self.subparser_script + url = reverse("wooey:wooey_script", kwargs={"slug": script_version.script.slug}) + request = self.factory.get(url, data={"test_arg": "3.3", "sp1": "2"}) + request.user = AnonymousUser() + response = self.script_view_func( + request, + pk=script_version.script.pk, + ) + self.assertEqual(response.status_code, 200) + + context = response.resolve_context(response.context_data) + main_parser, subparser1, subparser2 = list(context["form"]["parsers"].keys()) + self.assertEqual( + context["form"]["parsers"][main_parser][0]["form"] + .fields[f"{main_parser[0]}-test_arg"] + .initial, + "3.3", + ) + self.assertEqual( + context["form"]["parsers"][subparser1][0]["form"] + .fields[f"{subparser1[0]}-sp1"] + .initial, + "2", + ) + def test_job_view_permissions(self): # Make sure users cannot see jobs from other users job = factories.generate_job(self.translate_script) diff --git a/wooey/views/views.py b/wooey/views/views.py index 96135bd..46e9de7 100644 --- a/wooey/views/views.py +++ b/wooey/views/views.py @@ -73,6 +73,17 @@ class WooeyScriptBase(DetailView): "script_version", "script_iteration" ).last() + # Set parameter initial values by parsing the URL parameters + # and matching them to the script parameters. + for param in script_version.get_parameters(): + if param.script_param in self.request.GET: + value = ( + self.request.GET.getlist(param.script_param) + if param.multiple_choice + else self.request.GET.get(param.script_param) + ) + initial[param.form_slug] = value + context["form"] = utils.get_form_groups( script_version=script_version, initial_dict=initial, From b752728a949c25b90e633e3417d8e0b6ebff824e Mon Sep 17 00:00:00 2001 From: Joseph Min Date: Sat, 9 Dec 2023 11:49:08 -0500 Subject: [PATCH 2/3] select subparser from url query parameters --- wooey/templates/wooey/scripts/script_view.html | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/wooey/templates/wooey/scripts/script_view.html b/wooey/templates/wooey/scripts/script_view.html index 205c003..7384b29 100644 --- a/wooey/templates/wooey/scripts/script_view.html +++ b/wooey/templates/wooey/scripts/script_view.html @@ -56,7 +56,7 @@