devadmin 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- devadmin/__init__.py +19 -0
- devadmin/__main__.py +5 -0
- devadmin/adapters/__init__.py +0 -0
- devadmin/adapters/base.py +175 -0
- devadmin/adapters/tortoise/__init__.py +81 -0
- devadmin/adapters/tortoise/introspect.py +209 -0
- devadmin/adapters/tortoise/models.py +34 -0
- devadmin/adapters/tortoise/queries.py +330 -0
- devadmin/adapters/tortoise/related.py +215 -0
- devadmin/adapters/tortoise/store.py +156 -0
- devadmin/auth/__init__.py +16 -0
- devadmin/auth/csrf.py +18 -0
- devadmin/auth/ip.py +28 -0
- devadmin/auth/login.py +149 -0
- devadmin/auth/session.py +94 -0
- devadmin/cli.py +314 -0
- devadmin/config.py +34 -0
- devadmin/core/__init__.py +0 -0
- devadmin/core/binding.py +609 -0
- devadmin/core/forms.py +475 -0
- devadmin/core/listing.py +400 -0
- devadmin/core/options.py +99 -0
- devadmin/core/paths.py +39 -0
- devadmin/core/site.py +288 -0
- devadmin/core/values.py +122 -0
- devadmin/history/__init__.py +38 -0
- devadmin/history/diff.py +109 -0
- devadmin/history/encoding.py +120 -0
- devadmin/history/records.py +40 -0
- devadmin/history/render.py +129 -0
- devadmin/history/service.py +256 -0
- devadmin/web/__init__.py +0 -0
- devadmin/web/app.py +106 -0
- devadmin/web/deps.py +275 -0
- devadmin/web/guard.py +178 -0
- devadmin/web/static/autocomplete.js +231 -0
- devadmin/web/static/devadmin.css +1645 -0
- devadmin/web/static/devadmin.js +129 -0
- devadmin/web/static/htmx.min.js +1 -0
- devadmin/web/static/icons/check.svg +1 -0
- devadmin/web/static/icons/clock.svg +1 -0
- devadmin/web/static/icons/cross.svg +1 -0
- devadmin/web/static/icons/down.svg +1 -0
- devadmin/web/static/icons/eye.svg +1 -0
- devadmin/web/static/icons/lock.svg +1 -0
- devadmin/web/static/icons/pencil.svg +1 -0
- devadmin/web/static/icons/plus.svg +1 -0
- devadmin/web/static/icons/right.svg +1 -0
- devadmin/web/static/icons/trash.svg +1 -0
- devadmin/web/static/icons/up.svg +1 -0
- devadmin/web/templates/_field.html +35 -0
- devadmin/web/templates/_history.html +19 -0
- devadmin/web/templates/_pagination.html +8 -0
- devadmin/web/templates/action_confirm.html +26 -0
- devadmin/web/templates/base.html +63 -0
- devadmin/web/templates/delete.html +62 -0
- devadmin/web/templates/error.html +9 -0
- devadmin/web/templates/form.html +34 -0
- devadmin/web/templates/history.html +59 -0
- devadmin/web/templates/index.html +49 -0
- devadmin/web/templates/list.html +88 -0
- devadmin/web/templates/login.html +15 -0
- devadmin/web/templates/recent.html +25 -0
- devadmin/web/views_auth.py +146 -0
- devadmin/web/views_delete.py +774 -0
- devadmin/web/views_form.py +551 -0
- devadmin/web/views_history.py +261 -0
- devadmin/web/views_list.py +246 -0
- devadmin-0.1.0.dist-info/METADATA +702 -0
- devadmin-0.1.0.dist-info/RECORD +73 -0
- devadmin-0.1.0.dist-info/WHEEL +4 -0
- devadmin-0.1.0.dist-info/entry_points.txt +3 -0
- devadmin-0.1.0.dist-info/licenses/LICENSE +21 -0
devadmin/__init__.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from devadmin import history
|
|
2
|
+
from devadmin.config import AdminConfig
|
|
3
|
+
from devadmin.core.options import ConfigError, ModelAdmin, SimpleFilter, action, display
|
|
4
|
+
from devadmin.core.site import register
|
|
5
|
+
from devadmin.web.app import create_admin
|
|
6
|
+
|
|
7
|
+
__version__ = "0.1.0"
|
|
8
|
+
__all__ = [
|
|
9
|
+
"AdminConfig",
|
|
10
|
+
"ConfigError",
|
|
11
|
+
"ModelAdmin",
|
|
12
|
+
"SimpleFilter",
|
|
13
|
+
"__version__",
|
|
14
|
+
"action",
|
|
15
|
+
"create_admin",
|
|
16
|
+
"display",
|
|
17
|
+
"history",
|
|
18
|
+
"register",
|
|
19
|
+
]
|
devadmin/__main__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
from collections.abc import Mapping
|
|
2
|
+
from contextlib import AbstractAsyncContextManager
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from datetime import datetime
|
|
5
|
+
from enum import StrEnum
|
|
6
|
+
from typing import Any, Literal, Protocol
|
|
7
|
+
|
|
8
|
+
from devadmin.history.records import ActionRecord, ChangeRecord, HistoryEntry
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class RejectedValue(ValueError):
|
|
12
|
+
pass
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class FieldType(StrEnum):
|
|
16
|
+
INT = "int"
|
|
17
|
+
DECIMAL = "decimal"
|
|
18
|
+
FLOAT = "float"
|
|
19
|
+
STR = "str"
|
|
20
|
+
TEXT = "text"
|
|
21
|
+
BOOL = "bool"
|
|
22
|
+
DATE = "date"
|
|
23
|
+
DATETIME = "datetime"
|
|
24
|
+
TIME = "time"
|
|
25
|
+
ENUM = "enum"
|
|
26
|
+
JSON = "json"
|
|
27
|
+
UUID = "uuid"
|
|
28
|
+
FK = "fk"
|
|
29
|
+
M2M = "m2m"
|
|
30
|
+
UNSUPPORTED = "unsupported"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True)
|
|
34
|
+
class Relation:
|
|
35
|
+
kind: Literal["fk", "m2m"]
|
|
36
|
+
target: str
|
|
37
|
+
target_pk: str
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True)
|
|
41
|
+
class FieldInfo:
|
|
42
|
+
name: str
|
|
43
|
+
label: str
|
|
44
|
+
type: FieldType
|
|
45
|
+
nullable: bool
|
|
46
|
+
pk: bool
|
|
47
|
+
unique: bool
|
|
48
|
+
editable: bool
|
|
49
|
+
max_length: int | None
|
|
50
|
+
choices: list[tuple[Any, str]] | None
|
|
51
|
+
has_default: bool
|
|
52
|
+
relation: Relation | None
|
|
53
|
+
column: str
|
|
54
|
+
min_value: Any | None = None
|
|
55
|
+
max_value: Any | None = None
|
|
56
|
+
decimal_places: int | None = None
|
|
57
|
+
default: Any = None
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@dataclass(frozen=True)
|
|
61
|
+
class ModelInfo:
|
|
62
|
+
key: str
|
|
63
|
+
name: str
|
|
64
|
+
module: str
|
|
65
|
+
table: str
|
|
66
|
+
model: Any
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@dataclass(frozen=True)
|
|
70
|
+
class Filter:
|
|
71
|
+
path: str
|
|
72
|
+
op: Literal["eq", "iexact", "istartswith", "icontains", "isnull", "in"]
|
|
73
|
+
value: Any
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass(frozen=True)
|
|
77
|
+
class Order:
|
|
78
|
+
path: str
|
|
79
|
+
descending: bool
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@dataclass(frozen=True)
|
|
83
|
+
class Query:
|
|
84
|
+
filters: list[Filter]
|
|
85
|
+
search: list[list[Filter]]
|
|
86
|
+
ordering: list[Order]
|
|
87
|
+
joins: list[str]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@dataclass(frozen=True)
|
|
91
|
+
class Choice:
|
|
92
|
+
pk: Any
|
|
93
|
+
label: str
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@dataclass
|
|
97
|
+
class RelatedRow:
|
|
98
|
+
model_key: str
|
|
99
|
+
pk: Any
|
|
100
|
+
repr: str
|
|
101
|
+
raw: dict[str, Any]
|
|
102
|
+
nulled_field: str | None
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass
|
|
106
|
+
class RelatedRows:
|
|
107
|
+
cascaded: list[RelatedRow]
|
|
108
|
+
nulled: list[RelatedRow]
|
|
109
|
+
blocking: list[RelatedRow]
|
|
110
|
+
truncated: bool
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@dataclass(frozen=True)
|
|
114
|
+
class M2MLinks:
|
|
115
|
+
model_key: str
|
|
116
|
+
field: str
|
|
117
|
+
other_model_key: str
|
|
118
|
+
count: int
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
class HistoryStore(Protocol):
|
|
122
|
+
async def insert(self, action: ActionRecord, changes: list[ChangeRecord]) -> int: ...
|
|
123
|
+
|
|
124
|
+
async def object_history(
|
|
125
|
+
self, model_key: str, object_pk: str, offset: int, limit: int
|
|
126
|
+
) -> list[HistoryEntry]: ...
|
|
127
|
+
|
|
128
|
+
async def recent(self, offset: int, limit: int) -> list[HistoryEntry]: ...
|
|
129
|
+
|
|
130
|
+
async def purge(self, older_than: datetime, batch: int = 500) -> int: ...
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class Adapter(Protocol):
|
|
134
|
+
history: "HistoryStore"
|
|
135
|
+
write_errors: tuple[type[BaseException], ...]
|
|
136
|
+
|
|
137
|
+
def rejection_message(self, exc: BaseException) -> str: ...
|
|
138
|
+
|
|
139
|
+
def list_models(self) -> list[ModelInfo]: ...
|
|
140
|
+
|
|
141
|
+
def describe(self, model: Any) -> list[FieldInfo]: ...
|
|
142
|
+
|
|
143
|
+
def parse_pk(self, model: Any, raw: str) -> Any: ...
|
|
144
|
+
|
|
145
|
+
async def query(self, model: Any, query: Query, offset: int, limit: int) -> list[Any]: ...
|
|
146
|
+
|
|
147
|
+
async def count(self, model: Any, query: Query) -> int: ...
|
|
148
|
+
|
|
149
|
+
async def estimate_count(self, model: Any) -> int | None: ...
|
|
150
|
+
|
|
151
|
+
async def get(self, model: Any, pk: Any) -> Any | None: ...
|
|
152
|
+
|
|
153
|
+
async def create(self, model: Any, data: dict[str, Any]) -> Any: ...
|
|
154
|
+
|
|
155
|
+
async def update(self, obj: Any, data: dict[str, Any]) -> Any: ...
|
|
156
|
+
|
|
157
|
+
async def delete(self, obj: Any) -> None: ...
|
|
158
|
+
|
|
159
|
+
async def collect_related(self, objs: list[Any], limit: int) -> RelatedRows: ...
|
|
160
|
+
|
|
161
|
+
async def m2m_links(self, objs: list[Any]) -> list[M2MLinks]: ...
|
|
162
|
+
|
|
163
|
+
async def related_choices(self, model: Any, field: str, term: str, limit: int) -> list[Choice]: ...
|
|
164
|
+
|
|
165
|
+
def to_dict(self, obj: Any) -> dict[str, Any]: ...
|
|
166
|
+
|
|
167
|
+
def atomic(self) -> AbstractAsyncContextManager[Any]: ...
|
|
168
|
+
|
|
169
|
+
async def fk_reprs(self, obj: Any) -> dict[str, str]: ...
|
|
170
|
+
|
|
171
|
+
def pk_of(self, obj: Any) -> Any: ...
|
|
172
|
+
|
|
173
|
+
def repr_of(self, obj: Any) -> str: ...
|
|
174
|
+
|
|
175
|
+
def hide_from_names(self, sensitive: Mapping[str, set[str]]) -> None: ...
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
from collections.abc import Mapping
|
|
2
|
+
from contextlib import AbstractAsyncContextManager
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from tortoise.exceptions import FieldError, IntegrityError, ValidationError
|
|
6
|
+
from tortoise.transactions import in_transaction
|
|
7
|
+
|
|
8
|
+
from devadmin.adapters.base import Choice, FieldInfo, M2MLinks, ModelInfo, Query, RejectedValue, RelatedRows
|
|
9
|
+
from devadmin.adapters.tortoise import introspect, queries, related
|
|
10
|
+
from devadmin.adapters.tortoise.store import TortoiseHistoryStore
|
|
11
|
+
|
|
12
|
+
REJECTION_MESSAGE_LIMIT = 200
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TortoiseAdapter:
|
|
16
|
+
write_errors: tuple[type[BaseException], ...] = (IntegrityError, ValidationError, FieldError, RejectedValue)
|
|
17
|
+
|
|
18
|
+
def __init__(self, connection_name: str | None = None) -> None:
|
|
19
|
+
self.connection_name = connection_name
|
|
20
|
+
self.history = TortoiseHistoryStore(connection_name)
|
|
21
|
+
|
|
22
|
+
def list_models(self) -> list[ModelInfo]:
|
|
23
|
+
return introspect.list_models()
|
|
24
|
+
|
|
25
|
+
def describe(self, model: Any) -> list[FieldInfo]:
|
|
26
|
+
return introspect.describe(model)
|
|
27
|
+
|
|
28
|
+
def parse_pk(self, model: Any, raw: str) -> Any:
|
|
29
|
+
return introspect.parse_pk(model, raw)
|
|
30
|
+
|
|
31
|
+
def to_dict(self, obj: Any) -> dict[str, Any]:
|
|
32
|
+
return introspect.to_dict(obj)
|
|
33
|
+
|
|
34
|
+
def pk_of(self, obj: Any) -> Any:
|
|
35
|
+
return obj.pk
|
|
36
|
+
|
|
37
|
+
def repr_of(self, obj: Any) -> str:
|
|
38
|
+
return introspect.repr_of(obj)
|
|
39
|
+
|
|
40
|
+
def hide_from_names(self, sensitive: Mapping[str, set[str]]) -> None:
|
|
41
|
+
introspect.hide_from_names(sensitive)
|
|
42
|
+
|
|
43
|
+
def rejection_message(self, exc: BaseException) -> str:
|
|
44
|
+
lines = [line.strip() for line in str(exc).splitlines() if line.strip()]
|
|
45
|
+
return (lines[0] if lines else type(exc).__name__)[:REJECTION_MESSAGE_LIMIT]
|
|
46
|
+
|
|
47
|
+
async def query(self, model: Any, query: Query, offset: int, limit: int) -> list[Any]:
|
|
48
|
+
return await queries.query(model, query, offset, limit)
|
|
49
|
+
|
|
50
|
+
async def count(self, model: Any, query: Query) -> int:
|
|
51
|
+
return await queries.count(model, query)
|
|
52
|
+
|
|
53
|
+
async def estimate_count(self, model: Any) -> int | None:
|
|
54
|
+
return await queries.estimate_count(model)
|
|
55
|
+
|
|
56
|
+
async def get(self, model: Any, pk: Any) -> Any | None:
|
|
57
|
+
return await queries.get(model, pk)
|
|
58
|
+
|
|
59
|
+
async def create(self, model: Any, data: dict[str, Any]) -> Any:
|
|
60
|
+
return await queries.create(model, data)
|
|
61
|
+
|
|
62
|
+
async def update(self, obj: Any, data: dict[str, Any]) -> Any:
|
|
63
|
+
return await queries.update(obj, data)
|
|
64
|
+
|
|
65
|
+
async def delete(self, obj: Any) -> None:
|
|
66
|
+
await queries.delete(obj)
|
|
67
|
+
|
|
68
|
+
async def collect_related(self, objs: list[Any], limit: int) -> RelatedRows:
|
|
69
|
+
return await related.collect_related(objs, limit)
|
|
70
|
+
|
|
71
|
+
async def m2m_links(self, objs: list[Any]) -> list[M2MLinks]:
|
|
72
|
+
return await related.m2m_links(objs)
|
|
73
|
+
|
|
74
|
+
async def related_choices(self, model: Any, field: str, term: str, limit: int) -> list[Choice]:
|
|
75
|
+
return await queries.related_choices(model, field, term, limit)
|
|
76
|
+
|
|
77
|
+
async def fk_reprs(self, obj: Any) -> dict[str, str]:
|
|
78
|
+
return await queries.fk_reprs(obj)
|
|
79
|
+
|
|
80
|
+
def atomic(self) -> AbstractAsyncContextManager[Any]:
|
|
81
|
+
return in_transaction(self.connection_name)
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import uuid
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
from decimal import Decimal
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from tortoise import Tortoise, fields
|
|
8
|
+
from tortoise.fields.base import Field
|
|
9
|
+
from tortoise.fields.data import CharEnumFieldInstance, CharField, IntEnumFieldInstance
|
|
10
|
+
from tortoise.fields.relational import ForeignKeyFieldInstance, ManyToManyFieldInstance
|
|
11
|
+
from tortoise.models import Model
|
|
12
|
+
|
|
13
|
+
from devadmin.adapters.base import FieldInfo, FieldType, ModelInfo, Relation
|
|
14
|
+
|
|
15
|
+
_FIELD_TYPES: list[tuple[type[Field], FieldType]] = [
|
|
16
|
+
(ManyToManyFieldInstance, FieldType.M2M),
|
|
17
|
+
(ForeignKeyFieldInstance, FieldType.FK),
|
|
18
|
+
(CharEnumFieldInstance, FieldType.ENUM),
|
|
19
|
+
(IntEnumFieldInstance, FieldType.ENUM),
|
|
20
|
+
(fields.IntField, FieldType.INT),
|
|
21
|
+
(fields.DecimalField, FieldType.DECIMAL),
|
|
22
|
+
(fields.FloatField, FieldType.FLOAT),
|
|
23
|
+
(fields.CharField, FieldType.STR),
|
|
24
|
+
(fields.TextField, FieldType.TEXT),
|
|
25
|
+
(fields.BooleanField, FieldType.BOOL),
|
|
26
|
+
(fields.DatetimeField, FieldType.DATETIME),
|
|
27
|
+
(fields.DateField, FieldType.DATE),
|
|
28
|
+
(fields.TimeField, FieldType.TIME),
|
|
29
|
+
(fields.JSONField, FieldType.JSON),
|
|
30
|
+
(fields.UUIDField, FieldType.UUID),
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
_INTEGER = re.compile(r"-?[0-9]+")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def model_key(model: type[Model]) -> str:
|
|
37
|
+
return f"{model._meta.app}.{model.__name__}"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def list_models() -> list[ModelInfo]:
|
|
41
|
+
infos = [
|
|
42
|
+
ModelInfo(
|
|
43
|
+
key=model_key(model),
|
|
44
|
+
name=model.__name__,
|
|
45
|
+
module=model.__module__,
|
|
46
|
+
table=model._meta.db_table,
|
|
47
|
+
model=model,
|
|
48
|
+
)
|
|
49
|
+
for models in Tortoise.apps.values()
|
|
50
|
+
for model in models.values()
|
|
51
|
+
]
|
|
52
|
+
return sorted(infos, key=lambda info: info.key)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def field_type(field: Field) -> FieldType:
|
|
56
|
+
for field_class, kind in _FIELD_TYPES:
|
|
57
|
+
if isinstance(field, field_class):
|
|
58
|
+
return kind
|
|
59
|
+
return FieldType.UNSUPPORTED
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def describe(model: type[Model]) -> list[FieldInfo]:
|
|
63
|
+
meta = model._meta
|
|
64
|
+
relation_names = meta.fk_fields | meta.o2o_fields
|
|
65
|
+
hidden = (
|
|
66
|
+
meta.backward_fk_fields
|
|
67
|
+
| meta.backward_o2o_fields
|
|
68
|
+
| {meta.fields_map[name].source_field for name in relation_names}
|
|
69
|
+
| {name for name in meta.m2m_fields if meta.fields_map[name]._generated}
|
|
70
|
+
)
|
|
71
|
+
return [
|
|
72
|
+
_field_info(model, name, field)
|
|
73
|
+
for name, field in meta.fields_map.items()
|
|
74
|
+
if name not in hidden
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _field_info(model: type[Model], name: str, field: Field) -> FieldInfo:
|
|
79
|
+
kind = field_type(field)
|
|
80
|
+
if kind is FieldType.FK and field.source_field not in model._meta.fields_db_projection:
|
|
81
|
+
kind = FieldType.UNSUPPORTED
|
|
82
|
+
min_value, max_value, decimal_places = _bounds(field, kind)
|
|
83
|
+
auto_now = bool(getattr(field, "auto_now", False) or getattr(field, "auto_now_add", False))
|
|
84
|
+
editable = not (
|
|
85
|
+
(field.pk and field.generated) or auto_now or kind in (FieldType.M2M, FieldType.UNSUPPORTED)
|
|
86
|
+
)
|
|
87
|
+
return FieldInfo(
|
|
88
|
+
name=name,
|
|
89
|
+
label=name.replace("_", " ").capitalize(),
|
|
90
|
+
type=kind,
|
|
91
|
+
nullable=field.null,
|
|
92
|
+
pk=field.pk,
|
|
93
|
+
unique=field.unique and kind is not FieldType.M2M,
|
|
94
|
+
editable=editable,
|
|
95
|
+
max_length=field.max_length if kind is FieldType.STR else None,
|
|
96
|
+
choices=[(member.value, member.name) for member in field.enum_type] if kind is FieldType.ENUM else None,
|
|
97
|
+
has_default=field.default is not None or field.generated or auto_now,
|
|
98
|
+
relation=_relation(field, kind),
|
|
99
|
+
column=_column(model, name, field, kind),
|
|
100
|
+
min_value=min_value,
|
|
101
|
+
max_value=max_value,
|
|
102
|
+
decimal_places=decimal_places,
|
|
103
|
+
default=_static_default(field, kind, auto_now),
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _static_default(field: Field, kind: FieldType, auto_now: bool) -> Any:
|
|
108
|
+
default = field.default
|
|
109
|
+
if default is None or callable(default) or field.generated or auto_now:
|
|
110
|
+
return None
|
|
111
|
+
try:
|
|
112
|
+
if kind is FieldType.ENUM:
|
|
113
|
+
return field.enum_type(default)
|
|
114
|
+
if kind is FieldType.JSON and isinstance(default, (str, bytes)):
|
|
115
|
+
return field.decoder(default)
|
|
116
|
+
except Exception:
|
|
117
|
+
return None
|
|
118
|
+
return default
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _bounds(field: Field, kind: FieldType) -> tuple[Any, Any, int | None]:
|
|
122
|
+
if kind is FieldType.INT:
|
|
123
|
+
constraints = field.constraints
|
|
124
|
+
return constraints.get("ge"), constraints.get("le"), None
|
|
125
|
+
if kind is FieldType.DECIMAL:
|
|
126
|
+
digits = getattr(field, "max_digits", None)
|
|
127
|
+
places = getattr(field, "decimal_places", None)
|
|
128
|
+
if digits is None or places is None:
|
|
129
|
+
return None, None, places
|
|
130
|
+
nines = (9,) * digits
|
|
131
|
+
return Decimal((1, nines, -places)), Decimal((0, nines, -places)), places
|
|
132
|
+
return None, None, None
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _relation(field: Field, kind: FieldType) -> Relation | None:
|
|
136
|
+
if kind is FieldType.FK:
|
|
137
|
+
return Relation("fk", model_key(field.related_model), field.to_field)
|
|
138
|
+
if kind is FieldType.M2M:
|
|
139
|
+
return Relation("m2m", model_key(field.related_model), field.related_model._meta.pk_attr)
|
|
140
|
+
return None
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _column(model: type[Model], name: str, field: Field, kind: FieldType) -> str:
|
|
144
|
+
projection = model._meta.fields_db_projection
|
|
145
|
+
if isinstance(field, ForeignKeyFieldInstance):
|
|
146
|
+
return projection.get(field.source_field, field.source_field)
|
|
147
|
+
return projection.get(name, name)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _key_field(model: type[Model]) -> Field:
|
|
151
|
+
field = model._meta.pk
|
|
152
|
+
seen = set()
|
|
153
|
+
while field_type(field) is FieldType.FK and id(field) not in seen:
|
|
154
|
+
seen.add(id(field))
|
|
155
|
+
field = field.to_field_instance
|
|
156
|
+
return field
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def parse_pk(model: type[Model], raw: str) -> Any:
|
|
160
|
+
field = _key_field(model)
|
|
161
|
+
kind = field_type(field)
|
|
162
|
+
if kind is FieldType.INT:
|
|
163
|
+
if not _INTEGER.fullmatch(raw):
|
|
164
|
+
raise ValueError(f"invalid integer key: {raw!r}")
|
|
165
|
+
value = int(raw)
|
|
166
|
+
bounds = field.constraints
|
|
167
|
+
if not bounds["ge"] <= value <= bounds["le"]:
|
|
168
|
+
raise ValueError(f"integer key out of range: {raw!r}")
|
|
169
|
+
return value
|
|
170
|
+
if kind is FieldType.UUID:
|
|
171
|
+
return uuid.UUID(raw)
|
|
172
|
+
if kind in (FieldType.STR, FieldType.TEXT):
|
|
173
|
+
if not raw:
|
|
174
|
+
raise ValueError("empty key")
|
|
175
|
+
return raw
|
|
176
|
+
raise ValueError(f"unsupported key type for {model.__name__}")
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def to_dict(obj: Model) -> dict[str, Any]:
|
|
180
|
+
return {column: getattr(obj, name) for name, column in obj._meta.fields_db_projection.items()}
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
_NAMING_FIELDS = ("name", "title", "full_name", "email", "username", "code", "slug", "label", "number")
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
_unnamed: dict[str, frozenset[str]] = {}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def hide_from_names(sensitive: Mapping[str, set[str]]) -> None:
|
|
190
|
+
for key, names in sensitive.items():
|
|
191
|
+
_unnamed[key] = frozenset(names)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _naming_value(obj: Model) -> str | None:
|
|
195
|
+
fields = type(obj)._meta.fields_map
|
|
196
|
+
hidden = _unnamed.get(model_key(type(obj)), frozenset())
|
|
197
|
+
for name in _NAMING_FIELDS:
|
|
198
|
+
field = None if name in hidden else fields.get(name)
|
|
199
|
+
if isinstance(field, CharField) and not isinstance(field, CharEnumFieldInstance):
|
|
200
|
+
value = getattr(obj, name, None)
|
|
201
|
+
if isinstance(value, str) and value.strip():
|
|
202
|
+
return value
|
|
203
|
+
return None
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def repr_of(obj: Model) -> str:
|
|
207
|
+
if type(obj).__str__ is Model.__str__:
|
|
208
|
+
return _naming_value(obj) or f"{type(obj).__name__} #{obj.pk}"
|
|
209
|
+
return str(obj)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from tortoise import fields
|
|
2
|
+
from tortoise.models import Model
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class DevadminAction(Model):
|
|
6
|
+
id = fields.BigIntField(primary_key=True)
|
|
7
|
+
created_at = fields.DatetimeField(auto_now_add=True, db_index=True)
|
|
8
|
+
actor_id = fields.CharField(max_length=64, null=True)
|
|
9
|
+
actor_repr = fields.CharField(max_length=255)
|
|
10
|
+
kind = fields.CharField(max_length=16)
|
|
11
|
+
summary = fields.TextField()
|
|
12
|
+
source = fields.CharField(max_length=8)
|
|
13
|
+
ip = fields.CharField(max_length=45, null=True)
|
|
14
|
+
meta = fields.JSONField()
|
|
15
|
+
|
|
16
|
+
class Meta:
|
|
17
|
+
table = "devadmin_action"
|
|
18
|
+
indexes = (("actor_id", "created_at"),)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class DevadminChange(Model):
|
|
22
|
+
id = fields.BigIntField(primary_key=True)
|
|
23
|
+
action_id = fields.BigIntField(db_index=True)
|
|
24
|
+
model_key = fields.CharField(max_length=255)
|
|
25
|
+
object_pk = fields.CharField(max_length=255)
|
|
26
|
+
object_repr = fields.CharField(max_length=255)
|
|
27
|
+
op = fields.CharField(max_length=8)
|
|
28
|
+
changes = fields.JSONField()
|
|
29
|
+
snapshot = fields.JSONField()
|
|
30
|
+
field_labels = fields.JSONField()
|
|
31
|
+
|
|
32
|
+
class Meta:
|
|
33
|
+
table = "devadmin_change"
|
|
34
|
+
indexes = (("model_key", "object_pk", "id"),)
|