-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathscope.py
More file actions
31 lines (23 loc) · 764 Bytes
/
scope.py
File metadata and controls
31 lines (23 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from contextlib import contextmanager
@contextmanager
def scope_context(context, name, parent):
"""Context manager for setting scope on context.
Useful if ``Field`` or ``FieldSerializer`` implementations want to gather
information from related model.
:param context: ``ScopeContext`` instance.
:param name: The field name in this scope.
:param parent: The field containing model for this scope.
"""
context.name = name
context.parent = parent
try:
yield context
finally:
context.name = context.parent = None
class ScopeContext(object):
"""A scoped context.
"""
name = None
"""Name of the field while scoped."""
parent = None
"""The field containing model while scoped."""