|
|
@ -4,7 +4,7 @@ |
|
|
"""Base Marshmallow schema.""" |
|
|
"""Base Marshmallow schema.""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from typing import Any |
|
|
|
|
|
|
|
|
from typing import Any, Optional |
|
|
from marshmallow import Schema |
|
|
from marshmallow import Schema |
|
|
from tildes.schemas.context import TildesSchemaContext, TildesSchemaContextDict |
|
|
from tildes.schemas.context import TildesSchemaContext, TildesSchemaContextDict |
|
|
|
|
|
|
|
|
@ -17,9 +17,21 @@ class BaseTildesSchema(Schema): |
|
|
|
|
|
|
|
|
context: TildesSchemaContextDict |
|
|
context: TildesSchemaContextDict |
|
|
|
|
|
|
|
|
def __init__(self, context: TildesSchemaContextDict = {}, **kwargs: Any): |
|
|
|
|
|
|
|
|
def __init__( |
|
|
|
|
|
self, context: Optional[TildesSchemaContextDict] = None, **kwargs: Any |
|
|
|
|
|
): |
|
|
|
|
|
"""Pass an optional context, and forward Schema arguments to superclass.""" |
|
|
super().__init__(**kwargs) |
|
|
super().__init__(**kwargs) |
|
|
self.context = context |
|
|
|
|
|
|
|
|
self.context = context if context else {} |
|
|
|
|
|
|
|
|
def get_context_value(self, key: str) -> Any: |
|
|
def get_context_value(self, key: str) -> Any: |
|
|
return TildesSchemaContext.get(default=self.context).get(key) |
|
|
|
|
|
|
|
|
"""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) |