mirror of https://gitlab.com/tildes/tildes.git
Browse Source
Upgrade Marshmallow to 4.0
Upgrade Marshmallow to 4.0
See merge request tildes/tildes!171merge-requests/176/merge
29 changed files with 209 additions and 98 deletions
-
4tildes/requirements-dev.txt
-
4tildes/requirements.in
-
4tildes/requirements.txt
-
5tildes/tests/test_markdown_field.py
-
5tildes/tests/test_simplestring_field.py
-
6tildes/tildes/json.py
-
37tildes/tildes/schemas/base.py
-
9tildes/tildes/schemas/comment.py
-
30tildes/tildes/schemas/context.py
-
25tildes/tildes/schemas/fields.py
-
19tildes/tildes/schemas/group.py
-
5tildes/tildes/schemas/group_wiki_page.py
-
32tildes/tildes/schemas/listing.py
-
6tildes/tildes/schemas/message.py
-
29tildes/tildes/schemas/topic.py
-
29tildes/tildes/schemas/user.py
-
2tildes/tildes/views/api/web/comment.py
-
2tildes/tildes/views/api/web/group.py
-
3tildes/tildes/views/api/web/topic.py
-
8tildes/tildes/views/api/web/user.py
-
2tildes/tildes/views/bookmarks.py
-
6tildes/tildes/views/decorators.py
-
7tildes/tildes/views/login.py
-
2tildes/tildes/views/message.py
-
2tildes/tildes/views/register.py
-
6tildes/tildes/views/settings.py
-
8tildes/tildes/views/topic.py
-
8tildes/tildes/views/user.py
-
2tildes/tildes/views/votes.py
@ -0,0 +1,37 @@ |
|||||
|
# Copyright (c) 2018 Tildes contributors <code@tildes.net> |
||||
|
# SPDX-License-Identifier: AGPL-3.0-or-later |
||||
|
|
||||
|
"""Base Marshmallow schema.""" |
||||
|
|
||||
|
|
||||
|
from typing import Any, Optional |
||||
|
from marshmallow import Schema |
||||
|
from tildes.schemas.context import TildesSchemaContext, TildesSchemaContextDict |
||||
|
|
||||
|
|
||||
|
class BaseTildesSchema(Schema): |
||||
|
"""Base Marshmallow schema for Tildes schemas. |
||||
|
|
||||
|
Adds common code like the context dict. |
||||
|
""" |
||||
|
|
||||
|
context: TildesSchemaContextDict |
||||
|
|
||||
|
def __init__( |
||||
|
self, context: Optional[TildesSchemaContextDict] = None, **kwargs: Any |
||||
|
): |
||||
|
"""Pass an optional context, and forward Schema arguments to superclass.""" |
||||
|
super().__init__(**kwargs) |
||||
|
self.context = context if context else {} |
||||
|
|
||||
|
def get_context_value(self, key: str) -> Any: |
||||
|
"""Get a value from the context dict. |
||||
|
|
||||
|
Any active TildesSchemaContext, e.g. set using a "with" statement, |
||||
|
takes precedence. If there is no active TildesSchemaContext, then |
||||
|
it takes the value from the dict passed in __init__ instead. |
||||
|
""" |
||||
|
result = TildesSchemaContext.get(default=self.context).get(key) |
||||
|
if result: |
||||
|
return result |
||||
|
return self.context.get(key) |
||||
@ -0,0 +1,30 @@ |
|||||
|
# Copyright (c) 2018 Tildes contributors <code@tildes.net> |
||||
|
# SPDX-License-Identifier: AGPL-3.0-or-later |
||||
|
|
||||
|
"""Context variables that can be used with Marshmallow schemas.""" |
||||
|
import typing |
||||
|
|
||||
|
from marshmallow.experimental.context import Context |
||||
|
|
||||
|
|
||||
|
class TildesSchemaContextDict(typing.TypedDict, total=False): |
||||
|
"""Context for Tildes Marshmallow schemas. |
||||
|
|
||||
|
For convenience, we use one unified class instead of one per schema, |
||||
|
so it can be passed down through different schemas in a subgraph. |
||||
|
For example, if a Topic contains a reference to a User, |
||||
|
one instance of TildesContext can configure both the Topic and User. |
||||
|
""" |
||||
|
|
||||
|
# Applies to UserSchema |
||||
|
hide_username: bool |
||||
|
# Applies to UserSchema |
||||
|
check_breached_passwords: bool |
||||
|
# Applies to UserSchema |
||||
|
username_trim_whitespace: bool |
||||
|
|
||||
|
# Applies to GroupSchema |
||||
|
fix_path_capitalization: bool |
||||
|
|
||||
|
|
||||
|
TildesSchemaContext = Context[TildesSchemaContextDict] |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue