mirror of
https://github.com/inventree/InvenTree.git
synced 2025-11-30 09:20:03 +00:00
[bug] Media url fix (#10855)
* Bug fix for getMediaUrl - Proper check for file type - Fix "fully_qualified_url" for Attachment model * Add unit test * Fix typo * Fix unit test --------- Co-authored-by: Matthias Mair <code@mjmair.com>
This commit is contained in:
@@ -16,7 +16,7 @@ from django.conf import settings
|
|||||||
from django.contrib.staticfiles.storage import StaticFilesStorage
|
from django.contrib.staticfiles.storage import StaticFilesStorage
|
||||||
from django.core.exceptions import FieldError, ValidationError
|
from django.core.exceptions import FieldError, ValidationError
|
||||||
from django.core.files.storage import default_storage
|
from django.core.files.storage import default_storage
|
||||||
from django.db.models.fields.files import ImageFieldFile
|
from django.db.models.fields.files import FieldFile, ImageFieldFile
|
||||||
from django.http import StreamingHttpResponse
|
from django.http import StreamingHttpResponse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
@@ -176,10 +176,14 @@ def constructPathString(path: list[str], max_chars: int = 250) -> str:
|
|||||||
return pathstring
|
return pathstring
|
||||||
|
|
||||||
|
|
||||||
def getMediaUrl(file: StdImageFieldFile | ImageFieldFile, name: str | None = None):
|
def getMediaUrl(
|
||||||
|
file: FieldFile | ImageFieldFile | StdImageFieldFile, name: str | None = None
|
||||||
|
):
|
||||||
"""Return the qualified access path for the given file, under the media directory."""
|
"""Return the qualified access path for the given file, under the media directory."""
|
||||||
if not isinstance(file, StdImageFieldFile):
|
if not isinstance(file, (FieldFile, ImageFieldFile, StdImageFieldFile)):
|
||||||
raise ValueError('file_obj must be an instance of StdImageFieldFile')
|
raise TypeError(
|
||||||
|
'file must be one of FileField, ImageFileField, StdImageFieldFile'
|
||||||
|
)
|
||||||
if name is not None:
|
if name is not None:
|
||||||
file = regenerate_imagefile(file, name)
|
file = regenerate_imagefile(file, name)
|
||||||
if settings.STORAGE_TARGET == StorageBackends.S3:
|
if settings.STORAGE_TARGET == StorageBackends.S3:
|
||||||
|
|||||||
@@ -702,7 +702,7 @@ class TestHelpers(TestCase):
|
|||||||
def testMediaUrl(self):
|
def testMediaUrl(self):
|
||||||
"""Test getMediaUrl."""
|
"""Test getMediaUrl."""
|
||||||
# Str should not work
|
# Str should not work
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(TypeError):
|
||||||
helpers.getMediaUrl('xx/yy.png') # type: ignore
|
helpers.getMediaUrl('xx/yy.png') # type: ignore
|
||||||
|
|
||||||
# Correct usage
|
# Correct usage
|
||||||
|
|||||||
@@ -2034,7 +2034,7 @@ class Attachment(InvenTree.models.MetadataMixin, InvenTree.models.InvenTreeModel
|
|||||||
if self.attachment:
|
if self.attachment:
|
||||||
import InvenTree.helpers_model
|
import InvenTree.helpers_model
|
||||||
|
|
||||||
media_url = InvenTree.helpers.getMediaUrl(self.attachment.url)
|
media_url = InvenTree.helpers.getMediaUrl(self.attachment)
|
||||||
return InvenTree.helpers_model.construct_absolute_url(media_url)
|
return InvenTree.helpers_model.construct_absolute_url(media_url)
|
||||||
|
|
||||||
return ''
|
return ''
|
||||||
|
|||||||
@@ -202,6 +202,21 @@ class AttachmentTest(InvenTreeAPITestCase):
|
|||||||
self.assignRole('part.delete')
|
self.assignRole('part.delete')
|
||||||
self.delete(url, expected_code=204)
|
self.delete(url, expected_code=204)
|
||||||
|
|
||||||
|
def test_fully_qualified_url(self):
|
||||||
|
"""Test that the fully qualified URL is returned correctly."""
|
||||||
|
part = Part.objects.first()
|
||||||
|
|
||||||
|
attachment = Attachment.objects.create(
|
||||||
|
attachment=self.generate_file('test.txt'),
|
||||||
|
comment='Testing filename: test.txt',
|
||||||
|
model_type='part',
|
||||||
|
model_id=part.pk,
|
||||||
|
)
|
||||||
|
|
||||||
|
url = attachment.fully_qualified_url()
|
||||||
|
self.assertIs(type(url), str)
|
||||||
|
self.assertIn(f'/media/attachments/part/{part.pk}/test', url)
|
||||||
|
|
||||||
|
|
||||||
class SettingsTest(InvenTreeTestCase):
|
class SettingsTest(InvenTreeTestCase):
|
||||||
"""Tests for the 'settings' model."""
|
"""Tests for the 'settings' model."""
|
||||||
|
|||||||
Reference in New Issue
Block a user