In a Django project I have a space in the templates to show the messages coming from the Django messages framework. I would like to use that framework to show some messages that are not triggered by user actions in some views but instead are set by an app. Ideally my app will decide when it is best to send the messages but initially it will be random. An example to clarify a bit; I don't want to send the typical message related to an action like 'you have successfully updated your profile'. Instead I would like to recommend some content from times to times, something like 'you might be interested by spam'. The content ('spam' in this case) is provided by some objects in the database that another app gathers already.
I was wondering how I can set a message outside of the typical view. I was thinking about implementing a middleware that would be called on every request and setting the message in the process-request() class if this particular user has been marked to receive any.
Is it the even a good idea to begin with?
Related
Hello I wonder is there a way to send push notifications with Django to a user.
I have a website that accepts/refuses vacation demands.
When a user sends a vacation demand my Django app sends email to the CEO to notify him that there was a new vacation request.
When the CEO accepts the demand it sends email to the user that the demand was accepted.
But since the CEO receives plenty of emails a day and he barely sees my emails i would like to make a browser notification whenever he opens the browser to see notification from my website that a demand is waiting to be approved/refused.
Is there a library that can do that for me,
I've tried django-webpush but I couldn't managed it to work even though I
followed all the steps.
Yes you can, since your have the information that your user accessed your server at least once. checkout this lib
https://github.com/jazzband/django-push-notifications
EDIT Gonna put more information about it
If you expect receive one response from your backend to your backend you can write some watcher to receive new data, or create one plugin, or use sockets or even make your frontend send one call to backend with some interval time to check if there is any new messages...
Lets split up a bit
1 - Watcher
Using watchers you can just watch your backend to any changes... build it from scratch i thing i a bit "hard", you can use some modern frontend framework that already have it like Angular, React, Vue... and capture new incomes messages from your backend and create Notification instance in your browser and your it to your user (i guess they will have to keep the page openned to do it... im not 100% sure)
2 - Plugins
You can build one plugin to add to your browser and receive the data from your server... since you already in browser is more easier to use browser functions
3 - Sockets
The common way to make 2 ways comunication from frontend to backend, most used with chats and things arround that, just create one channel of communications between this 2 sides and you will be able to send and receive messages from frontend or backend
4 - Dirty Way
If you not get the time to implement it like supposed to do with quality you can go the dirty way, just setup one ajax in your page to check your backend to new messages every 5 minutes? or more or less... and if find any new data (of course you will have to handle it on your backend like any other suggestions above) and then you create one new notification in your browser and show to your user...
Im sure there is bunch of libs that already do most of things to you, so just search a bit and test until you find anything that fits your need
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.
I am creating a testing app in django. I want to be able to store the start time (when the page loads) and the end time (when the student hits submit) on every question. I realize that I could do something in the view function, however I am afraid that the latency between server and client would make any calculation meaningless. I would ideally like to calculate the start and end times on the client and send them back to the server using django's tag system.
I am not sure the server-client latency is really meaningful for your app, but you could shoot two calls, initiated by the client in Javascript, one when the document is ready and one when the user submits the answer. You would send the current timestamp in those calls.
You would then analyse those calls on the server's side with django views.
You can use hidden form fields. Just initialize start time on page load and end time before page submit using javascript.
in a django view it will be accesable under request.POST
Common use case:
User select item to add to cart
User makes payment via off site payment gateway such as paypal or
worldpay
User gets redirect to payment page and makes payment
Payment portal sends a POST request to a callback URL
User gets redirected back to your page
On Step 4, typically the following things happen:
Error handling and anti fraud checking
Updating of order/cart models and additional logic
My question is pertaining to this step 4:
In apps like Django-Paypal, rather than do all the logic processing on the callback url view function, signals are used instead. Is there a good reason for this? Why not just do all the logic on the callback url view function?
The use of signals decouples django-paypal from your own apps. You can have all kinds of crazy custom stuff happening on payment success or failure in your projects and still use the default provided view.
The class based views in Django 1.3 do make it possible to extend views, and provide an alternative way to decoupling an apps view.
Other considerations you should have before putting logic in views is time; if logic could take a long time (like any I/O), ask yourself if they are crucial to the Response at hand and consider putting it in a task queue, so you can handle the Request quickly, without blocking.
I mean, there's any generic app that you can use to make notifications like when in Facebook, someone adds you as friend, or invite you to an event?
Basically, I need to show to the user this type of notification for different contents type, with the possibility to do some custom actions (ignore, accept, etc) different for each one.
I wonder if someone have done this before, so I can plug it and create a type of notification simply passing the text of the notification, the options that must show and the views to call for each option.
Thanks.
django-notifications is a GitHub notifications alike app, and it's based on Django Activity Stream.
If you familia with django-activity-stream, the the usage of django-notifications almost the same.
django-notifications also provide notifications_unread templatetag to display unread notifications of current login user.
Django Activity Stream does this, for the most part. It's a generic relationship manager that watches for save events in the datbase, and when a condition is met it puts an "event happened!" record into its own tables.
It would be incumbent upon you to then present that feed of events to the user, along with links to the actions (specific to your project) that you want him to take.
Even if it's not what you want, it's an excellent example of how to start.
Maybe this is more closer to my needs:
django-notification
https://github.com/jtauber/django-notification
any experience with that?
There is also django-notify: http://code.google.com/p/django-notify/