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

FK Opportunity between RequestEvent and CRUDEvent #228

Open
hi5grant opened this issue Apr 28, 2022 · 1 comment
Open

FK Opportunity between RequestEvent and CRUDEvent #228

hi5grant opened this issue Apr 28, 2022 · 1 comment

Comments

@hi5grant
Copy link

Disclaimer: I'm not an expert yet in the Django middleware, signals, or this project's history so forgive me if this comes off totally ignorant.

Is there a way to tie a RequestEvent to CRUDEvent(s)? I've been wondering whether this library would be great to be able to track user activity by each request a user makes and what they changed or read from each. But I don't know if there is anything existing (or even possible) to pass a RequestEvent.id or maybe a new .request_uuid to the CRUDEvents object creation to match them together? Maybe a m2m entity between them? just spitballing.

@hi5grant
Copy link
Author

I did some digging and saw that there isn't a direct way to pass or receive the request object using signals. I noticed that the request object can be accessed in the _threads_local variable. I could see that in being able to use the request object in tying entities together.

   def process_request(self, request):
        _thread_locals.request = request
        return None

However, I know there's contention on whether that's safe/a good idea. I'd like to suggest a different way to store session data that Django supports: https://docs.djangoproject.com/en/4.0/topics/http/sessions/#using-sessions-out-of-views

The examples in this section import the SessionStore object directly from the django.contrib.sessions.backends.db backend. In your own code, you should consider importing SessionStore from the session engine designated by SESSION_ENGINE, as below:

>>> from importlib import import_module
>>> from django.conf import settings
>>> SessionStore = import_module(settings.SESSION_ENGINE).SessionStore

An API is available to manipulate session data outside of a view:

>>> from django.contrib.sessions.backends.db import SessionStore
>>> s = SessionStore()
>>> # stored as seconds since epoch since datetimes are not serializable in JSON.
>>> s['last_login'] = 1376587691
>>> s.create()
>>> s.session_key
'2b1189a188b44ad18c35e113ac6ceead'
>>> s = SessionStore(session_key='2b1189a188b44ad18c35e113ac6ceead')
>>> s['last_login']
1376587691

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

1 participant