From fda97aa0e29c5a0829c831594476966267f64124 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Mon, 4 Mar 2024 23:36:22 -0800 Subject: [PATCH] Show classifier status as OK if shouldnt exist --- src/documents/tests/test_api_status.py | 25 ++++++++++++++++++++++++- src/documents/views.py | 18 +++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/documents/tests/test_api_status.py b/src/documents/tests/test_api_status.py index 964995bdc..d7d45250a 100644 --- a/src/documents/tests/test_api_status.py +++ b/src/documents/tests/test_api_status.py @@ -9,6 +9,8 @@ from rest_framework.test import APITestCase from documents.classifier import DocumentClassifier from documents.classifier import load_classifier +from documents.models import Document +from documents.models import Tag from paperless import version @@ -172,15 +174,36 @@ class TestSystemStatus(APITestCase): def test_system_status_classifier_error(self): """ GIVEN: - - The classifier is not found + - The classifier does not exist + - > 0 documents and tags with auto matching exist WHEN: - The user requests the system status THEN: - The response contains an error classifier status """ with override_settings(MODEL_FILE="does_not_exist"): + Document.objects.create( + title="Test Document", + ) + Tag.objects.create(name="Test Tag", matching_algorithm=Tag.MATCH_AUTO) self.client.force_login(self.user) response = self.client.get(self.ENDPOINT) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data["tasks"]["classifier_status"], "ERROR") self.assertIsNotNone(response.data["tasks"]["classifier_error"]) + + def test_system_status_classifier_ok_no_objects(self): + """ + GIVEN: + - The classifier does not exist (and should not) + - No documents nor objects with auto matching exist + WHEN: + - The user requests the system status + THEN: + - The response contains an OK classifier status + """ + with override_settings(MODEL_FILE="does_not_exist"): + self.client.force_login(self.user) + response = self.client.get(self.ENDPOINT) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data["tasks"]["classifier_status"], "OK") diff --git a/src/documents/views.py b/src/documents/views.py index bd0b6fa0f..ec6ad90d7 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -1622,7 +1622,23 @@ class SystemStatusView(GenericAPIView, PassUserMixin): try: classifier = load_classifier() if classifier is None: - raise Exception("Classifier not loaded") + # Check if classifier should exist + docs_queryset = Document.objects.exclude( + tags__is_inbox_tag=True, + ) + if docs_queryset.count() > 0 and ( + Tag.objects.filter(matching_algorithm=Tag.MATCH_AUTO).exists() + or DocumentType.objects.filter( + matching_algorithm=Tag.MATCH_AUTO, + ).exists() + or Correspondent.objects.filter( + matching_algorithm=Tag.MATCH_AUTO, + ).exists() + or StoragePath.objects.filter( + matching_algorithm=Tag.MATCH_AUTO, + ).exists() + ): + raise Exception("Classifier not loaded") classifier_status = "OK" task_result_model = apps.get_model("django_celery_results", "taskresult") result = (