afurlan's blog

/^random (nerd|geek)? posts$/

tag: django

Creating the urllist.txt with your sitemaps configuration

Tuesday, December 1, 2009 - Two comments

In Django, you can configure the sitemap of your website using the sitemaps framework and then use this configuration to generate the sitemap.xml. Notwithstanding, the Yahoo! used to support only the urllist.txt type of sitemap and, because of that, I still use to have both (urllist.txt and sitemap.xml) available on my websites.

But, once you have your sitemap configured, why not use it to generate the urllist.txt? I created a view which generates the urllist.txt based on your sitemap configuration, as follows:

# -*- coding: utf-8 -*-

from django.conf import settings
from django.http import HttpResponse
from django.contrib.sites.models import Site

def urllist_from_sitemaps(request, sitemaps):
        urllist = []
        protocol = getattr(settings, 'PROTOCOL', 'http')
        baseurl = u'%s://%s' % (protocol, Site.objects.get_current().domain)
        for cls in sitemaps.values():
                instance = object.__new__(cls)
                for item in instance.items():
                        urllist.append(baseurl + item.get_absolute_url())
        return HttpResponse(u'\n'.join(urllist), mimetype='text/plain')

And now, you just need to configure the urls.py and add the urllist.txt entry:

sitemaps = {
    'entries': EntrySitemap,
    'tags': TagSitemap,
    'archive': ArchiveSitemap,
}

urlpatterns = patterns('',
    ...
    (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
    (r'^urllist.txt$', 'myproj.apps.myapp.views.urllist_from_sitemaps', {'sitemaps': sitemaps}),
    ...
)

And I think that is all for now...

Update 1: I submitted a patch to be added into the official Sitemaps framework of the Django. The patch is very different of the code in this post and, if you'd like to see it and its code, here it is.

As always: if you found some english bug, warn me and I'll be glad to fix it. :)

Hello world, again

Monday, May 25, 2009 - 16 comments

merlin Yep, that's the second time I post a "Hello world" entry here. The reason is that I recently moved my domain from an account at Bluehost to a VPS at Linode. So, first of all, I would like to introduce you to merlin, my new VPS. :)

As I'm the sysadmin behind my server now, I decided to not install mod-php here and then I had to change my blog engine. I used to run Wordpress for years and now I'm running django-diario, a wonderful blog engine for django made by Brazilian djanglers.

About this new version of my old blog, you may have noticed that it has only this post... That's right, I removed the old stuff (in fact I just didn't migrate them from Wordpress) and I intend to start translating and re-posting a small part of them as soon as possible. If you want to see here any specific post from my old blog, please, let me know.

As you can see now I'm writing in English and I used to write in Portuguese... I'm not a professional English writer, so you won't be a lucky guy if you find some bugs around here and if you do find something, please, let me know about it and I will gladly fix it.

You'll probably see at here lots of posts about Python and Debian. This time I'll also try to write more often and in small chunks. :)

I think that's all...