How to make notifications from one app to another app in django? - django

I am making one e-commerce website and i am just trying to make notifications from admin side to user side both are diffrent app in django...
My problems is how to make notifications in admin side (one app)when i click the button send the messages and display the notifications or message in userside(other app)

If I understood your question correctly, you want to notify all the users ( for example for a new offer) when they login to their accounts.
For this matter, you must create a model for the notification (and if it's user-specified there is need for a foreign key to the user in the notification model or maybe you want to show the notification to all users by date this varies duo to your application) and in the profile view just make a query on notifications and show them to the user.
but if you want a chat room like notification (show notification to online users only) just use Django channels.

Related

Is there anyway to initiate chat in dialogflow?

I am working in Dialogflow right now, is there any way to initiate chat using webhooks instead of using the common welcome massages given by dialogflow or some chat apps. Like I want to have my own initialize chats for many different situations. For example, today is my day to save money, then it will initiate a chat to make sure user get into save_money_daily intent. More details on the comment
There are few things that you wanted to do here that are not natural, I guess. Let me go on each:
How can I send an automatic chat for every possible case from the backend automatically?
Ans: You are building a google assistant on dialog flow, using as a chatbot, any chatbot can not send an automatic message(unless set by user), chatbots, and assistants are in the nature of the user interaction.
Let's any bot on Facebook, Skype, or any platform even the google assistant, they can't popup unwantedly with any information, it will be not user friendly.
There is a way to do, like if the user is interacting with your bot: You can ask like:
'Do you want me to remind you for saving money every day'
If user gives permission, save it in-app local cache and send app notification, via. Once the user get the app notification you can invoke the chat on the app notification click and open chat screen with your reply, but in the background when the user clicks on app notification you have to send a request to initiate chat.
And this solves your second problem as well.
Let me give you a high-level example:
Three Intent: With some user utterance
GetLatestMoive: ['get me the latest moives list']
GetLatestNews: ['Headline for today']
HealthCheck: ['I want health tips']
Create Notification and send it to the user via the app:
Based on the notification if the user clicks any of them, initiate your chat in-app with that utterance, it will automatically call the intent, and rest will follow based on your intent follow up a map and conversation design.
Hope this helps!

How can i create a notification system by django

Is there anyway to create a notification system for my website by django without django channels.
If there is way how?
If you only need to send something (not in realtime) you can configure django to send emails.
If you don't need realtime you can save the notification in your DB like a model and inform the user with a badge somewhere in your front-end to invite him to read it.
But if you want realtime notification to clients on your app I think Websoket and Django Channels are the easier way.

create facebook like notification system in django

Say user a liked user b. I want to show this as a notification in the notification panel of user b when she logs in. She can also view her previous notifications (basically like facebook notification). This is not email notification or a success/warning type one time notification.
I want to know the right track to follow. Do I use django-notifications or django-messages-framework or push notifications? Where is the use of channels and web sockets in all of these?
My steps:
1) create a notification app
2) create a notification model (what fields do I put here if since django-notification already has actor, verb, action object and target fields?)
3) Do i have to create forms.py? How is the notification message getting passed?
4) What view do I put in views.py? (please give an example code)
If there is any example project that implemented the notification system please provide a link.
If you're using django-notifications you can just create an object with the actor as user_a, verb as 'like' or any other action performed, recipient would be user_b
So something like
notify.send(request.user, verb='liked', recipient=[target_user])
When user_b logs in you can fetch all the notification (or maybe just the unread ones) and display it
user.notifications.active()
user.notifications.unread()

Using Firebase to make a simple group chat app, alongside an already existing django-react app

