mirror of
https://github.com/langgenius/dify.git
synced 2025-11-10 00:11:30 +00:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: lyzno1 <yuanyouhuilyz@gmail.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Hanqing Zhao <sherry9277@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Harry <xh001x@hotmail.com>
67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
import contextvars
|
|
from collections.abc import Iterator
|
|
from contextlib import contextmanager
|
|
from typing import TypeVar
|
|
|
|
from flask import Flask, g
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
@contextmanager
|
|
def preserve_flask_contexts(
|
|
flask_app: Flask,
|
|
context_vars: contextvars.Context,
|
|
) -> Iterator[None]:
|
|
"""
|
|
A context manager that handles:
|
|
1. flask-login's UserProxy copy
|
|
2. ContextVars copy
|
|
3. flask_app.app_context()
|
|
|
|
This context manager ensures that the Flask application context is properly set up,
|
|
the current user is preserved across context boundaries, and any provided context variables
|
|
are set within the new context.
|
|
|
|
Note:
|
|
This manager aims to allow use current_user cross thread and app context,
|
|
but it's not the recommend use, it's better to pass user directly in parameters.
|
|
|
|
Args:
|
|
flask_app: The Flask application instance
|
|
context_vars: contextvars.Context object containing context variables to be set in the new context
|
|
|
|
Yields:
|
|
None
|
|
|
|
Example:
|
|
```python
|
|
with preserve_flask_contexts(flask_app, context_vars=context_vars):
|
|
# Code that needs Flask app context and context variables
|
|
# Current user will be preserved if available
|
|
```
|
|
"""
|
|
# Set context variables if provided
|
|
if context_vars:
|
|
for var, val in context_vars.items():
|
|
var.set(val)
|
|
|
|
# Save current user before entering new app context
|
|
saved_user = None
|
|
# Check for user in g (works in both request context and app context)
|
|
if hasattr(g, "_login_user"):
|
|
saved_user = g._login_user
|
|
|
|
# Enter Flask app context
|
|
with flask_app.app_context():
|
|
try:
|
|
# Restore user in new app context if it was saved
|
|
if saved_user is not None:
|
|
g._login_user = saved_user
|
|
|
|
# Yield control back to the caller
|
|
yield
|
|
finally:
|
|
# Any cleanup can be added here if needed
|
|
pass
|