From b4e4ba35169c72a161e263e540c1586d3a4d790a Mon Sep 17 00:00:00 2001 From: Samuel Paccoud - DINUM Date: Mon, 1 Jul 2024 15:41:13 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(documents)=20store=20documen?= =?UTF-8?q?t=20file=20in=20object=20storage=20in=20a=20folder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We will need to store more than a file for a document: multiple languages, images, etc. For this, the document ID should be a folder and the content a file in this folder. --- src/backend/core/models.py | 13 ++++++++++--- src/backend/core/tests/test_models_documents.py | 6 ++++++ 2 files changed, 16 insertions(+), 3 deletions(-) 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