Send data to users base on database event in Django channels - django

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"
})

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?

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.

How to execute code after authentication in Django?

I want to execute one or more functions after a user logs into my site. How is this possible? I looked into Middleware. Djangobook says that I'll need this to run a piece of code on each and every request that Django handles. However, I just need the code run when the authentication happens successfully.
Note: I am using Django Allauth for authentication and I don't have any view of my own to log in users.
You need to tap into Allauth's signals. Specifically the user logged in signal
allauth.account.signals.user_logged_in(request, user)
Sent when a user logs in.
So add code similar to the following in your project.
from django.dispatch.dispatcher import receiver
from allauth.account.signals import user_logged_in
#receiver(user_logged_in, dispatch_uid="unique")
def user_logged_in_(request, user, **kwargs):
print request.user
This code should be in a place that's likely to be read when django starts up. models.py and views.py are good candidates.
As per the official documentation, there is a signal allauth.account.signals.user_logged_in which gets triggered when a user logs in. This can serve your purpose.

Updating when database has changed in Django

I have many records in my database that are displayed on a screen. However, the records are user generated and sometimes have to be removed. Each record has a show field which is initially always set to true. When we get content that has to be removed a human will set it to false in the Django admin interface. When this happens, we need the bad content to be removed from the screen. So my question is, in the Django interface, what is the way to tell when a record has been updated and do something in response to this change?
You should read about signals:
An Idea on how doing this:
from django.core.signals import post_save
from django.dispatch import receiver
#receiver(post_save, sender=MyModel)
def my_handler(sender, instance, created, raw, **kwargs):
if created: # True for save, False for update
...
I think this could help you tell when the record its being updated and when you can do something about it.
But if the user seeing the records wont have to refresh the page so the record is hidden, then you could use websockets to receive that information your signal sent. Or you can just do ajax requests every 20-30 seconds to check all the records and discovery which one is hidden, or you can check a list of latest hidden records that your signal will populate.
Anyway, there is different ways of doing this.