Hi,
On 9/22/06, James Crasta <[EMAIL PROTECTED]> wrote:
> First, we define a RestfulView base class, which leverages python's
> __call__ functionality.
And now for something completely different: A decorator! :P
OK, but seriously, I quite like the idea of using decorators. Below is
a completely hacked up implementation (I don't understand enough about
python internals to make sure it's a good implementation), along with
some examples.
Russell
class methodDispatcher:
functions = {}
def __init__(self, method=''):
self.method = method
def unResolvable(self, *args, **kwargs):
from django.http import HttpResponseNotAllowed
try:
return HttpResponseNotAllowed(self.functions[self.fname].keys())
except Exception, e:
print "This is just a proof of concept. " \
"We should've actually returned an HttpResponseNotAllowed."
def resolve(self, request, *args, **kwargs):
f = self.functions[self.fname].get(request.method,
self.functions[self.fname].get('', self.unResolvable))
f(request, *args, **kwargs)
def __call__(self, f, *args, **kwargs):
if (not self.functions.has_key(f.__name__)):
self.functions[f.__name__] = {}
self.functions[f.__name__][self.method] = f
self.fname = f.__name__
return self.resolve
@methodDispatcher('GET')
def view_1(request, *args, **kwargs):
print "I am not a poster"
@methodDispatcher('POST')
def view_1(request, *args, **kwargs):
print "I am a poster"
@methodDispatcher('GET')
def view_2(request, *args, **kwargs):
print "I am also not a poster"
@methodDispatcher('')
def view_2(request, *args, **kwargs):
print "I might be a poster ... actually, I'm a %ser" % (request.method, )
class request:
method = 'GET'
view_1(request)
request.method = 'POST'
view_1(request)
view_2(request)
request.method = 'PUT'
view_1(request)
view_2(request)
--
echo http://russell.rucus.net/spam/ | sed 's,t/.*,t,;P;s,.*//,,;s,\.,@,;'
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers
-~----------~----~----~----~------~----~------~--~---