mirror of
https://github.com/peeringdb/peeringdb.git
synced 2024-05-11 05:55:09 +00:00
* module docstrings * db schema graph * dev docs first pass * dev docs pass 2 * add generated notification to top of generated docs files * linting * regen docs Co-authored-by: Stefan Pratter <stefan@20c.com> Co-authored-by: Sunshine Buchholz <sunshine@20c.com>
34 lines
690 B
Python
34 lines
690 B
Python
"""
|
|
Define custom context managers.
|
|
"""
|
|
|
|
import contextvars
|
|
from contextlib import contextmanager
|
|
|
|
# stores current request in a thread safe context aware
|
|
# manner.
|
|
_current_request = contextvars.ContextVar("current_request")
|
|
|
|
|
|
@contextmanager
|
|
def current_request(request=None):
|
|
|
|
"""
|
|
Will yield the current request, if there is one.
|
|
|
|
To se the current request for the context pass it to
|
|
the request parameter.
|
|
"""
|
|
|
|
if request:
|
|
token = _current_request.set(request)
|
|
else:
|
|
token = None
|
|
try:
|
|
yield _current_request.get()
|
|
except LookupError:
|
|
yield None
|
|
finally:
|
|
if token:
|
|
_current_request.reset(token)
|