diff --git a/src/backend/core/models.py b/src/backend/core/models.py index 61c7c2df..a1cb9efa 100644 --- a/src/backend/core/models.py +++ b/src/backend/core/models.py @@ -316,12 +316,19 @@ class Document(BaseModel): def __str__(self): return self.title + @property + def key_base(self): + """Key base of the location where the document is stored in object storage.""" + if not self.pk: + raise RuntimeError( + "The document instance must be saved before requesting a storage key." + ) + return str(self.pk) + @property def file_key(self): """Key of the object storage file to which the document content is stored""" - if not self.pk: - return None - return str(self.pk) + return f"{self.key_base}/file" @property def content(self): diff --git a/src/backend/core/tests/test_models_documents.py b/src/backend/core/tests/test_models_documents.py index c4c58901..c7462353 100644 --- a/src/backend/core/tests/test_models_documents.py +++ b/src/backend/core/tests/test_models_documents.py @@ -47,6 +47,12 @@ def test_models_documents_title_max_length(): factories.DocumentFactory(title="a" * 256) +def test_models_documents_file_key(): + """The file key should be built from the instance uuid.""" + document = factories.DocumentFactory(id="9531a5f1-42b1-496c-b3f4-1c09ed139b3c") + assert document.file_key == "9531a5f1-42b1-496c-b3f4-1c09ed139b3c/file" + + # get_abilities