Skip to content

Commit

Permalink
dashboard: save duplicate image as new instance
Browse files Browse the repository at this point in the history
  • Loading branch information
m4ra committed Feb 12, 2024
1 parent 9b32619 commit 0ea35c3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
23 changes: 23 additions & 0 deletions adhocracy4/dashboard/mixins.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from copy import deepcopy
from pathlib import Path

from django.apps import apps
from django.conf import settings
from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.core.files.base import ContentFile
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect
from django.urls import NoReverseMatch
Expand Down Expand Up @@ -170,7 +172,28 @@ def post(self, request, *args, **kwargs):
project_clone.created = timezone.now()
project_clone.is_draft = True
project_clone.is_archived = False

if project.tile_image:
# django's image.name contains the file storage location defined in 'upload_to'
tile_file_name = Path(
project.tile_image.name
).name # retreives only the filename
tile_copy = ContentFile(
project.tile_image.read()
) # retreives the image bytes
project_clone.tile_image.save(tile_file_name, tile_copy, False)

if project.image:
# django's image.name contains the file storage location defined in 'upload_to'
image_file_name = Path(project.image.name).name
image_copy = ContentFile(project.image.read())
project_clone.image.save(image_file_name, image_copy, False)

project_clone.save()
if project.topics:
for topic in project.topics.all():
project_clone.topics.add(topic)

signals.project_created.send(
sender=None, project=project_clone, user=self.request.user
)
Expand Down
3 changes: 3 additions & 0 deletions changelog/1528.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Added

- retreival of image name to save as new image instance when project is duplicated
35 changes: 26 additions & 9 deletions tests/dashboard/test_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib
from pathlib import Path

import pytest
from django.test.utils import override_settings
Expand Down Expand Up @@ -257,7 +258,7 @@ def test_project_publish_redirect(client, project, another_user):

@pytest.mark.django_db
def test_project_duplicate(
client, another_user, area_settings, phase_factory, image_factory
client, another_user, area_settings, phase_factory, image_factory, topic_factory
):
module = area_settings.module
project = module.project
Expand All @@ -272,6 +273,7 @@ def test_project_duplicate(

project.tile_image = test_tile_image
project.image = test_image
topic_factory(projects=[project])
project.save()

project_list_url = reverse(
Expand All @@ -291,14 +293,24 @@ def test_project_duplicate(

assert project_clone.is_draft is True
assert project_clone.is_archived is False

assert project_clone.image == project.image
assert project_clone.image.name == project.image.name
assert project_clone.image.path == project.image.path

assert project_clone.tile_image == project.tile_image
assert project_clone.tile_image.name == project.tile_image.name
assert project_clone.tile_image.path == project.tile_image.path
assert project_clone.topics.all().exists()
assert len(project_clone.topics.all()) == len(project.topics.all())

assert project_clone.image
# django saves a new instance of the image by extending the original name
assert Path(project.image.name).stem in Path(project_clone.image.name).stem
assert len(project_clone.image.name) > len(project.image.name)
assert Path(project_clone.image.path).parent == Path(project.image.path).parent

assert project_clone.tile_image
assert (
Path(project.tile_image.name).stem in Path(project_clone.tile_image.name).stem
)
assert len(project_clone.tile_image.name) > len(project.tile_image.name)
assert (
Path(project_clone.tile_image.path).parent
== Path(project.tile_image.path).parent
)

assert project_clone.created > project.created
for attr in ("description", "information", "result", "access"):
Expand All @@ -319,3 +331,8 @@ def test_project_duplicate(
area_settings_clone = module_clone.settings_instance
assert area_settings_clone.pk != area_settings.pk
assert area_settings_clone.polygon == area_settings.polygon

# check if original project is deleted, the copied image remains
project.delete()
project_clone.refresh_from_db()
assert project_clone.tile_image.path is not None

0 comments on commit 0ea35c3

Please sign in to comment.