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

Upgrade EasyAuditMiddleware to support sync and async contexts. #291

Open
KaczuH opened this issue Apr 20, 2024 · 2 comments
Open

Upgrade EasyAuditMiddleware to support sync and async contexts. #291

KaczuH opened this issue Apr 20, 2024 · 2 comments

Comments

@KaczuH
Copy link

KaczuH commented Apr 20, 2024

EasyAuditMiddleware currently only supports synchronous code, requiring adaptations by Django for async operation.

As such, projects using easy audit cannot benefit from async stack as shown by the django.request logger:

DEBUG:django.request:Asynchronous handler adapted for middleware easyaudit.middleware.easyaudit.EasyAuditMiddleware.

Relevant Django documentation can be found in Django Docs:.

There is a potential simple fix: replacing threading.local with asgiref and using MiddlewareMixin.

KaczuH pushed a commit to KaczuH/django-easy-audit that referenced this issue 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: soynatan#291
@reckless-huang
Copy link

This is a good PR, why is there no response?

KaczuH pushed a commit to KaczuH/django-easy-audit that referenced this issue Nov 25, 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: 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.
jheld pushed a commit that referenced this issue Nov 25, 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

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.
@jheld
Copy link
Collaborator

jheld commented Nov 26, 2024

1.3.8-a1 has support for this. available on PyPI. I'm sure there will be more pre-releases before 1.3.8 is official, but still the feedback from it being used in the wild is very helpful.

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

No branches or pull requests

3 participants