Skip to content

Commit f7b917f

Browse files
Fixed asset images having an extraneous period (#2777)
1 parent 05cd80d commit f7b917f

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

importer/tasks.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,17 +450,22 @@ def import_item(self, import_item):
450450

451451
for idx, asset_url in enumerate(asset_urls, start=1):
452452
asset_title = f"{import_item.item.item_id}-{idx}"
453+
file_extension = (
454+
os.path.splitext(urlparse(asset_url).path)[1].lstrip(".").lower()
455+
)
453456
item_asset = Asset(
454457
item=import_item.item,
455458
campaign=import_item.item.project.campaign,
456459
title=asset_title,
457460
slug=slugify(asset_title, allow_unicode=True),
458461
sequence=idx,
459-
media_url=f"{idx}.jpg",
462+
media_url=f"{idx}.{file_extension}",
460463
media_type=MediaType.IMAGE,
461464
download_url=asset_url,
462465
resource_url=item_resource_url,
463-
storage_image="/".join([relative_asset_file_path, f"{idx}.jpg"]),
466+
storage_image="/".join(
467+
[relative_asset_file_path, f"{idx}.{file_extension}"]
468+
),
464469
)
465470
# Previously, any asset that raised a validation error was just ignored.
466471
# We don't want that--we want to see if an asset fails validation
@@ -602,7 +607,9 @@ def download_asset(self, job):
602607
download_url = job.url
603608
else:
604609
download_url = asset.download_url
605-
file_extension = os.path.splitext(urlparse(download_url).path)[1].lower()
610+
file_extension = (
611+
os.path.splitext(urlparse(download_url).path)[1].lstrip(".").lower()
612+
)
606613
if not file_extension or file_extension == "jpeg":
607614
file_extension = "jpg"
608615

@@ -617,6 +624,7 @@ def download_asset(self, job):
617624
asset.id,
618625
)
619626
asset.storage_image = storage_image
627+
asset.media_url = os.path.basename(storage_image)
620628
asset.save()
621629

622630

importer/tests/test_tasks.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import concurrent.futures
2+
import os.path
23
import uuid
34
from unittest import mock
45

@@ -805,6 +806,10 @@ def test_download_asset_valid(self):
805806

806807
self.assertEqual(get_mock.call_args[0], ("http://example.com",))
807808
self.assertTrue(get_mock.call_args[1]["stream"])
809+
self.assertEqual(
810+
os.path.basename(self.import_asset.asset.storage_image.path),
811+
self.import_asset.asset.media_url,
812+
)
808813

809814
@override_settings(
810815
STORAGES={"default": {"BACKEND": "django.core.files.storage.InMemoryStorage"}},
@@ -1019,7 +1024,7 @@ def test_valid_file_extension(self, mock_logger, mock_download):
10191024
mock_download.return_value = "stored_image.png"
10201025
tasks.download_asset(self.task_mock, self.job)
10211026

1022-
asset_image_filename = self.asset.get_asset_image_filename(".png")
1027+
asset_image_filename = self.asset.get_asset_image_filename("png")
10231028
mock_download.assert_called_once_with(
10241029
self.asset.download_url, asset_image_filename
10251030
)

0 commit comments

Comments
 (0)