From 26c4331648945772ad697bbce8b3a6140c5de43a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Birrer?= Date: Thu, 14 Dec 2023 14:03:00 +0100 Subject: [PATCH] fix(document): adjust cloning for use with storage backends NOTE: no automatic thumbnailgeneration --- alexandria/core/factories.py | 21 ++++++++++++++++++++- alexandria/core/models.py | 8 ++++---- alexandria/core/tests/test_models.py | 21 +++++---------------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/alexandria/core/factories.py b/alexandria/core/factories.py index 1ddf87fe..dc457966 100644 --- a/alexandria/core/factories.py +++ b/alexandria/core/factories.py @@ -93,7 +93,26 @@ def marks(self, create, extracted, **kwargs): # pragma: todo cover class FileFactory(BaseFactory): - name = Faker("name") + """Factory for File. + + Usage: + If you want a nice set of Document, File with thumbnails: + + `thumb = FileFactory(variant=File.Variant.THUMBNAIL)` + + for multiple versions: + `thumb_v2 = FileFactory(variant=File.Variant.THUMBNAIL, document=thumb.document)` + + + """ + + name = factory.Maybe( + factory.LazyAttribute(lambda o: o.variant == models.File.Variant.ORIGINAL), + yes_declaration=factory.Sequence(lambda n: "Version #%i" % ((int(n) + 1) / 2)), + no_declaration=factory.LazyAttribute( + lambda o: f"{o.original.name}_preview.jpg" + ), + ) document = SubFactory(DocumentFactory) variant = models.File.Variant.ORIGINAL content = factory.Maybe( diff --git a/alexandria/core/models.py b/alexandria/core/models.py index 4ee28465..a5d8f9e9 100644 --- a/alexandria/core/models.py +++ b/alexandria/core/models.py @@ -151,10 +151,10 @@ def clone(self): self.pk = None self.save() - # no need to clone the thumbnail, it will be auto-created - new_original = latest_original.clone() - new_original.document = self - new_original.save() + # TODO: decide on how to deal with thumbnail creation + latest_original.pk = None + latest_original.document = self + latest_original.save() return self diff --git a/alexandria/core/tests/test_models.py b/alexandria/core/tests/test_models.py index 3680347c..a1e18680 100644 --- a/alexandria/core/tests/test_models.py +++ b/alexandria/core/tests/test_models.py @@ -1,28 +1,17 @@ import pytest -from alexandria.core.factories import DocumentFactory, FileFactory from alexandria.core.models import File @pytest.mark.parametrize("admin_groups", [["foo"]]) -def test_clone_document(admin_client, minio_mock): - document = DocumentFactory() - - v1 = FileFactory(document=document, variant="original", name="v1") - FileFactory(document=document, variant="thumbnail", original=v1) - v2 = FileFactory(document=document, variant="original", name="v2") - FileFactory( - document=document, - variant="thumbnail", - original=v2, - name="thumbnail_file", - ) - - clone = document.clone() +def test_clone_document(admin_client, file_factory): + v1_thumb = file_factory(variant=File.Variant.THUMBNAIL) + v2_thumb = file_factory(variant=File.Variant.THUMBNAIL, document=v1_thumb.document) + clone = v1_thumb.document.clone() # in the test, the thumbnail for the cloned file is not created # because the minio hook is not called assert File.objects.count() == 5 assert list(clone.files.values_list("name", flat=True)) == [ - v2.name, + v2_thumb.document.files.filter(variant=File.Variant.ORIGINAL).first().name, ]