Collect data from multiple Django signals in session or cache - django

I have a view in views.py that hits multiple database models. Each model has its own signals registered with it (pre_save, post_save, post_delete, ...etc). Each signal collects the changes in data for each model.
Now, I want to collect all those changes to use them somehow at the end of my view. So, I though about saving such data temporarily in the user's session or cache to retrieve them just at the ned of my view, but unfortunately the signal functions doesn't take request parameter with it.
So, how can I do such?

Related

Send data to users base on database event in Django channels

I have an application in Django that some users can add and update data. these users add or update data with Django standard forms and views.
I want to implement an other app that send new data to all users when a user update or create data in database. i read about Django-channels that can handle web socket, but i can't find something about server or database events in Django-channels.
So how can i send data to users when a database event occur?
You can use Signals to detect database events in Django. Take a look at the signals explained here: https://docs.djangoproject.com/en/2.1/topics/signals/
Basically, you'll be doing something along the lines of:
#receiver(post_save, sender=ModelClass)
def my_model_save(sender, instance, **kwargs):
# this code will be executed after an instance of ModelClass is saved.
Group(rate_key).send({
"text": "my message"
})

django_auth_ldap vs postgres db using django models

I am creating an app where I store the USERS in a Postgres database with the help of the standard User Model, in my Django app i use Django queries to get all needed information, like "first_name", "username" .. etc
I implemented Django_auth_ldap to start storing user identification data in an Openldap server if i want. But now, i'm confused to how to get the data i used to get using django queries. i don't want to change the behavior in my views, i want to continue using Django queries
This looks like it describes some of what you want: https://django-auth-ldap.readthedocs.io/en/latest/users.html
You can perform arbitrary population of your user models by adding listeners to the Django signal: django_auth_ldap.backend.populate_user. This signal is sent after the user object has been constructed (but not necessarily saved) and any configured attribute mapping has been applied (see below). You can use this to propagate information from the LDAP directory to the user object any way you like. If you need the user object to exist in the database at this point, you can save it in your signal handler or override get_or_build_user(). In either case, the user instance will be saved automatically after the signal handlers are run.

Django 1.10 - Maintaining context object information

My question is, is there a way to maintain the context object over multiple views? Say you are at the home page, you click a link to go to a specific part of the application but you want to maintain the context information as to not excessively query the DB for this information every time you change views.
I know of mixins and such, but these do not seem to maintain the information, or am I wrong? Question is, can I access the context information and pass it to the next view?
In order to maintain state across views, you can use django sessions.
The session framework lets you store and retrieve arbitrary data on a per-site-visitor basis.
You have to enable the session middleware and add the appropriate settings. Once you do, you will have to manually add code to each view to check to see if the session has information related to that view’s context and to update the session with the data from the context.

Single record of model in database

My django app needs to display data on my homepage which it collected from third party. Requesting the information and waiting for response takes about a second, which is too long processing time for a homepage. The data which my app receives doesn't change often, so there is no reason to fetch that data every time homepage is being rendered. Instead, I want to retain the data and have my app make a request only if the last "refresh" has been done more than an hour ago.
Since using global variables in django is apparently a no-no, I'd need to make a database model which will at all times hold a single record. This feels wrong. Is making a one-record table really a way to go here?
Instead of creating a model to cache the remote site's response, you can use Django's caching framework. More specifically, you can cache a specific view and set a timeout for the cached view. See this documentation page for more details on how to do that.

Django - access session in signal

My application saves data in anonymous users’ sessions. I need to access this data in a signal when the user creates his account. I was thinking about using a post_save signal when a User object is created. The problem is, I do not know how to access the session in the signal.
I thought about three possible solutions:
using the SessionStore object (https://docs.djangoproject.com/en/dev/topics/http/sessions/#using-sessions-out-of-views). The problem is, since I do not have access to the session or the cookies in the signal, I cannot get the session key to retrieve the session
signals.py file:
#receiver([post_save], sender=User)
def get_from_session(sender, instance, created, **kwargs):
s = SessionStore(session_key= ???) # how to access the key?
data = s.get(‘my_special_session_data’)
…
modify or wrap the User object, to make the django request an attribute of his, which could be passed with the signal. But I may not implement this solution for the current project, since I have no access to the User object.
handling the session data in the view, but this solution is suboptimal since we want to automatize the process.
Any thought? Thanks in advance.
Django does not the way to do it
But you could use this snippet:
http://djangosnippets.org/snippets/2179/