Django urlpatterns - it's more than just urls

In this project we are processing text messages. So peice of text comes into a method and needs to be routed to a particular view/method/class/thing. There's lots of ways of doing it, a big if might be one. RapidSMS includes a damn cunning way of doing it by using decorators.

Here's an example:

    @keyword(r'n(?:ote)? \+(\d+) (.+)')
    @authenticated
    def note_case (self, message, ref_id, note):
         [...]

I thought that was really neat, you can see the decorator here. Nice stuff!

However since I'm a GOD (Grumpy Old Developer) after using it for about 10 minutes I see there's a basic problem I'm not happy with: I find it hard to track what message goes where. There could be 100's of lines of code between each decorator, its hard to see them all at once.

Hmm. So how about using Django's urlpatterns? I actually like the url regex in Django because it's easy to see in one editor window what is going where.

In my case I made a file: msgs.py that simply maps the regex's to the view:

from django.conf.urls.defaults import patterns

urlpatterns = patterns('',
    (r'^country (?P\S+)', "apps.testy.views.country",),
    (r'^list (?P\S+)', "apps.testy.views.list",),
)

Then in my app I can use the URLResolver:

from django.core.urlresolvers import RegexURLResolver, Resolver404

resolver = RegexURLResolver(r'', "apps.testy.msgs")

In my class I'm able to use the resolver in a method, on any text:

       try:
            callback, callback_args, callback_kwargs = resolver.resolve(text)
        except Resolver404:
            raise ValueError, "There was no view found for: %s" % text
            
        response = callback(self, message, *callback_args, **callback_kwargs)

And now I can pass text through to different views based on those regex's. The only problem with it is that (understandably) it's using HTTP semantics. That Resolver404 inherits from a HTTPError, which isn't ideal for a non HTTP source. Also it's kind of annoying having to call it urlpatterns in msgs.py since they aren't really about URL's.

So then that's where I'd say something like: I've ripped urlpatterns out of Django, made it generic, placed it on pypi, then put a Django wrapper around it for URL'ishness and put it back into the Django project. Except I haven't. But if you were doing a project that included doing logic on a bit of text.... perhaps you could do that.

Kenya trip (pt 1)

As you may have heard if you follow twitter, I'm in Kenya at the moment. It's 4.30am, I'm really hot and can't sleep, so here's some ramblings about what I've been up to. A more coherent post on the project will follow once I get back.

About a week an a half ago an opportunity came up to help out on a Django project in Kenya, because a developer on the project wasn't able to go. First off I had to complete a prototype for a local company that's got a project in the pipe. It's a really nice company and a cool project, I hope it goes forward.

So once that was done (over the weekend) I flew out to Kenya on Tuesday night. Less than 5 days after the initial email. The amount of things we did in that time was silly, including moving forward the signing of the paperwork on our newly bought house (Danae got the key today), visiting the doctor for shots and pills, getting insurance for travel, house etc... the list went on.

Anyway the prototype seemed to work well and then I was able to fly out Tuesday evening. First stop Amsterdam 8 hour flight. Yay 9 hour layover there. Then 7 hour flight to Nairobi. I had one hour to change to a flight to my destination - Kisumu. Took 20 minutes to get off the plane. Took 20 minutes to get a visa. Ran over to the other terminal. Got stuck in a 20 minute security line up as a tour bus was let in before me. Got to the counter 5 minutes before the flight departed. Nope.

Next flight was at 5pm - its 7am.

Now I'm not a good person in crowds or around lot's of people. I'm used to very quiet, shy, law-abiding, British repressed Vancouver. It's wonderful, polite non-threating. Where people follow rules. Naples is the exact opposite. We were in Naples a year or two ago and it was hell. Nairobi airport is like Naples, but worse. I'm sure you can get used to it, but I found it intimidating and stressful. Not the kinda place I want to spend a day. Especially when I feel like a zombie.

In the end I got a cab ride to the national museum, found a fabulous but expensive cafe and hung out in that area. I went for a walk into the centre of Nairobi - not much to see. Got back to the airport with 3 hours to spare, just in case. Got into Kisumu about 6.30pm local time on Thursday.

I was knackered - I think that's the longest trip I've ever done (about 35 hours). And I lost my MacBook power supply somewhere along the way.

Some djangozen posts

Added some posts to djangozen.com:

I added in tagging for plugins (the tag cloud doesn't change the fonts because I hate that) and allowed anyone to own and unowned project. I also added in notifications to twitter, follow along at http://twitter.com/djangozen.

And finally I pulled out the plango source which powers a lot of the site and put the source online: http://djangozen.com/plugins/view/plango/.

Next the job board, oh and a bit of traffic would help.

New years resolution

Last year I had the luck to meet some old friends from university. For quite a while the last two years I seemed to spend a large amount of time sat in front of a car steering wheel or a computer monitor.

So one of my resolutions this year is to spend more time with friends. Plus spending more time catching up with old friends. I think that's important. So if you have just been added to my facebook or twitter - lookout.