Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions flask_appbuilder/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ def echo_header(title):
click.echo(click.style("-" * len(title), fg="green"))


def cast_int_like_to_int(cli_arg: Union[None, str, int]) -> Union[None, str, int]:
"""Cast int-like objects to int if possible

If the arg cannot be cast to an integer, return the unmodified object instead."""
try:
cli_arg_int = int(cli_arg)
return cli_arg_int
except TypeError:
# Don't cast if None
return cli_arg
except ValueError:
# Don't cast non-int-like strings
return cli_arg


@click.group()
def fab():
""" FAB flask group commands"""
Expand Down Expand Up @@ -155,17 +170,9 @@ def export_roles(
path: Optional[str] = None, indent: Optional[Union[int, str]] = None
) -> None:
"""Exports roles with permissions and view menus to JSON file"""
# Cast negative numbers to int (as they're passed as str from CLI)
try:
indent = int(indent)
except TypeError:
# Don't cast None
pass
except ValueError:
# Don't cast non-int-like strings
pass

current_app.appbuilder.sm.export_roles(path=path, indent=indent)
# Cast negative numbers to int (as they are passed as str from CLI)
cast_indent = cast_int_like_to_int(indent)
current_app.appbuilder.sm.export_roles(path=path, indent=cast_indent)


@fab.command("import-roles")
Expand Down
17 changes: 17 additions & 0 deletions flask_appbuilder/tests/test_fab_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from flask import Flask
from flask_appbuilder import AppBuilder, SQLA
from flask_appbuilder.cli import (
cast_int_like_to_int,
create_app,
create_permissions,
create_user,
Expand Down Expand Up @@ -79,6 +80,22 @@ def test_list_views(self):
self.assertIn("List of registered views", result.output)
self.assertIn(" Route:/api/v1/security", result.output)

def test_cast_int_like_to_int(self):
scenarii = {
-1: -1,
0: 0,
1: 1,
"-1": -1,
"0": 0,
"1": 1,
"+1": 1,
"foo": "foo",
None: None,
}

for input, expected_output in scenarii.items():
self.assertEqual(cast_int_like_to_int(input), expected_output)


class SQLAlchemyImportExportTestCase(FABTestCase):
def setUp(self):
Expand Down