fabiocaccamo / django-extra-settings

:gear: config and manage typed extra settings using just the django admin.
MIT License
534 stars 32 forks source link

File URLs incorrect when using a remote storage #147

Closed perepicornell closed 5 months ago

perepicornell commented 6 months ago

Python version 3.9.12

Django version 3.2.14

Package version Freshly installed today

Current behavior (bug description)

Using boto3 to have the media files uploaded to an S3 provider, in File or Image type settings the uploading works fine, but then in the list view the link to the files are like this: http://localhost:5001/files/test_file.png

Which is wrong.

Expected behavior

The file's link should be, in this case, the one generated by boto3. But, in general, should honor the storage backend configuration.

Ideas

I believe it could be solved by specifying the storage backend in the file fields, maybe something like this:

class Setting(..):
    value_file = models.FileField(
        blank=True,
        upload_to=fields.upload_to_files,
        verbose_name=_("Value"),
        storage=fields.use_storage_backend,
    )

# and in fields module:
def use_storage_backend(obj, filename):
    """Provide storage backend for a file."""
    return _upload_to(settings.EXTRA_SETTINGS_USE_STORAGE_BACKEND, filename)

This approach will not honor the default storage settings, but in my case, I never change it. Instead, I specify the storage for each field.

The fact that now I made it work by setting

STORAGES = {
    "default": {
        "BACKEND": "app_name.storage_backends.PublicMediaStorage",
    },
}

Is only because that's the only way I had to make extra_settings upload a file using boto3.

Final note just in case: this is not related to the other issue I posted about grappelli. Everything I tested while writing this issue was whilst having grappelli disabled.

I might not be able to adopt this library (depending on what happens with the grappelli issue) but still, I thought you might find it useful to know.

Thanks again :blush:

Upvote & Fund

Fund with Polar

fabiocaccamo commented 6 months ago

@perepicornell thank you for reporting this.

Actually the ImageField / FileField use the default storage.

An improvement could be to add two new settings for specifying custom storage for both image and file fields, the image widget you see in the admin is the django one, it doesn't get changed by this app, so I'm not sure that supporting these new custom settings will solve this issue.

perepicornell commented 6 months ago

Hi, just to clarify, changing the storage backend won't modify the file widget, or at least, my experience with django_storages + boto3 is that with or without changing the storage backend everything looks the same. The only change that can be perceived at the front-end is that the link to the currently uploaded file will point to some long URL in your S3 provider, instead of the path specified in the MEDIA_URL.

fabiocaccamo commented 5 months ago

@perepicornell this library uses the default storage backend by default and don't customize its behavior at all, nor in the upload, nor in the admin.

So, if something is not working as expected, there is probably something that has not been configured correctly on your side (maybe you had already uploaded the file/image before having configured the S3 storage?).

Anyway, adding a new setting for defining the storage backend to use (instead of the default one), could be a useful feature.