One issue that I have had with this blog for a while has been the lack of notifications when a new, legitimate comment is posted under one of my articles. I don’t get many comments, but when I do, I would like to be able to respond right away. I don’t like email notifications, so when Prowl was announced this week, I was excited about the possibility of sending push notifications to my iPhone whenever a new comment was posted. It turned out to be much easier than I anticipated, thanks to Jacob Burch’s Prowlpy module and Django’s signals framework.
To setup push notifications for anything, you need to get these done first:
Register for an account at the Prowl site, then go to iTunes Store and buy Prowl for your iPhone/iPod Touch
After setting up your device, login to your account at the Prowl web site, click on Settings, and generate an API key
Download and install Prowlpy and httplib2. I opted for simply putting Prowlpy somewhere on my PYTHONPATH, and installing httplib2 through aptitude:
sudo apt-get install python-httplib2
Now that you are ready, simply create a signals.py file under whichever Django application you would like to use to send push notifications for, and use something similar to this for sending out messages to the Prowl API:
from django.conf import settings
from django.contrib.sites.models import Site
from prowl.prowlpy import Prowl
def comment_notification(sender, instance, **kwargs):
"""
Connects to the Prowl API to send a push notification
"""
if not instance.pk:
prowl_api = Prowl(settings.PROWL_API_KEY)
site = Site.objects.get_current()
if prowl_api.verify_key():
try:
prowl_api.post(
application="taylanpince.com",
event="New Comment",
description="A new comment has been published under blog post: %(title)s\r\n%(link)s" % {
"title": instance.post.title,
"link": "http://%(domain)s%(path)s" % {
"domain": site.domain,
"path": instance.post.get_absolute_url(),
}
}
)
except Exception:
pass
Obviously, you also need to put your Prowl API key that you got from your account page earlier inside your settings.py file:
PROWL_API_KEY = 'YOUR_API_KEY'
Finally, you have to link this signal to a model. In your models.py file, you can have the model call this method as a pre_save signal:
from django.db.models.signals import pre_save
from blog.signals import comment_notification
class Comment(models.Model):
...
pre_save.connect(comment_notification, Comment)
You could, of course, call this signal post_save as well. That’s up to you. In my case, I also do some spam filtering through the Akismet API, so it makes more sense to handle all of that in a pre_save signal and only send out a notification if a comment passes that check. I suggest you do something similar - it wouldn’t be fun to get push notifications for spam.
Finally, I had to fork the Prawlpy script and make some minor modifications to make sure the content is encoded properly. Unless you comment out the following lines in Prowlpy, you will get urlencoded strings in your notification:
# URL-encode and string-ify keywords. Better type/content testing is needed here
application = urllib.quote(str(application))
event = urllib.quote(str(event))
description = urllib.quote(str(description))
priority = urllib.quote(str(priority))
And that’s it, now I will always be on top of the comments posted here!
sudo apt-get install python-httplib2 worked for me
Awesome, thanks
© Copyright 2001-2010 Taylan Pince. All rights reserved.
Great tip! I’ll have to try this out when I get my iPhone in a couple of weeks. :)