StepInside

Creativity and Inspiration

Entries tagged with "django"

Django Blog: Notifying about new comments by email

Every blogger is interested in comments for his/her blog posts. So do I. Unfortunately there are no so much comments on my blog (only two), but I hope there will be more in future :)

But how could the author of the blog be notified about new comments? One possibility is to create feed for new comments and subscribe to it using your favourite feed reader (f.e. Google Reader). In fact I had been using this approach until recently. But I think that more convenient approach is to be notified by email. Now I am going to describe how I implemented this functionality in my blog.

The problem seems to be simple – just send email after comment object is saved to database. But I am using django.contrib.comments application and can not modify its code.

Fortunately django provides way to hook into process of the object saving. Internally Django (or more strictly speaking post magic-removal version of Django) uses PyDispatch framework – multiple-producer-multiple-consumer signal-dispatching system :) Despite this complex name idea of this framework is really simple: you can produce signals using following method

dispatcher.send(signal=Any, sender=Anonymous,
    
*args, **kwargs)

and register listeners for the signals using:

dispatcher.connect(receiver, signal=Any, sender=Any,
    
weak=True)

Check sources of django.dispatch package for more details.

Django ORM framework supports following signals defined in django.db.models.signals package:

signal when given signal is sent args kwargs
pre_init before model object initialization (from constructor) positional arguments to init keyword arguments to init
post_init after object is initialized None instance = self
pre_save just before saving object to database None instance = self
post_save after saving object to database None instance = self
pre_delete just before deleting object from database None instance = self
post_delete after deleting object from database None instance = self

For all these signals model class serves as sender.

Now our task becomes very easy – write and register post_save signal listener for FreeComment class:

from django.db import models
from django.db.models import signals
from django.dispatch import dispatcher
from django.core.mail import send_mail
from django.template import Context, loader, Template,
    
TemplateDoesNotExist
from django.contrib.comments.models import FreeComment

# send mail on comment
def send_comment_by_mail(instance):
  
comment = instance
  
if comment.content_type.model_class() != Entry:
    
return
  
entry = Entry.objects.get(id__exact = comment.object_id)

  
# templates for mail subject and body
  
try:
    
subject_tmp = loader.get_template("free_comment_subject")
  
except TemplateDoesNotExist:
    
subject_tmp = Template('New comment for entry \
       "{{ entry.title }}" by "{{ comment.person_name }}"'
)
  
try:
    
body_tmp = loader.get_template("free_comment_body")
  
except TemplateDoesNotExist:
    
body_tmp = Template('{{ comment.comment }}')

  
# send email to the user
  
ctx = Context({'entry': entry, 'comment': comment})
  
subject = subject_tmp.render(ctx).decode('utf-8').strip()
  
body = body_tmp.render(ctx).decode('utf-8')
  
entry.author.email_user(subject, body)

# connect signal
dispatcher.connect(send_comment_by_mail,
    
sender = FreeComment,
    
signal = signals.post_save)

Posted by ksh on July 1, 2006 | 10 comments | development, django

Removing the Magic

Finally I found time to rework code of step-inside.org to use current (post magic-removal) version of django. I managed to do this in about half a day. There were only two non-straightforward tasks:

  • Change database schema and migrate data. Fortunately there is very good overview of changes in Django Wiki
  • Initially I have used tagging application from django_stuff library, but now I decided to remove this dependency and implemented my own functionlaity to support tags for blog entries.

After migration I added two new cool features to my blog application: syntax highlighting and sending email for new comments.

I will blog about implementation details in the next posts.

Posted by ksh on June 30, 2006 | 0 comments | django, personal

Development of Personal Site: Part 4 (Django)

Though at my work I am java developer I am fond of python, and wanted to use python for developing dynamic parts of the site (like blog, poll, flickrbrowser).

At first I decided to find all applications that I need and combine them into single web site with consistent look and feel using paste . To my surprise most applications that I have found were just CGI-based web applications or scripts and I decided that it quite a lot of efforts to consistently combine them together.

I continued my investigation and found PyBlosxom . While this blog engine is quite simple and extensible by plugins it is not easy task to configure it properly. Besides at least four blogs mentioned on its users page switched to WordPress, that is written in php. But I don’t want php. One more important point that resources of my VPS are quite limited and it is not very wise to run php+mysql only for blog and python for other applications that I am going to develop.

Still investigating I found post in GvR’s blog about python web frameworks and somebody mentioned django in comments to this post. I had heard about it before, but thought that there is nothing special with it, just some combination of ORM and template library. But after reading very good opinions about it I decided to give it a try.

I was really amazed by this great framework. This exactly what I need. It allows to combine several applications in single site with little or no effort. Though all applications that you are able to see on this site were written by me almost from scratch (and this is different from my original intention to reuse applications as much as possible) it was so easy to create them and allows me to learn django better.
There are a lot of speculation that python should be promoted more aggressively and I think that django really can help python to become more widely-used language.

I imagine that as it becomes more popular a lot of django applications (like blog, forum, poll, CMS, image gallery, etc) will appear and instead of writing everything from scratch it will be possible to specify list of necessary applications (as eggs), configure them, write some base templates and CSS styles to unify look and feel and receive great dynamic web site.

Posted by ksh on April 14, 2006 | 5 comments | development, django, python, web

Calendar

March 2010
MoTuWeThFrSaSu
1234567
891011121314
15161718192021
22232425262728
2930310000

Tags

Archives

RSS feeds