I have a basic project which uses django as backend and reactJS as frontend. Basically, it just shows a home page when a user logs in, and that's it. The sign up of new users is handled through the django.admin panel.
Now, I want to create a group chat for my users who are currently logged in using firebase. Here's the problem since I can't really understand the workflow on how I should proceed. My basic idea is that,
frontend gets the username and password from backend,
frontend posts them to firebase
firebase sends a unique id token to the frontend,
now frontend is logged in with both django and firebase,
users who are logged in joins in the group chat
Is there any guidelines on how I should proceed with this? I have read the docs but I can't really understand what I should be doing to go through with this.
You want to look at the docs for firebase.database().ref().on(<event here>) where <event here> is one of the firebase database events such as on, child_added, value
basically what you'll be doing is rendering a database ref, say: firebase.database().ref('livechat').on('value', snap => {render the snapshot}) to every client (to view the chat). Then, a user posts to the 'livechat' ref. You can add as much additional info to the users posted data (like uid) so that you can later filter the 'livechat' node by uid (or the like). Make sense?
Where it gets a bit trickier is when you want to have 1 on 1 chats. You can accomplish this in much the same way - maybe use one of the users uid as the ref. Like: firebase.database().ref(`chat/${user.uid}`).push(message_here so that when you render the chat in the client you have individual conversations.

How to use Pusher with Django?

I am trying to build an app using pusher and django. I went through few of the links like https://github.com/pusher/django-pusherable, but it lacked an example and thus was difficult to understand! Can anyone please help in here?
And also what are channels in here and thus how to create a follow-following system with feeds(activity streams)?
Thanks!
Pusher allows you to easily implement a publish/subscribe pattern for messaging (also called pub/sub for short).
In this pattern, there are a number of channels. Each channel is like a radio station's frequency. A publisher puts messages on a channel, and any subscribers (listeners) that are listening to that channel will receive the message.
The publisher does not know how many people are listening to a particular channel, it just sends the message. It is up to the subscribers to listen to the channels that they are interested in.
Practically speaking, a channel is usually contains an event type; so subscribers can decide what to do with the data depending on the event type. This is sometimes also called a message class.
For example, stock updates can be a channel. The publisher (your backend script) will push a message to this channel whenever there is a change in stock; any and all clients listening on this channel will get that message.
Read more about channels at the API guide for channels.
Pusher takes care of managing the channels and giving you the tools to write listeners.
In your example each user would have their own activity stream channel. Followers (these can be users) can subscribe to listen on the channel of the user they are interested in.
Your system simply publishes updates for all channels.
In code, this would work like this (example from the pusher docs) - from the publisher (backend) side:
from pusher import Pusher
pusher.trigger(u'test-channel', u'my-event', {u'message': u'hello world'})
From the consumer (client) side:
var channel = pusher.subscribe('test-channel');
channel.bind('my-event', function(data) {
alert('An event was triggered with message: ' + data.message);
});
Once that is clear, lets move to django.
The django-pusherable module just makes it easy to create channels by decorating your views.
Each view that is decorated will automatically have a channel created for the object being accessed in the view. Each object gets its own channel, named modelclass_pk, so if your model is called Book, and you just created your first book, the channel will be called Book_1.
from pusherable.mixins import PusherDetailMixin, PusherUpdateMixin
class BookDetail(PusherDetailMixin, DetailView):
model = Book
class BookUpdate(PusherUpdateMixin, UpdateView):
model = Book
This takes care of the backend (pushing messages).
On the front end (client, reading messages), there are a few template tags provided for you. These tags just import the necessary javascript and help subscribe you to the correct events.
There are two default events for each model, update and view.
Now, suppose you want to know whenever the book with id 1 is updated, and automatically update the page, in your templates you would write the following. obj is the the object for book:
{% load pusherable_tags %}
{% pusherable_script %}
{% pusherable_subscribe 'update' obj %}
<script>
function pusherable_notify(event, data) {
console.log(data.user + "has begun to " + event + " " + data.model);
}
</script>
In your backend, you would call this view with a specific book:
def book_update(request):
obj = get_object_or_404(Book, pk=1)
return render(request, 'update.html', {'obj': obj})
Now open that view in a new browser tab.
In another browser tab, or in the django shell - update the book with id 1, and you'll notice the javascript console will automatically log your changes.
How can I use it if I have 2 classes in my database like say,one for
question and one for options, after creating one question it should
appear in the feeds of its followers and along with options, Do I have
to push the options also? How to do this?
Pusher does not care what your database classes are, or what your database relationships are. You have to figure this out yourself.
Pusher's job is limited to making the "live update" happen on the browser without the user having to refresh the page.
Plus how to create relationships, i.e when an user follows another how
to subscribe to it and show related feeds?
I think you don't quite understand what is Pusher's role in all this.
Pusher doesn't care about your database and it has no knowledge about your relationships in the database, what object relates to what and who is following whom.
All pusher does is makes so that one page on the browser will automatically update without the user having to refresh.
The logic to "follow" another user should already be created in your application. That is, you must have a view that allows a user to follow someone else. Once they follow someone, a record will be created/updated in the database. This action will trigger Pusher to publish a message for that database object. Now, whoever is listening on that channel will receive that message, and then can do whatever they want with it.
Here is the order of events/development:
First, create your application as normal. It should have all the features that you expect. If this is a social network, people should be able to follow others and refresh their profile page to see any updates from their followers.
The system should already "know" what is an update and what content is stored for each entity. So, if you are creating "users" and "followers", there should already be the forms, screens, logic, database tables, etc. to make sure that content can be added, updated, by the correct users.
Once you have all that in place correctly and working as you like, now you bring in Pusher; and then you decide which "event" do you want to have automatically updated in the browser.
Suppose the event is "whenever a user adds new content to the site, all their followers should be notified". So you would then do the following:
Go to the view that is executed when a user posts new content.
Update that view as described above, inheriting from PusherUpdateMixin
Go to the template that is shown for users where all their followers are shown. In this template code, add the tags described above to include the pusher javascript api.
Next, in the same template, you will have code that lists all the users this user is following, in that logic code, you can then add a div which will be updated "automatically" whenever that user posts an update.