... because from time to time I'm a web developer, too
About me
Projects
Contact
Links

Pingback – receive pings in django

November 18, 2009 | frameworks, python
author: Karol Zielinski | comments: 4 | views: 2146
Tags: , , , ,

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.

Add new URL to urls.py

cd /path_to_our_project
vim urls.py

add there:

(r'^xml-rpc/$', 'my_project.main.views.receive_pingback', {}),

New table in database

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

Receiving pingbacks

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:

  1. source_url – website, which pings your site
  2. target_url – pinged URL
  3. all pingbacks has been saved in database via Trackback model
  4. mail about new pingback to your article has been sent to you

That’s all.

Bookmark and Share
Post Pingback – receive pings in django to develway Post Pingback – receive pings in django to Delicious Post Pingback – receive pings in django to Digg Post Pingback – receive pings in django to Facebook Post Pingback – receive pings in django to Reddit Post Pingback – receive pings in django to StumbleUpon

Related news and resources

Comments (4)

4Avatars v0.3.1 v0.3.1
Kyle Fox
November 20, 2009, 12:39 pm

I think the formatting for your views.py file is breaking the remainder of the blog post.

4Avatars v0.3.1 v0.3.1
Karol Zielinski
November 23, 2009, 8:21 am

What do you mean?

4Avatars v0.3.1 v0.3.1
Bumrassy
December 12, 2009, 3:16 pm

ehh… nice thoughts :) )

4Avatars v0.3.1 v0.3.1
nfo
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

Write a comment

Karol Zielinski :: Just a tech stuff 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.

Most popular posts

Much more links

Karol Zielinski    |   contact me
Gdynia, Poland
RSS - Just a tech stuff - python, java blog - web development blog Karol Zielinski on twitter Karol Zielinski on LinkedIn Karol Zielinski on facebook Karol Zielinski on delicious Karol Zielinski on digg Karol Zielinski on flickr Karol Zielinski on stumbleupon Karol Zielinski on technorati