Configure STORAGES using OPTIONS instead of subclassing

This commit is contained in:
Bart Schuurmans 2024-03-29 22:22:33 +01:00
parent 4fa823e8df
commit b5ef9f6241
4 changed files with 37 additions and 61 deletions

View file

@ -1,9 +1,9 @@
# Generated by Django 3.2.23 on 2024-01-28 02:49 # Generated by Django 3.2.23 on 2024-01-28 02:49
import bookwyrm.storage_backends
import django.core.serializers.json import django.core.serializers.json
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
from django.core.files.storage import storages
class Migration(migrations.Migration): class Migration(migrations.Migration):
@ -30,7 +30,7 @@ class Migration(migrations.Migration):
name="export_data", name="export_data",
field=models.FileField( field=models.FileField(
null=True, null=True,
storage=bookwyrm.storage_backends.ExportsFileStorage, storage=storages["exports"],
upload_to="", upload_to="",
), ),
), ),

View file

@ -10,9 +10,9 @@ from django.db.models import BooleanField, FileField, JSONField
from django.db.models import Q from django.db.models import Q
from django.core.serializers.json import DjangoJSONEncoder from django.core.serializers.json import DjangoJSONEncoder
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from django.utils.module_loading import import_string from django.core.files.storage import storages
from bookwyrm import settings, storage_backends from bookwyrm import settings
from bookwyrm.models import AnnualGoal, ReadThrough, ShelfBook, ListItem from bookwyrm.models import AnnualGoal, ReadThrough, ShelfBook, ListItem
from bookwyrm.models import Review, Comment, Quotation from bookwyrm.models import Review, Comment, Quotation
@ -35,8 +35,7 @@ class BookwyrmAwsSession(BotoSession):
def select_exports_storage(): def select_exports_storage():
"""callable to allow for dependency on runtime configuration""" """callable to allow for dependency on runtime configuration"""
cls = import_string(settings.EXPORTS_STORAGE) return storages["exports"]
return cls()
class BookwyrmExportJob(ParentJob): class BookwyrmExportJob(ParentJob):
@ -116,7 +115,7 @@ def create_archive_task(job_id):
if settings.USE_S3: if settings.USE_S3:
# Storage for writing temporary files # Storage for writing temporary files
exports_storage = storage_backends.ExportsS3Storage() exports_storage = storages["exports"]
# Handle for creating the final archive # Handle for creating the final archive
s3_tar = S3Tar( s3_tar = S3Tar(
@ -136,7 +135,7 @@ def create_archive_task(job_id):
) )
# Add images to TAR # Add images to TAR
images_storage = storage_backends.ImagesStorage() images_storage = storages["default"]
if user.avatar: if user.avatar:
add_file_to_s3_tar(s3_tar, images_storage, user.avatar) add_file_to_s3_tar(s3_tar, images_storage, user.avatar)

View file

@ -392,13 +392,27 @@ if USE_S3:
# Storages # Storages
STORAGES = { STORAGES = {
"default": { "default": {
"BACKEND": "bookwyrm.storage_backends.ImagesStorage", "BACKEND": "storages.backends.s3.S3Storage",
"OPTIONS": {
"location": "images",
"default_acl": "public-read",
"file_overwrite": False,
},
}, },
"staticfiles": { "staticfiles": {
"BACKEND": "bookwyrm.storage_backends.StaticStorage", "BACKEND": "storages.backends.s3.S3Storage",
"OPTIONS": {
"location": "static",
"default_acl": "public-read",
},
}, },
"exports": { "exports": {
"BACKEND": "bookwyrm.storage_backends.ExportsS3Storage", "BACKEND": "storages.backends.s3.S3Storage",
"OPTIONS": {
"location": "images",
"default_acl": None,
"file_overwrite": False,
},
}, },
} }
# S3 Static settings # S3 Static settings
@ -431,10 +445,17 @@ elif USE_AZURE:
# Storages # Storages
STORAGES = { STORAGES = {
"default": { "default": {
"BACKEND": "bookwyrm.storage_backends.AzureImagesStorage", "BACKEND": "storages.backends.azure_storage.AzureStorage",
"OPTIONS": {
"location": "images",
"overwrite_files": False,
},
}, },
"staticfiles": { "staticfiles": {
"BACKEND": "bookwyrm.storage_backends.AzureStaticStorage", "BACKEND": "storages.backends.azure_storage.AzureStorage",
"OPTIONS": {
"location": "static",
},
}, },
"exports": { "exports": {
"BACKEND": None, # not implemented yet "BACKEND": None, # not implemented yet
@ -465,7 +486,10 @@ else:
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage", "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
}, },
"exports": { "exports": {
"BACKEND": "bookwyrm.storage_backends.ExportsFileStorage", "BACKEND": "django.core.files.storage.FileSystemStorage",
"OPTIONS": {
"location": "exports",
},
}, },
} }
# Static settings # Static settings

View file

@ -1,47 +0,0 @@
"""Handles backends for storages"""
from django.core.files.storage import FileSystemStorage
from storages.backends.s3 import S3Storage
from storages.backends.azure_storage import AzureStorage
class StaticStorage(S3Storage): # pylint: disable=abstract-method
"""Storage class for Static contents"""
location = "static"
default_acl = "public-read"
class ImagesStorage(S3Storage): # pylint: disable=abstract-method
"""Storage class for Image files"""
location = "images"
default_acl = "public-read"
file_overwrite = False
class AzureStaticStorage(AzureStorage): # pylint: disable=abstract-method
"""Storage class for Static contents"""
location = "static"
class AzureImagesStorage(AzureStorage): # pylint: disable=abstract-method
"""Storage class for Image files"""
location = "images"
overwrite_files = False
class ExportsFileStorage(FileSystemStorage): # pylint: disable=abstract-method
"""Storage class for exports contents with local files"""
location = "exports"
overwrite_files = False
class ExportsS3Storage(S3Storage): # pylint: disable=abstract-method
"""Storage class for exports contents with S3"""
location = "exports"
default_acl = None
overwrite_files = False