Django - access session in signal - django

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/

Related

Collect data from multiple Django signals in session or cache

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?

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.

How to trigger an action on session timeout in Django

I wonder how I could trigger an action whenever a session timeout occurs. Is there a way to set up a callback function that would be executed before the session data get destroyed? I need a solution that also works for unauthenticated users.
Assuming that you are using the default session database backend, you can use a Django signal to detect when a session is deleted from the DB, following an approach similar to this:
Django - detect session start and end
Note that in Django 1.5+ the command to use is clearsessions, and not cleanup.
For more details you can refer to the official documentation on clearing the session store.
If the cronjob configuration is not feasible in your case, you can also clear the session store in one of your views, by doing something like this:
from django.core import management
management.call_command('clearsessions')

Django: Create connection between old and new application model

The situation:
I have a database with thousands of users.
I have created a new application called 'my_app'.
my_app has one object called 'my_Object'.
my_object has an a relation one_to_one with the Django User.
class my_object(models.Model):
user = models.OneToOneField(User)
My question is:
How can I create a register for all users already on the database? I know I can use post_save for the new ones, but how can I relate / connect this new object backwards?
Thanks!
Run a SQL query in your favorite database manager that creates records for all the existing User records.
Instead of doing from MySql you could also do it from the django shell. Access all the records from the old user base as a query list and then create a my_object for them. As answered above there is no need for setting the signal, as the task has to be done only once. Also such an app based signal, if possible would be more cumbersome to do.
old_users = Old_users.objects.all() for user in old_users:
object = my_object(user = user)
object.save()
By the way, how have you added the old users to django user?

Django custom manager request object/current user

I have made a custom manager by creating a class inheriting from models.Manager.The manager just changed the default model.objects query to add some filters. Now, I want to add a filter according to the user logged in. I dont want to have to search through code changing what params are added, is there any way I can get the request object/current user without passing it through to the method?
Im hoping this is not a stupid question, but I may just be getting confused...
This is the basic setup of the Manager
class pubManager(models.Manager):
def get_queryset(self):
return pubEnt.objects.filter(state='new')
def on_site(self):
return pubEnt.objects.filter(state='old', val=0)
There is no way in django to access the current request without passing it. If can't live without it you should probably rethink your design! Having access to the request shouldn't be a requirement of a manager's method, since it could also be accessible from somewhere where you do not have a request object (think for example of calling the method from the python shell). If you need access to the currently logged-in user, pass the user object to the method (from request.user), but not the whole request!
Global Django requests
http://nedbatchelder.com/blog/201008/global_django_requests.html