Skip to content

Commit

Permalink
Merge pull request #19 from open-craft/maxim/add-alternative-storage
Browse files Browse the repository at this point in the history
feat: allow specifying an alternative storage through django settings
  • Loading branch information
ziafazal authored Apr 5, 2023
2 parents bdc7871 + ffb9c85 commit d89bcb6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
10 changes: 5 additions & 5 deletions h5pxblock/h5pxblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import pkg_resources

from django.conf import settings
from django.core.files.storage import default_storage
from django.template import Context, Template
from django.utils import timezone
from webob import Response
Expand All @@ -15,7 +14,7 @@
from xblock.core import XBlock
from xblock.fields import Scope, String, Boolean, Integer, Dict, DateTime, UNIQUE_ID
from xblock.fragment import Fragment
from h5pxblock.utils import str2bool, unpack_and_upload_on_cloud, unpack_package_local_path
from h5pxblock.utils import get_h5p_storage, str2bool, unpack_and_upload_on_cloud, unpack_package_local_path


# Make '_' a no-op so we can scrape strings
Expand All @@ -26,6 +25,7 @@
H5P_ROOT = os.path.join(settings.MEDIA_ROOT, "h5pxblockmedia")
H5P_URL = os.path.join(settings.MEDIA_URL, "h5pxblockmedia")

H5P_STORAGE = get_h5p_storage()

@XBlock.wants('user')
@XBlock.wants('i18n')
Expand Down Expand Up @@ -122,7 +122,7 @@ def render_template(self, template_path, context):

@property
def store_content_on_local_fs(self):
return default_storage.__class__.__name__ == 'FileSystemStorage'
return H5P_STORAGE.__class__.__name__ == 'FileSystemStorage'

@property
def get_block_path_prefix(self):
Expand Down Expand Up @@ -256,9 +256,9 @@ def studio_submit(self, request, suffix=""):
self.h5p_content_json_path = self.h5p_content_url
else:
unpack_and_upload_on_cloud(
h5p_package, default_storage, self.cloud_storage_path
h5p_package, H5P_STORAGE, self.cloud_storage_path
)
self.h5p_content_json_path = default_storage.url(self.cloud_storage_path)
self.h5p_content_json_path = H5P_STORAGE.url(self.cloud_storage_path)
elif request.params["h5_content_path"]:
self.h5p_content_json_path = request.params["h5_content_path"]

Expand Down
23 changes: 23 additions & 0 deletions h5pxblock/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,36 @@
from zipfile import is_zipfile, ZipFile
from django.conf import settings
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage, get_storage_class

log = logging.getLogger(__name__)


MAX_WORKERS = getattr(settings, "THREADPOOLEXECUTOR_MAX_WORKERS", 10)


def get_h5p_storage():
"""
Returns storage for h5p content
If H5PXBLOCK_STORAGE is defined in django settings, intializes storage using the
specified settings. Otherwise, returns default_storage.
"""
h5p_storage_settings = getattr(settings, "H5PXBLOCK_STORAGE", None)

if not h5p_storage_settings:
return default_storage

storage_class_import_path = h5p_storage_settings.get("storage_class", None)
storage_settings = h5p_storage_settings.get("settings", {})

storage_class = get_storage_class(storage_class_import_path)

storage = storage_class(**storage_settings)

return storage


def str2bool(val):
""" Converts string value to boolean"""
return val in ['True', 'true', '1']
Expand Down

0 comments on commit d89bcb6

Please sign in to comment.