Django is cute

I’ve been playing around a bit more with django, and writing simple web services in it. I have to say it’s cute!

Building on my simple authentication middleware I added a second decorator that extract argument out of the URL and passes them to the function as regular arguments:

def extract_arguments(required, optional):
    def _wrapper(func):
        def _iwrapper(request):
            kwargs = {}
            try:
                for r in required:
                    kwargs[r] = request.GET[r]
            except MultiValueDictKeyError, e:
                return HttpResponse('Failure')
            for o in optional:
                if request.GET.get(o, ''):
                    kwargs[o] = request.GET[o]
            return func(request, **kwargs)
        return _iwrapper
    return _wrapper

This allows me to write functions like this

@extract_arguments(['req1', 'req2'], ['opt1', 'opt2'])
def exposed_func(req1, req2, opt1='default', opt2=''):
    # perform magic

Yes it’s lacking in type information, but it shouldn’t be too difficult to add that if the need arises. For now string are enough.

I’m also impressed with the documentation. It didn’t take me long to dig out unique_together in the model reference. I have to admit I was stuck at the thought of a generic unique_for field option, luckily I scrolled down and read through the meta options as well.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>