diff --git a/wagtail_wordpress_import/management/commands/reduce_xml.py b/wagtail_wordpress_import/management/commands/reduce_xml.py index 96d37060..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,11 +111,9 @@ def handle(self, *args, **options): self.stdout.write(f"\n[{status_list}]") # stats to file - f = open(f"stats-{file_path}.json", "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-{file_path}.json") + self.style.SUCCESS(f"\nStats file is here: {stats_filename}") ) self.stdout.write( diff --git a/wagtail_wordpress_import/test/tests/test_commands.py b/wagtail_wordpress_import/test/tests/test_commands.py index f4c2b864..d7cea98e 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.json") + + stats_filename = generate_stats_file("test.xml", {}) + self.assertEqual(stats_filename, "stats-test.xml.json")