Pingback – receive pings in django
November 18, 2009 | frameworks, pythonauthor: Karol Zielinski | comments: 4 | views: 2146
Tags: blog, django, ping, pingback, trackback
Ok, I already told you how to ping other sites via django (pingback). Now it’s time for a description how to receive pings from the other bloggers.
Notice: that would be quite easy example. I tested it on pings sending by wordpress, so it should works for them. However… I’m not sure about the rest of blog engines.
I think that it’s an easiest possible implementation of this functionality.
Let’s start.
cd /path_to_our_project vim urls.py
add there:
(r'^xml-rpc/$', 'my_project.main.views.receive_pingback', {}),
vim main/models.py
add there:
class Trackback(models.Model):
news = models.ForeignKey('main.News', related_name='news2Trackback')
target_url = models.CharField('Target url', max_length=255)
source_url = models.CharField('Source url', max_length=255)
title = models.CharField('Title', max_length=255)
content = models.CharField('Content', max_length=255)
remote_ip = models.CharField('Remote IP', max_length=127)
dateCreated = models.DateTimeField('Date created', 'date created')
class Meta:
verbose_name = 'Trackback'
verbose_name_plural = 'Trackbacks'
def __unicode__(self):
return self.news
save the file and run script in command line:
python manage.py syncdb
now… implement method receive_pingback():
cd main/views.py
and add there:
from django.http import HttpResponse from django.core.mail import send_mail from datetime import datetime as datetime2now def receive_pingback(request): if request.method == 'POST' and len(request.POST): my_req = request.POST['.*)\(?P.*)\<\/string") inc = 0 source_url = None target_url = None for ea in line_splitted: match = param_re.match(ea) if match: line_data = match.group('data') if inc == 0: source_url = line_data else: target_url = line_data inc += 1 if source_url and target_url: is_tb = Trackback.objects.filter(source_url = source_url, target_url = target_url) if is_tb: return HttpResponse('OK') splitted_target = target_url.split('/') if len(splitted_target) < 2: return HttpResponse('Bad params!') news_slug = splitted_target[-1] if not news_slug: news_slug = splitted_target[-2] news = News.objects.get(slug = news_slug) if not news: return HttpResponse('Unknown news!') splitted_source = source_url.split('/') if len(splitted_source) < 2: return HttpResponse('Bad params!') title = splitted_source[2] t = Trackback( news = news, target_url = target_url, source_url = source_url, title = title, content = '', remote_ip = request.META['REMOTE_ADDR'], dateCreated = datetime2now.now() ) t.save() emailMessage = 'Trackback to your article: ' + str(target_url) mailFrom = 'my_project.com ' send_mail('New trackback on your website', emailMessage, mailFrom, ['your_email_address'] ) return HttpResponse('OK') else: return HttpResponse('Bad params!') else: status = 'I can handle only POST requests.' return HttpResponse(status)
A bit of description:
That’s all.
Hello, I'm Karol Zielinski, internet evangelist, an entrepreneur, project manager and a web developer from Gdynia, Poland. I like creative design, good advertisement, social media and all kind of stuff around the web.
November 20, 2009, 12:39 pm
I think the formatting for your views.py file is breaking the remainder of the blog post.
November 23, 2009, 8:21 am
What do you mean?
December 12, 2009, 3:16 pm
ehh… nice thoughts
)
January 10, 2010, 10:54 am
hi,
nice article !
a bit more documentation about the relationship with the News model would be nice !
anyway, good work!
nfo