-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathtypefmt.py
More file actions
97 lines (72 loc) · 2.36 KB
/
typefmt.py
File metadata and controls
97 lines (72 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import json
import typing as t
from enum import Enum
from markupsafe import Markup
from flask_admin._compat import text_type
from flask_admin._types import T_COLUMN_TYPE_FORMATTERS
from flask_admin._types import T_MODEL_VIEW
def null_formatter(view: T_MODEL_VIEW, value: t.Any, name: str) -> str:
"""
Return `NULL` as the string for `None` value
:param value:
Value to check
"""
return Markup("<i>NULL</i>")
def empty_formatter(view: T_MODEL_VIEW, value: t.Any, name: str) -> str:
"""
Return empty string for `None` value
:param value:
Value to check
"""
return ""
def bool_formatter(view: T_MODEL_VIEW, value: t.Any, name: str) -> str:
"""
Return check icon if value is `True` or empty string otherwise.
:param value:
Value to check
"""
bootstrap = "bi-check-circle-fill" if value else "bi-dash-circle-fill"
fa = "fa-check-circle" if value else "fa-minus-circle"
label = f'{name}: {"true" if value else "false"}'
return Markup(f'<span class="fa {fa} bi bi-{bootstrap} title="{label}"></span>')
def list_formatter(view: T_MODEL_VIEW, values: t.Iterable[t.Any], name: str) -> str:
"""
Return string with comma separated values
:param values:
Value to check
"""
return ", ".join(text_type(v) for v in values)
def enum_formatter(view: T_MODEL_VIEW, value: Enum, name: str) -> str:
"""
Return the name of the enumerated member.
:param value:
Value to check
"""
return value.name
def dict_formatter(view: T_MODEL_VIEW, value: t.Any, name: str) -> str:
"""
Removes unicode entities when displaying dict as string. Also unescapes
non-ASCII characters stored in the JSON.
:param value:
Dict to convert to string
"""
return json.dumps(value, ensure_ascii=False)
BASE_FORMATTERS: T_COLUMN_TYPE_FORMATTERS = {
type(None): empty_formatter,
bool: bool_formatter,
list: list_formatter,
dict: dict_formatter,
}
EXPORT_FORMATTERS: T_COLUMN_TYPE_FORMATTERS = {
type(None): empty_formatter,
list: list_formatter,
dict: dict_formatter,
}
DETAIL_FORMATTERS: T_COLUMN_TYPE_FORMATTERS = {
type(None): empty_formatter,
list: list_formatter,
dict: dict_formatter,
}
BASE_FORMATTERS[Enum] = enum_formatter
EXPORT_FORMATTERS[Enum] = enum_formatter
DETAIL_FORMATTERS[Enum] = enum_formatter