Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rend possible la mise à jour d'image avec un SVG #6207

Merged
merged 3 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions zds/gallery/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,12 @@ def perform_update(self, data):
raise ImageTooLarge(self.image.title, physical.size)

try:
ImagePIL.open(physical)
if not physical.name.endswith(".svg"):
ImagePIL.open(physical)
elif load_svg_file(physical) is None:
raise NotAnImage(physical)
except OSError:
raise NotAnImage(self.image.title)
raise NotAnImage(physical)

self.image.physical = physical

Expand Down
75 changes: 43 additions & 32 deletions zds/gallery/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def test_fail_member_no_permission_can_edit_image(self):

self.client.post(
reverse("gallery-image-edit", args=[self.gallery.pk, self.image.pk]),
{"title": "modify with no perms", "legend": "test legend", "slug": "test-slug", "physical": fp},
{"title": "modify with no perms", "legend": "test legend", "physical": fp},
follow=True,
)

Expand All @@ -525,23 +525,25 @@ def test_fail_member_no_permission_can_edit_image(self):
def test_success_member_edit_image(self):
self.client.force_login(self.profile1.user)

nb_files = len(os.listdir(self.gallery.get_gallery_path()))

with (settings.BASE_DIR / "fixtures" / "logo.png").open("rb") as fp:

response = self.client.post(
reverse("gallery-image-edit", args=[self.gallery.pk, self.image.pk]),
{"title": "edit title", "legend": "dit legend", "slug": "edit-slug", "physical": fp},
follow=True,
)
self.assertEqual(200, response.status_code)
self.assertEqual(nb_files + 3, len(os.listdir(self.gallery.get_gallery_path())))

image_test = Image.objects.get(pk=self.image.pk)
self.assertEqual("edit title", image_test.title)
image_test.delete()
# picture AND thumbnail should be gone
self.assertEqual(nb_files, len(os.listdir(self.gallery.get_gallery_path())))
for filename in (
settings.BASE_DIR / "fixtures" / "logo.png",
settings.BASE_DIR / "assets" / "licenses" / "copyright.svg",
):
with self.subTest(filename):
nb_files = len(os.listdir(self.gallery.get_gallery_path()))

with open(filename, "rb") as fp:
response = self.client.post(
reverse("gallery-image-edit", args=[self.gallery.pk, self.image.pk]),
{"title": "edit title", "legend": "dit legend", "physical": fp},
follow=True,
)
self.assertEqual(200, response.status_code)
# Check that 1 image and 2 thumbnails have been saved in the gallery
self.assertEqual(nb_files + 3, len(os.listdir(self.gallery.get_gallery_path())))

self.image.refresh_from_db()
self.assertEqual("edit title", self.image.title)

def test_access_permission(self):
self.client.force_login(self.profile1.user)
Expand Down Expand Up @@ -603,6 +605,7 @@ def test_delete_image_from_other_user(self):

def test_success_delete_image_write_permission(self):
self.client.force_login(self.profile1.user)
nb_files = len(os.listdir(self.gallery1.get_gallery_path()))

response = self.client.post(
reverse("gallery-image-delete", kwargs={"pk_gallery": self.gallery1.pk}),
Expand All @@ -613,6 +616,9 @@ def test_success_delete_image_write_permission(self):

self.assertEqual(0, Image.objects.filter(pk=self.image1.pk).count())

# picture AND thumbnails should be gone
self.assertEqual(nb_files, len(os.listdir(self.gallery1.get_gallery_path())))

def test_success_delete_list_images_write_permission(self):
self.client.force_login(self.profile1.user)

Expand Down Expand Up @@ -652,17 +658,22 @@ def test_success_new_image_write_permission(self):
self.client.force_login(self.profile1.user)
self.assertEqual(0, len(self.gallery.get_images()))

with (settings.BASE_DIR / "fixtures" / "logo.png").open("rb") as fp:
response = self.client.post(
reverse("gallery-image-new", args=[self.gallery.pk]),
{"title": "Test title", "legend": "Test legend", "slug": "test-slug", "physical": fp},
follow=True,
)

self.assertEqual(200, response.status_code)
self.assertEqual(1, len(self.gallery.get_images()))
self.assertEqual(3, len(os.listdir(self.gallery.get_gallery_path()))) # New image and thumbnail
self.gallery.get_images()[0].delete()
for filename in (
settings.BASE_DIR / "fixtures" / "logo.png",
settings.BASE_DIR / "assets" / "licenses" / "copyright.svg",
):
with self.subTest(filename):
with open(filename, "rb") as fp:
response = self.client.post(
reverse("gallery-image-new", args=[self.gallery.pk]),
{"title": "Test title", "legend": "Test legend", "physical": fp},
follow=True,
)

self.assertEqual(200, response.status_code)
self.assertEqual(1, len(self.gallery.get_images()))
self.assertEqual(3, len(os.listdir(self.gallery.get_gallery_path()))) # New image and thumbnail
self.gallery.get_images()[0].delete()

def test_fail_new_image_with_read_permission(self):
self.client.force_login(self.profile2.user)
Expand All @@ -671,7 +682,7 @@ def test_fail_new_image_with_read_permission(self):
with (settings.BASE_DIR / "fixtures" / "logo.png").open("rb") as fp:
response = self.client.post(
reverse("gallery-image-new", args=[self.gallery.pk]),
{"title": "Test title", "legend": "Test legend", "slug": "test-slug", "physical": fp},
{"title": "Test title", "legend": "Test legend", "physical": fp},
follow=True,
)

Expand All @@ -685,7 +696,7 @@ def test_fail_new_image_with_no_permission(self):
with (settings.BASE_DIR / "fixtures" / "logo.png").open("rb") as fp:
response = self.client.post(
reverse("gallery-image-new", args=[self.gallery.pk]),
{"title": "Test title", "legend": "Test legend", "slug": "test-slug", "physical": fp},
{"title": "Test title", "legend": "Test legend", "physical": fp},
follow=True,
)

Expand All @@ -698,7 +709,7 @@ def test_fail_gallery_not_exist(self):
with (settings.BASE_DIR / "fixtures" / "logo.png").open("rb") as fp:
response = self.client.post(
reverse("gallery-image-new", args=[156]),
{"title": "Test title", "legend": "Test legend", "slug": "test-slug", "physical": fp},
{"title": "Test title", "legend": "Test legend", "physical": fp},
follow=True,
)

Expand Down