refactor(ImportModelsCommand): clean code, extract logic into methods (#18866)
Co-authored-by: michael_hoffman <michael.hoffman@nielsen.com>
This commit is contained in:
parent
c1ee75dd8e
commit
9fd18e98ac
|
|
@ -33,7 +33,6 @@ from superset.models.core import Database
|
||||||
|
|
||||||
|
|
||||||
class ImportModelsCommand(BaseCommand):
|
class ImportModelsCommand(BaseCommand):
|
||||||
|
|
||||||
"""Import models"""
|
"""Import models"""
|
||||||
|
|
||||||
dao = BaseDAO
|
dao = BaseDAO
|
||||||
|
|
@ -73,14 +72,6 @@ class ImportModelsCommand(BaseCommand):
|
||||||
def validate(self) -> None:
|
def validate(self) -> None:
|
||||||
exceptions: List[ValidationError] = []
|
exceptions: List[ValidationError] = []
|
||||||
|
|
||||||
# load existing databases so we can apply the password validation
|
|
||||||
db_passwords = {
|
|
||||||
str(uuid): password
|
|
||||||
for uuid, password in db.session.query(
|
|
||||||
Database.uuid, Database.password
|
|
||||||
).all()
|
|
||||||
}
|
|
||||||
|
|
||||||
# verify that the metadata file is present and valid
|
# verify that the metadata file is present and valid
|
||||||
try:
|
try:
|
||||||
metadata: Optional[Dict[str, str]] = load_metadata(self.contents)
|
metadata: Optional[Dict[str, str]] = load_metadata(self.contents)
|
||||||
|
|
@ -88,7 +79,19 @@ class ImportModelsCommand(BaseCommand):
|
||||||
exceptions.append(exc)
|
exceptions.append(exc)
|
||||||
metadata = None
|
metadata = None
|
||||||
|
|
||||||
# validate that the type declared in METADATA_FILE_NAME is correct
|
self._validate_metadata_type(metadata, exceptions)
|
||||||
|
self._load__configs(exceptions)
|
||||||
|
self._prevent_overwrite_existing_model(exceptions)
|
||||||
|
|
||||||
|
if exceptions:
|
||||||
|
exception = CommandInvalidError(f"Error importing {self.model_name}")
|
||||||
|
exception.add_list(exceptions)
|
||||||
|
raise exception
|
||||||
|
|
||||||
|
def _validate_metadata_type(
|
||||||
|
self, metadata: Optional[Dict[str, str]], exceptions: List[ValidationError]
|
||||||
|
) -> None:
|
||||||
|
"""Validate that the type declared in METADATA_FILE_NAME is correct"""
|
||||||
if metadata and "type" in metadata:
|
if metadata and "type" in metadata:
|
||||||
type_validator = validate.Equal(self.dao.model_cls.__name__) # type: ignore
|
type_validator = validate.Equal(self.dao.model_cls.__name__) # type: ignore
|
||||||
try:
|
try:
|
||||||
|
|
@ -97,7 +100,14 @@ class ImportModelsCommand(BaseCommand):
|
||||||
exc.messages = {METADATA_FILE_NAME: {"type": exc.messages}}
|
exc.messages = {METADATA_FILE_NAME: {"type": exc.messages}}
|
||||||
exceptions.append(exc)
|
exceptions.append(exc)
|
||||||
|
|
||||||
# validate objects
|
def _load__configs(self, exceptions: List[ValidationError]) -> None:
|
||||||
|
# load existing databases so we can apply the password validation
|
||||||
|
db_passwords: Dict[str, str] = {
|
||||||
|
str(uuid): password
|
||||||
|
for uuid, password in db.session.query(
|
||||||
|
Database.uuid, Database.password
|
||||||
|
).all()
|
||||||
|
}
|
||||||
for file_name, content in self.contents.items():
|
for file_name, content in self.contents.items():
|
||||||
# skip directories
|
# skip directories
|
||||||
if not content:
|
if not content:
|
||||||
|
|
@ -121,7 +131,10 @@ class ImportModelsCommand(BaseCommand):
|
||||||
exc.messages = {file_name: exc.messages}
|
exc.messages = {file_name: exc.messages}
|
||||||
exceptions.append(exc)
|
exceptions.append(exc)
|
||||||
|
|
||||||
# check if the object exists and shouldn't be overwritten
|
def _prevent_overwrite_existing_model( # pylint: disable=invalid-name
|
||||||
|
self, exceptions: List[ValidationError]
|
||||||
|
) -> None:
|
||||||
|
"""check if the object exists and shouldn't be overwritten"""
|
||||||
if not self.overwrite:
|
if not self.overwrite:
|
||||||
existing_uuids = self._get_uuids()
|
existing_uuids = self._get_uuids()
|
||||||
for file_name, config in self._configs.items():
|
for file_name, config in self._configs.items():
|
||||||
|
|
@ -139,8 +152,3 @@ class ImportModelsCommand(BaseCommand):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if exceptions:
|
|
||||||
exception = CommandInvalidError(f"Error importing {self.model_name}")
|
|
||||||
exception.add_list(exceptions)
|
|
||||||
raise exception
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue