Skip to content

Commit

Permalink
[fix] Made private storage backend configuration #195
Browse files Browse the repository at this point in the history
Added OPENWISP_FIRMWARE_PRIVATE_STORAGE_INSTANCE setting
to configure an instance of private storage class.

Fixes #195
  • Loading branch information
pandafy committed Jun 8, 2022
1 parent e166999 commit 2e3f4ce
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 7 deletions.
16 changes: 16 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,22 @@ If you need to use a `custom upgrader class <#writing-custom-firmware-upgrader-c
you will need to use this setting to provide an entry with the class path of your upgrader
as the value.

``OPENWISP_FIRMWARE_PRIVATE_STORAGE_INSTANCE``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+--------------+-------------------------------------------------------------------------------------+
| **type**: | ``str`` |
+--------------+-------------------------------------------------------------------------------------+
| **default**: | ``openwisp_firmware_upgrader.private_storage.storage.file_system_private_storage`` |
+--------------+-------------------------------------------------------------------------------------+

Dotted path to an instance of any one of the storage classes in
`private_storage <https://github.com/edoburu/django-private-storage#django-private-storage>`_.
This instance is used to store firmware image files.

By default, an instance of ``private_storage.storage.files.PrivateFileSystemStorage``
is used.

Extending openwisp-firmware-upgrader
------------------------------------

Expand Down
7 changes: 1 addition & 6 deletions openwisp_firmware_upgrader/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
from decimal import Decimal
from pathlib import Path
from urllib.parse import urljoin

import swapper
from django.conf import settings
Expand All @@ -13,7 +12,6 @@
from django.utils.module_loading import import_string
from django.utils.translation import gettext_lazy as _
from private_storage.fields import PrivateFileField
from private_storage.storage.files import PrivateFileSystemStorage

from openwisp_users.mixins import OrgMixin
from openwisp_utils.base import TimeStampedEditableModel
Expand All @@ -30,7 +28,6 @@
FIRMWARE_IMAGE_TYPE_CHOICES,
REVERSE_FIRMWARE_IMAGE_MAP,
)
from ..settings import FIRMWARE_API_BASEURL, IMAGE_URL_PATH
from ..swapper import get_model_name, load_model
from ..tasks import (
batch_upgrade_operation,
Expand Down Expand Up @@ -180,9 +177,7 @@ class AbstractFirmwareImage(TimeStampedEditableModel):
'File',
upload_to=get_build_directory,
max_file_size=app_settings.MAX_FILE_SIZE,
storage=PrivateFileSystemStorage(
base_url=urljoin(FIRMWARE_API_BASEURL, IMAGE_URL_PATH)
),
storage=app_settings.PRIVATE_STORAGE_INSTANCE,
)
type = models.CharField(
blank=True,
Expand Down
9 changes: 9 additions & 0 deletions openwisp_firmware_upgrader/private_storage/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from urllib.parse import urljoin

from private_storage.storage.files import PrivateFileSystemStorage

from ..settings import FIRMWARE_API_BASEURL, IMAGE_URL_PATH

file_system_private_storage = PrivateFileSystemStorage(
base_url=urljoin(FIRMWARE_API_BASEURL, IMAGE_URL_PATH)
)
15 changes: 15 additions & 0 deletions openwisp_firmware_upgrader/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.module_loading import import_string

from openwisp_controller.connection import settings as conn_settings

Expand Down Expand Up @@ -28,3 +30,16 @@

# Path of urls that need to be refered in migrations files.
IMAGE_URL_PATH = 'firmware/'

try:
PRIVATE_STORAGE_INSTANCE = import_string(
getattr(
settings,
'OPENWISP_FIRMWARE_PRIVATE_STORAGE_INSTANCE',
'openwisp_firmware_upgrader.private_storage.storage.file_system_private_storage',
)
)
except ImportError:
raise ImproperlyConfigured(
'Failed to import FIRMWARE_UPGRADER_PRIVATE_STORAGE_INSTANCE'
)
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/openwisp2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
MEDIA_URL = '/media/'
MEDIA_ROOT = '{0}/media/'.format(BASE_DIR)

PRIVATE_STORAGE_ROOT = '{0}/firmware/'.format(BASE_DIR)
PRIVATE_STORAGE_ROOT = os.path.join(MEDIA_ROOT, 'private')

TEMPLATES = [
{
Expand Down

0 comments on commit 2e3f4ce

Please sign in to comment.