diff --git a/tildes/tildes/schemas/base.py b/tildes/tildes/schemas/base.py index 48c967f..4c741c4 100644 --- a/tildes/tildes/schemas/base.py +++ b/tildes/tildes/schemas/base.py @@ -6,7 +6,7 @@ from typing import Any from marshmallow import Schema -from tildes.schemas.context import TildesSchemaContextDict +from tildes.schemas.context import TildesSchemaContext, TildesSchemaContextDict class BaseTildesSchema(Schema): @@ -20,3 +20,6 @@ class BaseTildesSchema(Schema): def __init__(self, context: TildesSchemaContextDict = {}, **kwargs: Any): super().__init__(**kwargs) self.context = context + + def get_context_value(self, key: str) -> Any: + return TildesSchemaContext.get(default=self.context).get(key) diff --git a/tildes/tildes/schemas/group.py b/tildes/tildes/schemas/group.py index 9e36cbf..1669595 100644 --- a/tildes/tildes/schemas/group.py +++ b/tildes/tildes/schemas/group.py @@ -49,9 +49,7 @@ class GroupSchema(BaseTildesSchema): ) -> dict: """Prepare the path value before it's validated.""" # 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 if "path" not in data or not isinstance(data["path"], str): diff --git a/tildes/tildes/schemas/user.py b/tildes/tildes/schemas/user.py index 7811afb..133c57e 100644 --- a/tildes/tildes/schemas/user.py +++ b/tildes/tildes/schemas/user.py @@ -66,7 +66,7 @@ class UserSchema(BaseTildesSchema): def anonymize_username(self, data: dict, many: bool) -> dict: """Hide the username if the dumping context specifies to do so.""" # pylint: disable=unused-argument - if not TildesSchemaContext.get(default=self.context).get("hide_username"): + if not self.get_context_value("hide_username"): return data if "username" not in data: @@ -105,9 +105,7 @@ class UserSchema(BaseTildesSchema): Requires check_breached_passwords be True in the schema's context. """ # 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 if is_breached_password(value): @@ -125,9 +123,7 @@ class UserSchema(BaseTildesSchema): Requires username_trim_whitespace be True in the schema's context. """ # 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 if "username" not in data: