-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let flask_admin peek defaults without crashing
The flask_admin database frontend attempts to peek column default values to fill form fields, but calls the functions generating dynamic defaults with a context of None. Paper over this. Fixes: #89 Signed-off-by: Nils Philippsen <[email protected]>
- Loading branch information
Showing
2 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import pytest | ||
from sqlalchemy import inspect | ||
from sqlalchemy.exc import NoInspectionAvailable | ||
from sqlalchemy.orm import Mapper | ||
|
||
from tahrir_api import model | ||
|
||
|
||
def get_models_columns_with_defaults(): | ||
models_columns_as_params = [] | ||
|
||
for name, thing in model.__dict__.items(): | ||
if isinstance(thing, type): | ||
try: | ||
inspected = inspect(thing) | ||
except NoInspectionAvailable: | ||
continue | ||
|
||
if isinstance(inspected, Mapper): | ||
for colname, column in inspected.c.items(): | ||
default = getattr(column, "default", None) | ||
if default is None: | ||
continue | ||
if hasattr(default, "arg"): | ||
models_columns_as_params.append( | ||
pytest.param(column, id=f"{name}.{colname}")) | ||
|
||
return models_columns_as_params | ||
|
||
|
||
@pytest.mark.parametrize("column", get_models_columns_with_defaults()) | ||
def test_safe_column_default(column): | ||
if getattr(column.default, "is_callable", False): | ||
column.default.arg(None) |