Skip to content
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

Update EasyAuditMiddleware to support async context #292

Merged
merged 1 commit into from
Nov 25, 2024

Conversation

KaczuH
Copy link

@KaczuH KaczuH commented Apr 20, 2024

Replaced standard threading with 'asgiref.local' in EasyAuditMiddleware. Also, made EasyAuditMiddleware extend Django's MiddlewareMixin to automatically handle sync and async execution modes. Github issue: #291

easyaudit/middleware/easyaudit.py Outdated Show resolved Hide resolved
@KaczuH KaczuH requested a review from samamorgan April 27, 2024 01:12
Copy link
Collaborator

@jheld jheld left a comment

Choose a reason for hiding this comment

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

As far I can tell, this is a good modernization effort.

@jheld
Copy link
Collaborator

jheld commented Nov 25, 2024

@KaczuH can you please squash these commits before I merge?

Replaced standard threading with 'asgiref.local' in EasyAuditMiddleware. Also, made EasyAuditMiddleware extend Django's MiddlewareMixin to automatically handle sync and async execution modes.
Github issue: soynatan#291

Update EasyAuditMiddleware for async compatibility

The EasyAuditMiddleware class has been updated to be compatible with both synchronous and asynchronous processes. The class initialization now checks if the 'get_response' function is a coroutine and sets the class' async capability accordingly. An '__acall__' method has been introduced to handle asynchronous calls.

Refactor sync_to_async calls in test_main.py

Replaced sync_to_async keyword usage throughout the test_main.py file with direct asyncio calls, specifically in the ASGIRequestEvent tests. This change both simplifies the code and reduces reliance on the sync_to_async function.

Add test for async capability in middleware

A new test has been added to verify the async capability of the EasyAuditMiddleware. This ensures that the Django logger does not emit a debug message for asynchronous handler adaptation for the middleware. This is aligned with the Django documentation recommendations on async views.
@KaczuH
Copy link
Author

KaczuH commented Nov 25, 2024

@jheld Done!

@jheld jheld merged commit 137a794 into soynatan:master Nov 25, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants