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.
Close this Window Bookmark and Share This Page
Copy HTML:
If you like this then please subscribe to the RSS Feed.
![[Digg]](http://therning.org/magnus/wp-content/plugins/bookmarkify/digg.png)
![[Reddit]](http://therning.org/magnus/wp-content/plugins/bookmarkify/reddit.png)
Leave a comment