Skip to content

Commit

Permalink
fix(document): adjust cloning for use with storage backends
Browse files Browse the repository at this point in the history
NOTE: no automatic thumbnailgeneration
  • Loading branch information
fugal-dy committed Dec 14, 2023
1 parent 922f55a commit 26c4331
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
21 changes: 20 additions & 1 deletion alexandria/core/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions alexandria/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 5 additions & 16 deletions alexandria/core/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -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,
]

0 comments on commit 26c4331

Please sign in to comment.