-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
admin: Fix AnnouncementItemForm image update
When submitting the form without replacing the image file, the filename of the previous image would be replaced to something invalid. Teach AnnouncementItemFactory how to create the File record to keep the test data realistic. Leave replaced files in tact for future reference Revert "Leave replaced files in tact for future reference" This reverts commit 4c8fc35. Soft delete AnnouncementItem image files when replacing them Replace File.deleted with deleted_at
- Loading branch information
1 parent
8f0779f
commit 5abcf43
Showing
8 changed files
with
129 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 5.1.3 on 2024-11-26 09:38 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("files", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="file", | ||
name="deleted_at", | ||
field=models.DateTimeField( | ||
help_text="Marqué pour suppression du stockage", null=True, verbose_name="supprimé le" | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import io | ||
import pathlib | ||
import uuid | ||
|
||
import pytest | ||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
from django.urls import reverse | ||
from PIL import Image | ||
from pytest_django.asserts import assertRedirects | ||
|
||
from itou.communications.models import AnnouncementCampaign | ||
from itou.files.models import File | ||
from tests.communications.factories import AnnouncementItemFactory | ||
|
||
|
||
class TestAnnouncementItemAdmin: | ||
@pytest.fixture | ||
def black_pixel(self): | ||
with io.BytesIO() as buf: | ||
image = Image.new(mode="RGB", size=(1, 1), color=(0, 0, 0)) | ||
image.save(buf, format="png") | ||
buf.seek(0) | ||
yield buf.getvalue() | ||
|
||
def test_add_image(self, admin_client, black_pixel): | ||
response = admin_client.post( | ||
reverse("admin:communications_announcementcampaign_add"), | ||
{ | ||
"max_items": 3, | ||
"start_date": "01/11/2024", | ||
"live": "on", | ||
"items-TOTAL_FORMS": "1", | ||
"items-INITIAL_FORMS": "0", | ||
"items-0-priority": "0", | ||
"items-0-title": "Bla", | ||
"items-0-description": "Ho", | ||
"items-0-image": SimpleUploadedFile("my-image.png", black_pixel, content_type="image/png"), | ||
"items-0-image_alt_text": "aaa", | ||
"items-0-link": "", | ||
"_save": "Enregistrer", | ||
}, | ||
) | ||
assertRedirects(response, reverse("admin:communications_announcementcampaign_changelist")) | ||
campaign = AnnouncementCampaign.objects.get() | ||
[item] = campaign.items.all() | ||
filename = pathlib.Path(item.image.name) | ||
assert uuid.UUID(filename.stem) # Did not use the provided filename | ||
assert filename.suffix == ".png" | ||
file = File.objects.get() | ||
assert file.key.startswith("news-images/") | ||
|
||
def test_change_image(self, admin_client, black_pixel): | ||
item = AnnouncementItemFactory(with_image=True) | ||
url = reverse("admin:communications_announcementcampaign_change", args=(item.campaign_id,)) | ||
response = admin_client.post( | ||
url, | ||
{ | ||
"max_items": 3, | ||
"start_date": "01/11/2024", | ||
"live": "on", | ||
"items-TOTAL_FORMS": "1", | ||
"items-INITIAL_FORMS": "1", | ||
"items-0-id": f"{item.pk}", | ||
"items-0-priority": "0", | ||
"items-0-title": "Bla", | ||
"items-0-description": "Ho", | ||
"items-0-image": SimpleUploadedFile("my-image.png", black_pixel, content_type="image/png"), | ||
"items-0-image_alt_text": "aaa", | ||
"items-0-link": "", | ||
"_save": "Enregistrer", | ||
}, | ||
) | ||
assertRedirects(response, reverse("admin:communications_announcementcampaign_changelist")) | ||
item.refresh_from_db() | ||
filename = pathlib.Path(item.image.name) | ||
assert uuid.UUID(filename.stem) # Did not use the provided filename | ||
assert filename.suffix == ".png" | ||
assert File.objects.filter(deleted_at__isnull=False).count() == 1 | ||
assert item.image_storage.deleted_at is None | ||
assert item.image_storage.key.startswith("news-images/") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters