From c6900c5d51275fed6dd94300d77ea6241dc17780 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 23 Feb 2023 10:57:58 -0800 Subject: [PATCH] prevent date suggestion search if disabled --- src/documents/tests/test_api.py | 23 +++++++++++++++++++++++ src/documents/views.py | 10 ++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.py index 2e8e96207..0a87bd95f 100644 --- a/src/documents/tests/test_api.py +++ b/src/documents/tests/test_api.py @@ -1948,6 +1948,29 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase): }, ) + @mock.patch("documents.parsers.parse_date_generator") + @override_settings(NUMBER_OF_SUGGESTED_DATES=0) + def test_get_suggestions_dates_disabled( + self, + parse_date_generator, + ): + """ + GIVEN: + - NUMBER_OF_SUGGESTED_DATES = 0 (disables feature) + WHEN: + - API reuqest for document suggestions + THEN: + - Dont check for suggested dates at all + """ + doc = Document.objects.create( + title="test", + mime_type="application/pdf", + content="this is an invoice from 12.04.2022!", + ) + + self.client.get(f"/api/documents/{doc.pk}/suggestions/") + self.assertFalse(parse_date_generator.called) + def test_saved_views(self): u1 = User.objects.create_superuser("user1") u2 = User.objects.create_superuser("user2") diff --git a/src/documents/views.py b/src/documents/views.py index 1c80d0bab..db25e6934 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -422,10 +422,12 @@ class DocumentViewSet( classifier = load_classifier() - gen = parse_date_generator(doc.filename, doc.content) - dates = sorted( - {i for i in itertools.islice(gen, settings.NUMBER_OF_SUGGESTED_DATES)}, - ) + dates = [] + if settings.NUMBER_OF_SUGGESTED_DATES > 0: + gen = parse_date_generator(doc.filename, doc.content) + dates = sorted( + {i for i in itertools.islice(gen, settings.NUMBER_OF_SUGGESTED_DATES)}, + ) return Response( {