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

Request not available in signal #225

Open
mschoettle opened this issue Mar 25, 2022 · 2 comments
Open

Request not available in signal #225

mschoettle opened this issue Mar 25, 2022 · 2 comments

Comments

@mschoettle
Copy link
Contributor

mschoettle commented Mar 25, 2022

I am looking into a way to address #165 and noticed that the request is None in _thread_locals. This seems to be a bug (causing the extra work to retrieve the user in request_started). It also means that the user cannot be retrieved from the request in request_started.

It looks like the request is stored and immediately after deleted from the locals object in the middleware.

See:

def __call__(self, request):
_thread_locals.request = request # seems redundant w/process_request, but keeping in for now.
if hasattr(self, 'process_request'):
response = self.process_request(request)
response = response or self.get_response(request)
if hasattr(self, 'process_response'):
response = self.process_response(request, response)
return response

The order of execution is actually:

  1. request_started signal triggered
  2. EasyAuditMiddleware called

I saw in django-structlog that there are custom signals (foo = django.dispatch.Signal()) being used which are triggered from the middleware. This would allow for more fine-grained control.

So maybe something like the following could work:

def __call__(self, request):
    request_started_signal.send(sender=self.__class__, request=request)
    response = self.get_response(request)
    request_finished_signal.send(sender=self.__class__, request=request, response=response)
@steverecio
Copy link
Contributor

@mschoettle did you figure this out? All my request events are suddenly not recording the User object anymore

@mschoettle
Copy link
Contributor Author

No, haven't looked into this in a while but will need to soon.

Is it possible that you upgraded Django and then it stopped working? Which version of Django are you using?

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

2 participants