Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing name args to formatter functions #2454

Merged
merged 2 commits into from
Jul 16, 2024
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
5 changes: 3 additions & 2 deletions flask_admin/_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Union, Sequence, Dict, Callable
from typing import Union, Sequence, Dict, Callable, Any

import sqlalchemy

T_COLUMN_LIST = Sequence[Union[str, sqlalchemy.Column]]
T_FORMATTERS = Dict[type, Callable] # todo: Make this tighter
T_FORMATTER = Callable[[Any, Any, Any], Any]
T_FORMATTERS = Dict[type, T_FORMATTER]
2 changes: 1 addition & 1 deletion flask_admin/contrib/geoa/typefmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sqlalchemy import func


def geom_formatter(view, value):
def geom_formatter(view, value, name) -> str:
kwargs = {
"data-role": "leaflet",
"disabled": "disabled",
Expand Down
6 changes: 4 additions & 2 deletions flask_admin/contrib/mongoengine/typefmt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union

from markupsafe import Markup, escape

from mongoengine.base import BaseList
Expand All @@ -8,7 +10,7 @@
from . import helpers


def grid_formatter(view, value):
def grid_formatter(view, value, name) -> Union[str, Markup]:
if not value.grid_id:
return ''

Expand All @@ -26,7 +28,7 @@ def grid_formatter(view, value):
})


def grid_image_formatter(view, value):
def grid_image_formatter(view, value, name) -> Union[str, Markup]:
if not value.grid_id:
return ''

Expand Down
12 changes: 6 additions & 6 deletions flask_admin/contrib/sqla/typefmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy.orm.collections import InstrumentedList


def choice_formatter(view, choice):
def choice_formatter(view, choice, name) -> str:
"""
Return label of selected choice
see https://sqlalchemy-utils.readthedocs.io/
Expand All @@ -16,7 +16,7 @@ def choice_formatter(view, choice):
return choice.value


def arrow_formatter(view, arrow_time):
def arrow_formatter(view, arrow_time, name) -> str:
"""
Return human-friendly string of the time relative to now.
see https://arrow.readthedocs.io/
Expand All @@ -27,7 +27,7 @@ def arrow_formatter(view, arrow_time):
return arrow_time.humanize()


def arrow_export_formatter(view, arrow_time):
def arrow_export_formatter(view, arrow_time, name) -> str:
"""
Return string representation of Arrow object
see https://arrow.readthedocs.io/
Expand All @@ -47,13 +47,13 @@ def arrow_export_formatter(view, arrow_time):
})
try:
from sqlalchemy_utils import Choice
DEFAULT_FORMATTERS[Choice] = choice_formatter # type: ignore[assignment]
DEFAULT_FORMATTERS[Choice] = choice_formatter
except ImportError:
pass

try:
from arrow import Arrow
DEFAULT_FORMATTERS[Arrow] = arrow_formatter # type: ignore[assignment]
EXPORT_FORMATTERS[Arrow] = arrow_export_formatter # type: ignore[assignment]
DEFAULT_FORMATTERS[Arrow] = arrow_formatter
EXPORT_FORMATTERS[Arrow] = arrow_export_formatter
except ImportError:
pass
13 changes: 7 additions & 6 deletions flask_admin/model/typefmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from markupsafe import Markup
from flask_admin._compat import text_type
from flask_admin._types import T_FORMATTERS


def null_formatter(view, value, name):
Expand Down Expand Up @@ -38,7 +39,7 @@ def bool_formatter(view, value, name):
return Markup('<span class="fa %s glyphicon glyphicon-%s icon-%s" title="%s"></span>' % (fa, glyph, glyph, label))


def list_formatter(view, values, name):
def list_formatter(view, values, name) -> str:
"""
Return string with comma separated values

Expand All @@ -48,7 +49,7 @@ def list_formatter(view, values, name):
return u', '.join(text_type(v) for v in values)


def enum_formatter(view, value, name):
def enum_formatter(view, value, name) -> str:
"""
Return the name of the enumerated member.

Expand All @@ -58,7 +59,7 @@ def enum_formatter(view, value, name):
return value.name


def dict_formatter(view, value, name):
def dict_formatter(view, value, name) -> str:
"""
Removes unicode entities when displaying dict as string. Also unescapes
non-ASCII characters stored in the JSON.
Expand All @@ -69,20 +70,20 @@ def dict_formatter(view, value, name):
return json.dumps(value, ensure_ascii=False)


BASE_FORMATTERS = {
BASE_FORMATTERS: T_FORMATTERS = {
type(None): empty_formatter,
bool: bool_formatter,
list: list_formatter,
dict: dict_formatter,
}

EXPORT_FORMATTERS = {
EXPORT_FORMATTERS: T_FORMATTERS = {
type(None): empty_formatter,
list: list_formatter,
dict: dict_formatter,
}

DETAIL_FORMATTERS = {
DETAIL_FORMATTERS: T_FORMATTERS = {
type(None): empty_formatter,
list: list_formatter,
dict: dict_formatter,
Expand Down
Loading