2
0
mirror of https://github.com/inventree/InvenTree.git synced 2026-04-07 03:50:52 +00:00

Storage fixes (#11672)

* Use storage class rather than manually constructing URL

* Fix for report helpers
This commit is contained in:
Oliver
2026-04-04 11:49:21 +11:00
committed by GitHub
parent bb3293ef31
commit 3a1e860789
2 changed files with 7 additions and 15 deletions

View File

@@ -1,14 +1,13 @@
"""Serializers used in various InvenTree apps.""" """Serializers used in various InvenTree apps."""
import os
from collections import OrderedDict from collections import OrderedDict
from copy import deepcopy from copy import deepcopy
from decimal import Decimal from decimal import Decimal
from typing import Any, Optional from typing import Any, Optional
from django.conf import settings
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError as DjangoValidationError from django.core.exceptions import ValidationError as DjangoValidationError
from django.core.files.storage import default_storage
from django.db import models from django.db import models
from django.db.models import QuerySet from django.db.models import QuerySet
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@@ -33,8 +32,6 @@ from InvenTree.fields import InvenTreeRestURLField, InvenTreeURLField
from InvenTree.helpers import str2bool from InvenTree.helpers import str2bool
from InvenTree.helpers_model import getModelsWithMixin from InvenTree.helpers_model import getModelsWithMixin
from .setting.storages import StorageBackends
# region path filtering # region path filtering
class FilterableSerializerField: class FilterableSerializerField:
@@ -689,9 +686,7 @@ class InvenTreeAttachmentSerializerField(serializers.FileField):
if not value: if not value:
return None return None
if settings.STORAGE_TARGET == StorageBackends.S3: return default_storage.url(str(value))
return str(value.url)
return os.path.join(str(settings.MEDIA_URL), str(value))
class InvenTreeImageSerializerField(serializers.ImageField): class InvenTreeImageSerializerField(serializers.ImageField):
@@ -705,9 +700,7 @@ class InvenTreeImageSerializerField(serializers.ImageField):
if not value: if not value:
return None return None
if settings.STORAGE_TARGET == StorageBackends.S3: return default_storage.url(str(value))
return str(value.url)
return os.path.join(str(settings.MEDIA_URL), str(value))
class InvenTreeDecimalField(serializers.FloatField): class InvenTreeDecimalField(serializers.FloatField):

View File

@@ -2,7 +2,6 @@
import base64 import base64
import logging import logging
import os
from datetime import date, datetime from datetime import date, datetime
from decimal import Decimal, InvalidOperation from decimal import Decimal, InvalidOperation
from io import BytesIO from io import BytesIO
@@ -11,7 +10,6 @@ from typing import Any, Optional
from django import template from django import template
from django.apps.registry import apps from django.apps.registry import apps
from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.exceptions import SuspiciousFileOperation, ValidationError from django.core.exceptions import SuspiciousFileOperation, ValidationError
from django.core.files.storage import default_storage from django.core.files.storage import default_storage
@@ -292,7 +290,7 @@ def asset(filename: str, raise_error: bool = False) -> str | None:
# In debug mode, return a web URL to the asset file (rather than a local file path) # In debug mode, return a web URL to the asset file (rather than a local file path)
if get_global_setting('REPORT_DEBUG_MODE', cache=False): if get_global_setting('REPORT_DEBUG_MODE', cache=False):
return str(Path(settings.MEDIA_URL, 'report', 'assets', filename)) return default_storage.url(str(full_path))
storage_path = default_storage.path(str(full_path)) storage_path = default_storage.path(str(full_path))
@@ -368,8 +366,9 @@ def uploaded_image(
if debug_mode: if debug_mode:
# In debug mode, return a web path (rather than an encoded image blob) # In debug mode, return a web path (rather than an encoded image blob)
if exists: if exists:
return os.path.join(settings.MEDIA_URL, filename) return default_storage.url(filename)
return os.path.join(settings.STATIC_URL, 'img', replacement_file)
return staticfiles_storage.url(str(Path('img', replacement_file)))
if img_data: if img_data:
img = Image.open(BytesIO(img_data)) img = Image.open(BytesIO(img_data))