mirror of
https://github.com/inventree/InvenTree.git
synced 2026-04-06 11:31:04 +00:00
Storage fixes (#11672)
* Use storage class rather than manually constructing URL * Fix for report helpers
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
"""Serializers used in various InvenTree apps."""
|
||||
|
||||
import os
|
||||
from collections import OrderedDict
|
||||
from copy import deepcopy
|
||||
from decimal import Decimal
|
||||
from typing import Any, Optional
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.exceptions import ValidationError as DjangoValidationError
|
||||
from django.core.files.storage import default_storage
|
||||
from django.db import models
|
||||
from django.db.models import QuerySet
|
||||
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_model import getModelsWithMixin
|
||||
|
||||
from .setting.storages import StorageBackends
|
||||
|
||||
|
||||
# region path filtering
|
||||
class FilterableSerializerField:
|
||||
@@ -689,9 +686,7 @@ class InvenTreeAttachmentSerializerField(serializers.FileField):
|
||||
if not value:
|
||||
return None
|
||||
|
||||
if settings.STORAGE_TARGET == StorageBackends.S3:
|
||||
return str(value.url)
|
||||
return os.path.join(str(settings.MEDIA_URL), str(value))
|
||||
return default_storage.url(str(value))
|
||||
|
||||
|
||||
class InvenTreeImageSerializerField(serializers.ImageField):
|
||||
@@ -705,9 +700,7 @@ class InvenTreeImageSerializerField(serializers.ImageField):
|
||||
if not value:
|
||||
return None
|
||||
|
||||
if settings.STORAGE_TARGET == StorageBackends.S3:
|
||||
return str(value.url)
|
||||
return os.path.join(str(settings.MEDIA_URL), str(value))
|
||||
return default_storage.url(str(value))
|
||||
|
||||
|
||||
class InvenTreeDecimalField(serializers.FloatField):
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
import base64
|
||||
import logging
|
||||
import os
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal, InvalidOperation
|
||||
from io import BytesIO
|
||||
@@ -11,7 +10,6 @@ from typing import Any, Optional
|
||||
|
||||
from django import template
|
||||
from django.apps.registry import apps
|
||||
from django.conf import settings
|
||||
from django.contrib.staticfiles.storage import staticfiles_storage
|
||||
from django.core.exceptions import SuspiciousFileOperation, ValidationError
|
||||
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)
|
||||
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))
|
||||
|
||||
@@ -368,8 +366,9 @@ def uploaded_image(
|
||||
if debug_mode:
|
||||
# In debug mode, return a web path (rather than an encoded image blob)
|
||||
if exists:
|
||||
return os.path.join(settings.MEDIA_URL, filename)
|
||||
return os.path.join(settings.STATIC_URL, 'img', replacement_file)
|
||||
return default_storage.url(filename)
|
||||
|
||||
return staticfiles_storage.url(str(Path('img', replacement_file)))
|
||||
|
||||
if img_data:
|
||||
img = Image.open(BytesIO(img_data))
|
||||
|
||||
Reference in New Issue
Block a user