Skip to content

Commit

Permalink
Change how import error is handled
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelafox committed Nov 23, 2024
1 parent a8af6dc commit 70508d5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
22 changes: 6 additions & 16 deletions flask_admin/contrib/fileadmin/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
from azure.storage.blob import BlobSasPermissions
from azure.storage.blob import BlobServiceClient
from azure.storage.blob import generate_blob_sas
except ImportError:
BlobServiceClient = None
except ImportError as e:
raise Exception(
"Could not import `azure.storage.blob`. "
"Enable `azure-blob-storage` integration "
"by installing `flask-admin[azure-blob-storage]`"
) from e

from flask import redirect

Expand Down Expand Up @@ -49,26 +53,12 @@ def __init__(self, container_name, connection_string):
:param connection_string:
Azure Blob Storage Connection String
"""

if not BlobServiceClient:
raise ValueError(
"Could not import `azure.storage.blob`. "
"Enable `azure-blob-storage` integration "
"by installing `flask-admin[azure-blob-storage]`"
)

self._container_name = container_name
self._connection_string = connection_string
self.__client = None

@property
def _client(self):
if BlobServiceClient is None:
raise ValueError(
"Could not import `azure.storage.blob`. "
"Enable `azure-blob-storage` integration "
"by installing `flask-admin[azure-blob-storage]`"
)
if not self.__client:
self.__client = BlobServiceClient.from_connection_string(
self._connection_string
Expand Down
7 changes: 5 additions & 2 deletions flask_admin/tests/fileadmin/test_fileadmin_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

import pytest

from flask_admin.contrib.fileadmin import azure
try:
from flask_admin.contrib.fileadmin import azure
except ImportError:
azure = None

from .test_fileadmin import Base

Expand All @@ -14,7 +17,7 @@ class TestAzureFileAdmin(Base.FileAdminTests):

@pytest.fixture(autouse=True)
def setup_and_teardown(self):
if not azure.BlobServiceClient:
if azure is None:
raise SkipTest("AzureFileAdmin dependencies not installed")

self._container_name = f"fileadmin-tests-{uuid4()}"
Expand Down

0 comments on commit 70508d5

Please sign in to comment.