muffin-rest 5.1.3__tar.gz → 5.1.5__tar.gz
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.
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/PKG-INFO +1 -1
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/peewee/filters.py +3 -23
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/peewee/sorting.py +6 -5
- muffin_rest-5.1.5/muffin_rest/peewee/utils.py +27 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/pyproject.toml +1 -1
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/LICENSE +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/README.rst +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/__init__.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/api.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/errors.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/filters.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/handler.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/mongo/__init__.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/mongo/filters.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/mongo/schema.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/mongo/sorting.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/mongo/types.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/mongo/utils.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/openapi.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/options.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/peewee/__init__.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/peewee/handler.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/peewee/openapi.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/peewee/options.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/peewee/schemas.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/peewee/types.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/py.typed +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/redoc.html +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/sorting.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/sqlalchemy/__init__.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/sqlalchemy/filters.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/sqlalchemy/sorting.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/sqlalchemy/types.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/swagger.html +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/types.py +0 -0
- {muffin_rest-5.1.3 → muffin_rest-5.1.5}/muffin_rest/utils.py +0 -0
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
from __future__ import annotations
|
|
3
3
|
|
|
4
4
|
import operator
|
|
5
|
-
from typing import Any, Callable,
|
|
6
|
-
from warnings import warn
|
|
5
|
+
from typing import Any, Callable, Tuple, Type, Union, cast
|
|
7
6
|
|
|
8
7
|
from peewee import Field, ModelSelect
|
|
9
8
|
|
|
10
9
|
from muffin_rest.filters import Filter, Filters
|
|
11
10
|
|
|
11
|
+
from .utils import get_model_field_by_name
|
|
12
|
+
|
|
12
13
|
|
|
13
14
|
class PWFilter(Filter):
|
|
14
15
|
"""Support Peewee."""
|
|
@@ -53,8 +54,6 @@ class PWFilters(Filters):
|
|
|
53
54
|
|
|
54
55
|
handler = cast(PWRESTHandler, self.handler)
|
|
55
56
|
if isinstance(obj, PWFilter):
|
|
56
|
-
if obj.field is None:
|
|
57
|
-
obj.field = get_model_field_by_name(handler, obj.name)
|
|
58
57
|
return obj
|
|
59
58
|
|
|
60
59
|
if isinstance(obj, Field):
|
|
@@ -71,22 +70,3 @@ class PWFilters(Filters):
|
|
|
71
70
|
if schema_field is None and field:
|
|
72
71
|
schema_field = handler.meta.Schema._declared_fields.get(field.name)
|
|
73
72
|
return self.MUTATE_CLASS(name, field=field, schema_field=schema_field, **meta)
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
def get_model_field_by_name(handler, name) -> Optional[Field]:
|
|
77
|
-
"""Get model field by name."""
|
|
78
|
-
fields = handler.meta.model._meta.fields
|
|
79
|
-
candidate = fields.get(name)
|
|
80
|
-
if candidate:
|
|
81
|
-
return candidate
|
|
82
|
-
|
|
83
|
-
for field in fields.values():
|
|
84
|
-
if field.column_name == name:
|
|
85
|
-
return field
|
|
86
|
-
|
|
87
|
-
warn(
|
|
88
|
-
f"{handler.__qualname__} {handler.meta.model} has no field {name}",
|
|
89
|
-
category=RuntimeWarning,
|
|
90
|
-
stacklevel=5,
|
|
91
|
-
)
|
|
92
|
-
return None
|
|
@@ -8,6 +8,8 @@ from peewee import Field
|
|
|
8
8
|
|
|
9
9
|
from muffin_rest.sorting import Sort, Sorting
|
|
10
10
|
|
|
11
|
+
from .utils import get_model_field_by_name
|
|
12
|
+
|
|
11
13
|
if TYPE_CHECKING:
|
|
12
14
|
from .types import TVCollection
|
|
13
15
|
|
|
@@ -15,9 +17,7 @@ if TYPE_CHECKING:
|
|
|
15
17
|
class PWSort(Sort):
|
|
16
18
|
"""Sorter for Peewee."""
|
|
17
19
|
|
|
18
|
-
async def apply(
|
|
19
|
-
self, collection: TVCollection, *, desc: bool = False, **_
|
|
20
|
-
) -> TVCollection:
|
|
20
|
+
async def apply(self, collection: TVCollection, *, desc: bool = False, **_) -> TVCollection:
|
|
21
21
|
"""Sort the collection."""
|
|
22
22
|
return collection.order_by_extend(self.field if not desc else self.field.desc())
|
|
23
23
|
|
|
@@ -39,14 +39,15 @@ class PWSorting(Sorting):
|
|
|
39
39
|
return obj
|
|
40
40
|
|
|
41
41
|
handler = cast(PWRESTHandler, self.handler)
|
|
42
|
-
model_meta = handler.meta.model._meta # type: ignore[]
|
|
43
42
|
|
|
44
43
|
if isinstance(obj, Field):
|
|
45
44
|
name, field = obj.name, obj
|
|
46
45
|
|
|
47
46
|
else:
|
|
48
47
|
name = obj
|
|
49
|
-
field = meta.pop("field",
|
|
48
|
+
field = meta.pop("field", None) or name
|
|
49
|
+
if isinstance(field, str):
|
|
50
|
+
field = get_model_field_by_name(handler, field, stacklevel=6)
|
|
50
51
|
|
|
51
52
|
if field:
|
|
52
53
|
sort = self.MUTATE_CLASS(name, field=field, **meta)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Support filters for Peewee ORM."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from typing import TYPE_CHECKING, Optional
|
|
5
|
+
from warnings import warn
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from peewee import Field
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def get_model_field_by_name(handler, name: str, stacklevel=5) -> Optional[Field]:
|
|
12
|
+
"""Get model field by name."""
|
|
13
|
+
fields = handler.meta.model._meta.fields
|
|
14
|
+
candidate = fields.get(name)
|
|
15
|
+
if candidate:
|
|
16
|
+
return candidate
|
|
17
|
+
|
|
18
|
+
for field in fields.values():
|
|
19
|
+
if field.column_name == name:
|
|
20
|
+
return field
|
|
21
|
+
|
|
22
|
+
warn(
|
|
23
|
+
f"{handler.__qualname__} {handler.meta.model} has no field {name}",
|
|
24
|
+
category=RuntimeWarning,
|
|
25
|
+
stacklevel=stacklevel,
|
|
26
|
+
)
|
|
27
|
+
return None
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|