From 6341d90f5679594de226da5d8443388a7dc5fa01 Mon Sep 17 00:00:00 2001 From: Oliver Parker Date: Mon, 3 Oct 2022 21:14:10 +0100 Subject: [PATCH] Support "--verbosity 0" on update_index command - Ensure that the `update_index` command can run without console output if called with `--verbosity 0` - rebase of #4401 --- CHANGELOG.txt | 1 + docs/reference/management_commands.md | 10 +++++++ docs/releases/4.1.md | 1 + .../management/commands/update_index.py | 27 ++++++++++++------- wagtail/search/tests/test_backends.py | 7 +++++ 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 85f43ae17c..9c02817858 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -44,6 +44,7 @@ Changelog * Add anchor links to StreamField blocks so users can navigate straight to a given block (Thibaud Colas) * Support "Ctrl + f" in-page search within collapsed StreamField blocks (Thibaud Colas) * Remember the last opened side panel in the page editor, activating it on page load (Sage Abdullah) + * Ensure that the `update_index` command can run without console output if called with `--verbosity 0` (Ben Sturmfels, Oliver Parker) * Fix: Prevent `PageQuerySet.not_public` from returning all pages when no page restrictions exist (Mehrdad Moradizadeh) * Fix: Ensure that duplicate block ids are unique when duplicating stream blocks in the page editor (Joshua Munn) * Fix: Revise colour usage so that privacy & locked indicators can be seen in Windows High Contrast mode (LB (Ben Johnston)) diff --git a/docs/reference/management_commands.md b/docs/reference/management_commands.md index 22e6038bf8..92cb712b2c 100644 --- a/docs/reference/management_commands.md +++ b/docs/reference/management_commands.md @@ -97,6 +97,16 @@ You can prevent the `update_index` command from indexing any data by using the ` python manage.py update_index --schema-only ``` +### Silencing the command + +You can prevent logs to the console by providing `--verbosity 0` as an argument: + +```console +$ python manage.py update_index --verbosity 0 +``` + +If this is omitted or provided with any number above 0 it will produce the same logs. + (wagtail_update_index)= ## wagtail_update_index diff --git a/docs/releases/4.1.md b/docs/releases/4.1.md index 44752e568c..8a45664103 100644 --- a/docs/releases/4.1.md +++ b/docs/releases/4.1.md @@ -66,6 +66,7 @@ This feature was developed by Karl Hobley and Matt Westcott. * Add anchor links to StreamField blocks so users can navigate straight to a given block (Thibaud Colas) * Support "Ctrl + f" in-page search within collapsed StreamField blocks (Thibaud Colas) * Remember the last opened side panel in the page editor, activating it on page load (Sage Abdullah) + * Ensure that the [`update_index`](update_index) command can run without console output if called with `--verbosity 0` (Ben Sturmfels, Oliver Parker) ### Bug fixes diff --git a/wagtail/search/management/commands/update_index.py b/wagtail/search/management/commands/update_index.py index 2a31553bc4..32aa9b0932 100644 --- a/wagtail/search/management/commands/update_index.py +++ b/wagtail/search/management/commands/update_index.py @@ -54,25 +54,30 @@ def group_models_by_index(backend, models): class Command(BaseCommand): + def write(self, *args, **kwargs): + """Helper function that respects verbosity when printing.""" + if self.verbosity > 0: + self.stdout.write(*args, **kwargs) + def update_backend( self, backend_name, schema_only=False, chunk_size=DEFAULT_CHUNK_SIZE ): - self.stdout.write("Updating backend: " + backend_name) + self.write("Updating backend: " + backend_name) backend = get_search_backend(backend_name) if not backend.rebuilder_class: - self.stdout.write("Backend '%s' doesn't require rebuilding" % backend_name) + self.write("Backend '%s' doesn't require rebuilding" % backend_name) return models_grouped_by_index = group_models_by_index( backend, get_indexed_models() ).items() if not models_grouped_by_index: - self.stdout.write(backend_name + ": No indices to rebuild") + self.write(backend_name + ": No indices to rebuild") for index, models in models_grouped_by_index: - self.stdout.write(backend_name + ": Rebuilding index %s" % index.name) + self.write(backend_name + ": Rebuilding index %s" % index.name) # Start rebuild rebuilder = backend.rebuilder_class(index) @@ -86,7 +91,7 @@ class Command(BaseCommand): object_count = 0 if not schema_only: for model in models: - self.stdout.write( + self.write( "{}: {}.{} ".format( backend_name, model._meta.app_label, model.__name__ ).ljust(35), @@ -107,7 +112,7 @@ class Command(BaseCommand): # Finish rebuild rebuilder.finish() - self.stdout.write(backend_name + ": indexed %d objects" % object_count) + self.write(backend_name + ": indexed %d objects" % object_count) self.print_newline() def add_arguments(self, parser): @@ -135,6 +140,8 @@ class Command(BaseCommand): ) def handle(self, **options): + self.verbosity = options["verbosity"] + # Get list of backends to index if options["backend_name"]: # index only the passed backend @@ -155,7 +162,7 @@ class Command(BaseCommand): ) def print_newline(self): - self.stdout.write("") + self.write("") def print_iter_progress(self, iterable): """ @@ -170,13 +177,13 @@ class Command(BaseCommand): """ for i, value in enumerate(iterable, start=1): yield value - self.stdout.write(".", ending="") + self.write(".", ending="") if i % 40 == 0: self.print_newline() - self.stdout.write(" " * 35, ending="") + self.write(" " * 35, ending="") elif i % 10 == 0: - self.stdout.write(" ", ending="") + self.write(" ", ending="") self.stdout.flush() diff --git a/wagtail/search/tests/test_backends.py b/wagtail/search/tests/test_backends.py index e41ace5554..18bad006ca 100644 --- a/wagtail/search/tests/test_backends.py +++ b/wagtail/search/tests/test_backends.py @@ -906,6 +906,13 @@ class BackendTests(WagtailTestUtils): ) self.assertSetEqual({r.title for r in results}, {"Programming Rust"}) + def test_update_index_no_verbosity(self): + stdout = StringIO() + management.call_command( + "update_index", verbosity=0, backend_name=self.backend_name, stdout=stdout + ) + self.assertFalse(stdout.read()) + @override_settings( WAGTAILSEARCH_BACKENDS={"default": {"BACKEND": "wagtail.search.backends.database"}}