From b5ef9f62410e86f29ca3a22b6e756ad95c3a1ec1 Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Fri, 29 Mar 2024 22:22:33 +0100 Subject: [PATCH] Configure STORAGES using OPTIONS instead of subclassing --- .../migrations/0193_auto_20240128_0249.py | 4 +- bookwyrm/models/bookwyrm_export_job.py | 11 ++--- bookwyrm/settings.py | 36 +++++++++++--- bookwyrm/storage_backends.py | 47 ------------------- 4 files changed, 37 insertions(+), 61 deletions(-) delete mode 100644 bookwyrm/storage_backends.py diff --git a/bookwyrm/migrations/0193_auto_20240128_0249.py b/bookwyrm/migrations/0193_auto_20240128_0249.py index c1c0527b9..82e32ee48 100644 --- a/bookwyrm/migrations/0193_auto_20240128_0249.py +++ b/bookwyrm/migrations/0193_auto_20240128_0249.py @@ -1,9 +1,9 @@ # Generated by Django 3.2.23 on 2024-01-28 02:49 -import bookwyrm.storage_backends import django.core.serializers.json from django.db import migrations, models import django.db.models.deletion +from django.core.files.storage import storages class Migration(migrations.Migration): @@ -30,7 +30,7 @@ class Migration(migrations.Migration): name="export_data", field=models.FileField( null=True, - storage=bookwyrm.storage_backends.ExportsFileStorage, + storage=storages["exports"], upload_to="", ), ), diff --git a/bookwyrm/models/bookwyrm_export_job.py b/bookwyrm/models/bookwyrm_export_job.py index da79de6a8..f355c86a4 100644 --- a/bookwyrm/models/bookwyrm_export_job.py +++ b/bookwyrm/models/bookwyrm_export_job.py @@ -10,9 +10,9 @@ from django.db.models import BooleanField, FileField, JSONField from django.db.models import Q from django.core.serializers.json import DjangoJSONEncoder 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 Review, Comment, Quotation @@ -35,8 +35,7 @@ class BookwyrmAwsSession(BotoSession): def select_exports_storage(): """callable to allow for dependency on runtime configuration""" - cls = import_string(settings.EXPORTS_STORAGE) - return cls() + return storages["exports"] class BookwyrmExportJob(ParentJob): @@ -116,7 +115,7 @@ def create_archive_task(job_id): if settings.USE_S3: # Storage for writing temporary files - exports_storage = storage_backends.ExportsS3Storage() + exports_storage = storages["exports"] # Handle for creating the final archive s3_tar = S3Tar( @@ -136,7 +135,7 @@ def create_archive_task(job_id): ) # Add images to TAR - images_storage = storage_backends.ImagesStorage() + images_storage = storages["default"] if user.avatar: add_file_to_s3_tar(s3_tar, images_storage, user.avatar) diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py index 0ad403ce5..61b45c423 100644 --- a/bookwyrm/settings.py +++ b/bookwyrm/settings.py @@ -392,13 +392,27 @@ if USE_S3: # Storages STORAGES = { "default": { - "BACKEND": "bookwyrm.storage_backends.ImagesStorage", + "BACKEND": "storages.backends.s3.S3Storage", + "OPTIONS": { + "location": "images", + "default_acl": "public-read", + "file_overwrite": False, + }, }, "staticfiles": { - "BACKEND": "bookwyrm.storage_backends.StaticStorage", + "BACKEND": "storages.backends.s3.S3Storage", + "OPTIONS": { + "location": "static", + "default_acl": "public-read", + }, }, "exports": { - "BACKEND": "bookwyrm.storage_backends.ExportsS3Storage", + "BACKEND": "storages.backends.s3.S3Storage", + "OPTIONS": { + "location": "images", + "default_acl": None, + "file_overwrite": False, + }, }, } # S3 Static settings @@ -431,10 +445,17 @@ elif USE_AZURE: # Storages STORAGES = { "default": { - "BACKEND": "bookwyrm.storage_backends.AzureImagesStorage", + "BACKEND": "storages.backends.azure_storage.AzureStorage", + "OPTIONS": { + "location": "images", + "overwrite_files": False, + }, }, "staticfiles": { - "BACKEND": "bookwyrm.storage_backends.AzureStaticStorage", + "BACKEND": "storages.backends.azure_storage.AzureStorage", + "OPTIONS": { + "location": "static", + }, }, "exports": { "BACKEND": None, # not implemented yet @@ -465,7 +486,10 @@ else: "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage", }, "exports": { - "BACKEND": "bookwyrm.storage_backends.ExportsFileStorage", + "BACKEND": "django.core.files.storage.FileSystemStorage", + "OPTIONS": { + "location": "exports", + }, }, } # Static settings diff --git a/bookwyrm/storage_backends.py b/bookwyrm/storage_backends.py deleted file mode 100644 index 1695daa67..000000000 --- a/bookwyrm/storage_backends.py +++ /dev/null @@ -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