Catch initiator request in Django signal handler - django

I am working on a Python/Django project. I use Django signals in order to do an operation after creation of new instances of a model.
The question is: Is there any trick to access the request that has triggered this signal? e.g. to use its user field and do some operations based on it.

Related

Django signals with ATOMIC_REQUESTS=True

Using Django (and DRF)
If I'm set ATOMIC_REQUESTS=True on the Database config, would this apply for signals as well?
I'm trying to understand if there is a chance our transaction is finished after response

Why use signals in Django?

I have read lots of documentation and articles about using signals in Django, but I cannot understand the concept.
What is the purpose of using signals in Django?
How does it work?
Please explain the concept of signals and how to use it in Django code.
The Django Signals is a strategy to allow decoupled applications to get notified when certain events occur. Let’s say you want to invalidate a cached page everytime a given model instance is updated, but there are several places in your code base that this model can be updated. You can do that using signals, hooking some pieces of code to be executed everytime this specific model’s save method is trigged.
Another common use case is when you have extended the Custom Django User by using the Profile strategy through a one-to-one relationship. What we usually do is use a “signal dispatcher” to listen for the User’s post_save event to also update the Profile instance as well.

where should i handle things like triggering notifications in django rest framework

From my understanding the objects in django rest framework have specific purposes
Model:
Representation of the data in a database
Serializer:
Converts models to json and requests to models (crud operations)
View:
External interface to the application. Accepting requests and returning the appropriate responses
If I want to trigger socket push notifications or do anything else that doesn't exactly fit into the three objects by the definition I gave, where would I put it?
Example:
Let's say there is an object ChatMessage and whenever it is created some data needs to be sent to a web socket to notify users. What is the appropriate place to handle that?

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.

Correct way of initializing internal data for API in Django with Piston/Django-tastypie vs jsonrpc

After some API implementations, using jsonrpclib in Python, I need to migrate them inside a Django Framework project. I am quite new in Django and Piston/tastypie, but have some experience using jsonrpc/xmlrpc libs in my Python apps.
Until now I have developed some modules, with a ServiceClass attached to the register of jsonrpc server who handle the request and call the methods in the ServiceClass.
When the class is attached to the register, a new instance of the ServiceClass is created, loading all the initial data and keeping it in memory, so every method called through jsonrpc can have access to the internal values in that instance.
Now, I am trying to do the same in Django with Piston or Tastypie. I followed this link http://www.robertshady.com/content/creating-very-basic-api-using-python-django-and-piston and other resources, and all the documentation I read is clear, showing the correct way to work with it:
Modify url.py to map requests like "/api/" to a specific handler.
Add a handler.py in the api application, extending the BaseHandler of
Piston/Tastypie.
So I am wondering if its the correct way of working with Django and APIs, to create the instance of my ServiceClass (init the data, provide the methods) inside handler.py when I create the instance of the Handler extending the BaseHandler. Is this Handler class instantiated once when the server starts? What if my ServiceClass relies on some Model to load the data from it?
I want to avoid the framework to instantiate my class everytime a new request arrive to the /api/ application.
I will be glad to hear about any recommendation,
Thanks,
Specifically for piston... You shouldnt really use the handler in terms of an instance. Its more like a metaclass that you set up with class attributes. These attributes control whicch model the handler will be bound to if any. And what fields it should show or what methods it supports.
Generally the request enter one of your methods and you then handle the request however you want, as an isolated state. If it needs to use a shared resource or use the model for queries, that part is up to you, being shared from some imported resource . You said you need a model which is why you would bind it to the handler as a class attribute and then query on it. You shouldnt really store state on the handler.