Browse Source

Add get_context_value() to BaseTildesSchema

merge-requests/171/head
Andrew Shu 4 weeks ago
parent
commit
427ff62049
  1. 5
      tildes/tildes/schemas/base.py
  2. 4
      tildes/tildes/schemas/group.py
  3. 10
      tildes/tildes/schemas/user.py

5
tildes/tildes/schemas/base.py

@ -6,7 +6,7 @@
from typing import Any from typing import Any
from marshmallow import Schema from marshmallow import Schema
from tildes.schemas.context import TildesSchemaContextDict
from tildes.schemas.context import TildesSchemaContext, TildesSchemaContextDict
class BaseTildesSchema(Schema): class BaseTildesSchema(Schema):
@ -20,3 +20,6 @@ class BaseTildesSchema(Schema):
def __init__(self, context: TildesSchemaContextDict = {}, **kwargs: Any): def __init__(self, context: TildesSchemaContextDict = {}, **kwargs: Any):
super().__init__(**kwargs) super().__init__(**kwargs)
self.context = context self.context = context
def get_context_value(self, key: str) -> Any:
return TildesSchemaContext.get(default=self.context).get(key)

4
tildes/tildes/schemas/group.py

@ -49,9 +49,7 @@ class GroupSchema(BaseTildesSchema):
) -> dict: ) -> dict:
"""Prepare the path value before it's validated.""" """Prepare the path value before it's validated."""
# pylint: disable=unused-argument # pylint: disable=unused-argument
if not TildesSchemaContext.get(default=self.context).get(
"fix_path_capitalization"
):
if not self.get_context_value("fix_path_capitalization"):
return data return data
if "path" not in data or not isinstance(data["path"], str): if "path" not in data or not isinstance(data["path"], str):

10
tildes/tildes/schemas/user.py

@ -66,7 +66,7 @@ class UserSchema(BaseTildesSchema):
def anonymize_username(self, data: dict, many: bool) -> dict: def anonymize_username(self, data: dict, many: bool) -> dict:
"""Hide the username if the dumping context specifies to do so.""" """Hide the username if the dumping context specifies to do so."""
# pylint: disable=unused-argument # pylint: disable=unused-argument
if not TildesSchemaContext.get(default=self.context).get("hide_username"):
if not self.get_context_value("hide_username"):
return data return data
if "username" not in data: if "username" not in data:
@ -105,9 +105,7 @@ class UserSchema(BaseTildesSchema):
Requires check_breached_passwords be True in the schema's context. Requires check_breached_passwords be True in the schema's context.
""" """
# pylint: disable=unused-argument # pylint: disable=unused-argument
if not TildesSchemaContext.get(default=self.context).get(
"check_breached_passwords"
):
if not self.get_context_value("check_breached_passwords"):
return return
if is_breached_password(value): if is_breached_password(value):
@ -125,9 +123,7 @@ class UserSchema(BaseTildesSchema):
Requires username_trim_whitespace be True in the schema's context. Requires username_trim_whitespace be True in the schema's context.
""" """
# pylint: disable=unused-argument # pylint: disable=unused-argument
if not TildesSchemaContext.get(default=self.context).get(
"username_trim_whitespace"
):
if not self.get_context_value("username_trim_whitespace"):
return data return data
if "username" not in data: if "username" not in data:

Loading…
Cancel
Save