From 31b19a48b701691c4d3b8c9bfe0b8ae8eca09bad Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Sat, 3 Feb 2024 14:25:05 -0800 Subject: [PATCH] Fixes the coverage probably too --- src/documents/caching.py | 18 +++++++++++++++++- src/documents/tests/test_api_documents.py | 4 ++-- src/documents/views.py | 5 +++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/documents/caching.py b/src/documents/caching.py index 27eb2cac6..4441c611d 100644 --- a/src/documents/caching.py +++ b/src/documents/caching.py @@ -1,4 +1,5 @@ import logging +from binascii import hexlify from dataclasses import dataclass from typing import TYPE_CHECKING from typing import Final @@ -61,6 +62,20 @@ def get_suggestion_cache(document_id: int) -> Optional[SuggestionCacheData]: # The classifier format is the same # The classifier hash is the same # Then the suggestions can be used + print(CLASSIFIER_VERSION_KEY in cache_hits) + if CLASSIFIER_VERSION_KEY in cache_hits: + print( + cache_hits[CLASSIFIER_VERSION_KEY] == DocumentClassifier.FORMAT_VERSION, + ) + print( + cache_hits[CLASSIFIER_VERSION_KEY] + == doc_suggestions.classifier_version, + ) + print(CLASSIFIER_HASH_KEY in cache_hits) + if CLASSIFIER_HASH_KEY in cache_hits: + print(cache_hits[CLASSIFIER_HASH_KEY]) + print(doc_suggestions.classifier_hash) + print(cache_hits[CLASSIFIER_HASH_KEY] == doc_suggestions.classifier_hash) if ( CLASSIFIER_VERSION_KEY in cache_hits and cache_hits[CLASSIFIER_VERSION_KEY] == DocumentClassifier.FORMAT_VERSION @@ -89,11 +104,12 @@ def set_suggestions_cache( """ if classifier is not None: doc_key = get_suggestion_cache_key(document_id) + print(classifier.last_auto_type_hash) cache.set( doc_key, SuggestionCacheData( classifier.FORMAT_VERSION, - classifier.last_auto_type_hash.hex(), + hexlify(classifier.last_auto_type_hash).decode(), suggestions, ), timeout, diff --git a/src/documents/tests/test_api_documents.py b/src/documents/tests/test_api_documents.py index bd36d4c11..f3199a30f 100644 --- a/src/documents/tests/test_api_documents.py +++ b/src/documents/tests/test_api_documents.py @@ -1317,7 +1317,7 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase): last_modified = timezone.now() cache.set(CLASSIFIER_MODIFIED_KEY, last_modified, CACHE_50_MINUTES) - cache.set(CLASSIFIER_HASH_KEY, classifier_checksum, CACHE_50_MINUTES) + cache.set(CLASSIFIER_HASH_KEY, classifier_checksum.decode(), CACHE_50_MINUTES) cache.set( CLASSIFIER_VERSION_KEY, DocumentClassifier.FORMAT_VERSION, @@ -1356,7 +1356,7 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase): self.assertIn("ETag", response.headers) self.assertEqual( response.headers["ETag"], - f'"{classifier_checksum}:{settings.NUMBER_OF_SUGGESTED_DATES}"', + f'"{classifier_checksum.decode()}:{settings.NUMBER_OF_SUGGESTED_DATES}"', ) response = self.client.get(f"/api/documents/{doc.pk}/suggestions/") diff --git a/src/documents/views.py b/src/documents/views.py index 0faedc989..13b095f61 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -421,14 +421,15 @@ class DocumentViewSet( document_cached_metadata = get_metadata_cache(doc.pk) + archive_metadata = None + archive_filesize = None if document_cached_metadata is not None: original_metadata = document_cached_metadata.original_metadata archive_metadata = document_cached_metadata.archive_metadata refresh_metadata_cache(doc.pk) else: original_metadata = self.get_metadata(doc.source_path, doc.mime_type) - archive_metadata = None - archive_filesize = None + if doc.has_archive_version: archive_filesize = self.get_filesize(doc.archive_path) archive_metadata = self.get_metadata(