Skip to content

Commit

Permalink
feat: storage backend definition could be set by platform settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekenhei committed Nov 12, 2024
1 parent 5e57ab6 commit 57e981c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion edx_sga/sga.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.core.files import File
from django.core.files.storage import default_storage
from django.template import Context, Template
from django.utils.encoding import force_text
from django.utils.timezone import now as django_now
Expand All @@ -44,6 +43,7 @@
from edx_sga.showanswer import ShowAnswerXBlockMixin
from edx_sga.tasks import get_zip_file_name, get_zip_file_path, zip_student_submissions
from edx_sga.utils import (
default_storage,
file_contents_iter,
get_file_modified_time_utc,
get_file_storage_path,
Expand Down
26 changes: 25 additions & 1 deletion edx_sga/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import pytz
from django.conf import settings
from django.core.files.storage import default_storage
from django.core.files.storage import default_storage as django_default_storage, get_storage_class
from edx_sga.constants import BLOCK_SIZE


Expand Down Expand Up @@ -76,3 +76,27 @@ def file_contents_iter(file_path):
"""
file_descriptor = default_storage.open(file_path)
return iter(partial(file_descriptor.read, BLOCK_SIZE), b"")

def get_default_storage():
"""
Get config for storage from settings, use Django's default_storage if no such settings are defined
"""
# .. setting_name: SGA_STORAGE_SETTINGS
# .. setting_default: {}
# .. setting_description: Specifies the storage class and keyword arguments to use in the constructor
# Default storage will be used if this settings in not specified.
# .. setting_example: {
# STORAGE_CLASS: 'storage',
# STORAGE_KWARGS: {}
# }
sga_storage_settings = getattr(settings, "SGA_STORAGE_SETTINGS", None)

if sga_storage_settings:
return get_storage_class(
sga_storage_settings['STORAGE_CLASS']
)(**sga_storage_settings['STORAGE_KWARGS'])

# If settings not defined, use default_storage from Django
return django_default_storage

default_storage = get_default_storage()

0 comments on commit 57e981c

Please sign in to comment.