Skip to content

feat: add a flask_context_manager. #21061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 17, 2025
Merged

Conversation

laipz8200
Copy link
Member

@laipz8200 laipz8200 commented Jun 16, 2025

Signed-off-by: -LAN- [email protected]

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

part of #20449

Replaces manual context management and user restoration logic with a
unified flask_context_manager for better code maintainability and
clarity. Removes redundant import of has_request_context from
multiple modules. Updates .gitignore to exclude .roo/ directory,
presumably related to AI Assistant outputs.

Screenshots

Before After
... ...

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

Replaces manual context management and user restoration logic with a
unified `flask_context_manager` for better code maintainability and
clarity. Removes redundant import of `has_request_context` from
multiple modules. Updates `.gitignore` to exclude `.roo/` directory,
presumably related to AI Assistant outputs.

Signed-off-by: -LAN- <[email protected]>
@laipz8200 laipz8200 marked this pull request as ready for review June 16, 2025 08:34
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 💪 enhancement New feature or request 📚 documentation Improvements or additions to documentation labels Jun 16, 2025
@laipz8200 laipz8200 requested a review from QuantumGhost June 16, 2025 09:05
@laipz8200 laipz8200 marked this pull request as draft June 16, 2025 09:13
Eliminates the usage of Flask's `copy_current_request_context` decorator
for thread creation across multiple app generators. The change simplifies
thread initialization by directly passing necessary context and parameters
to the worker function. This refactoring reduces complexity and potential
overhead associated with copying request contexts when not required.

Signed-off-by: -LAN- <[email protected]>
Simplifies the `flask_context_manager` function by requiring a
`contextvars.Context` object instead of an optional mapping of
`ContextVar` instances. This change enhances type safety and
streamlines the function's usage, ensuring a more consistent
and clear interface for managing context variables in Flask
applications.

Signed-off-by: -LAN- <[email protected]>
Introduces a note in the flask_context_manager function documentation to clarify
that while the manager facilitates using current_user across threads and app
contexts, directly passing the user in parameters is the preferred approach.

Signed-off-by: -LAN- <[email protected]>
@laipz8200 laipz8200 marked this pull request as ready for review June 16, 2025 09:32
QuantumGhost
QuantumGhost previously approved these changes Jun 16, 2025
Copy link
Contributor

@QuantumGhost QuantumGhost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other changes LGTM.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jun 16, 2025
@laipz8200 laipz8200 requested a review from QuantumGhost June 16, 2025 10:14
Copy link
Contributor

@QuantumGhost QuantumGhost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@crazywoola crazywoola merged commit 0dcacdf into main Jun 17, 2025
10 checks passed
@crazywoola crazywoola deleted the feat/flask-app-context-manager branch June 17, 2025 08:31
jsincorporated pushed a commit to jsincorporated/asaAi that referenced this pull request Jul 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 documentation Improvements or additions to documentation 💪 enhancement New feature or request lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants