From 8c7a4a747ad463c2b65983116253f6cf039dfba5 Mon Sep 17 00:00:00 2001 From: Fabien Le Frapper Date: Fri, 24 Dec 2021 09:04:23 +0100 Subject: [PATCH 1/3] Support slash in file path in reduce command --- wagtail_wordpress_import/management/commands/reduce_xml.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/wagtail_wordpress_import/management/commands/reduce_xml.py b/wagtail_wordpress_import/management/commands/reduce_xml.py index 96d37060..e97de990 100644 --- a/wagtail_wordpress_import/management/commands/reduce_xml.py +++ b/wagtail_wordpress_import/management/commands/reduce_xml.py @@ -106,11 +106,12 @@ def handle(self, *args, **options): self.stdout.write(f"\n[{status_list}]") # stats to file - f = open(f"stats-{file_path}.json", "w") + stats_filename = f"stats-{file_path.split('/')[-1]}.json" + f = open(stats_filename, "w") f.write(json.dumps(type_stats, indent=2)) self.stdout.write( - self.style.SUCCESS(f"\nStats file is here: stats-{file_path}.json") + self.style.SUCCESS(f"\nStats file is here: {stats_filename}") ) self.stdout.write( From 1869ef4f7dbc50003b1348c05f2c62b97705e0a6 Mon Sep 17 00:00:00 2001 From: Fabien Le Frapper Date: Fri, 24 Dec 2021 09:34:44 +0100 Subject: [PATCH 2/3] Add tests + move stats in its own function --- .../management/commands/reduce_xml.py | 10 ++++++---- wagtail_wordpress_import/test/tests/test_commands.py | 9 ++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/wagtail_wordpress_import/management/commands/reduce_xml.py b/wagtail_wordpress_import/management/commands/reduce_xml.py index e97de990..c4641566 100644 --- a/wagtail_wordpress_import/management/commands/reduce_xml.py +++ b/wagtail_wordpress_import/management/commands/reduce_xml.py @@ -11,6 +11,11 @@ def register_all_namespaces(filename): for ns in namespaces: ET.register_namespace(ns, namespaces[ns]) +def generate_stats_file(file_path, type_stats): + stats_filename = f"stats-{file_path.split('/')[-1]}.json" + f = open(stats_filename, "w") + f.write(json.dumps(type_stats, indent=2)) + return stats_filename class Command(BaseCommand): @@ -106,10 +111,7 @@ def handle(self, *args, **options): self.stdout.write(f"\n[{status_list}]") # stats to file - stats_filename = f"stats-{file_path.split('/')[-1]}.json" - f = open(stats_filename, "w") - f.write(json.dumps(type_stats, indent=2)) - + stats_filename = generate_stats_file(file_path, type_stats) self.stdout.write( self.style.SUCCESS(f"\nStats file is here: {stats_filename}") ) diff --git a/wagtail_wordpress_import/test/tests/test_commands.py b/wagtail_wordpress_import/test/tests/test_commands.py index f4c2b864..4bfbc215 100644 --- a/wagtail_wordpress_import/test/tests/test_commands.py +++ b/wagtail_wordpress_import/test/tests/test_commands.py @@ -3,7 +3,7 @@ from django.core.management import CommandError, call_command from django.test import TestCase, override_settings from wagtail.core.models import Page -from wagtail_wordpress_import.management.commands.reduce_xml import Command as ReduceCmd +from wagtail_wordpress_import.management.commands.reduce_xml import Command as ReduceCmd, generate_stats_file from wagtail_wordpress_import.xml_boilerplate import ( build_xml_stream, generate_temporary_file, @@ -153,3 +153,10 @@ def test_get_xml_file(self): build_xml_stream(xml_tags_fragment="").read() ) self.assertEqual(built_file, cmd.get_xml_file(built_file)) + + def test_xml_reduce_generate_stats(self): + stats_filename = generate_stats_file("/folder/test.xml", {}) + self.assertEqual(stats_filename, "stats-test.xml") + + stats_filename = generate_stats_file("test.xml", {}) + self.assertEqual(stats_filename, "stats-test.xml") From a61b0729849aaa415e6e249b0cdd3c31208d140a Mon Sep 17 00:00:00 2001 From: Fabien Le Frapper Date: Fri, 24 Dec 2021 12:27:06 +0100 Subject: [PATCH 3/3] Fix tests --- wagtail_wordpress_import/test/tests/test_commands.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wagtail_wordpress_import/test/tests/test_commands.py b/wagtail_wordpress_import/test/tests/test_commands.py index 4bfbc215..d7cea98e 100644 --- a/wagtail_wordpress_import/test/tests/test_commands.py +++ b/wagtail_wordpress_import/test/tests/test_commands.py @@ -156,7 +156,7 @@ def test_get_xml_file(self): def test_xml_reduce_generate_stats(self): stats_filename = generate_stats_file("/folder/test.xml", {}) - self.assertEqual(stats_filename, "stats-test.xml") + self.assertEqual(stats_filename, "stats-test.xml.json") stats_filename = generate_stats_file("test.xml", {}) - self.assertEqual(stats_filename, "stats-test.xml") + self.assertEqual(stats_filename, "stats-test.xml.json")