| | 507 | class ExposedDescriptor(object): |
|---|
| | 508 | """Descriptor used by RESTMethod to tell if it is exposed |
|---|
| | 509 | """ |
|---|
| | 510 | def __get__(self, obj, cls=None): |
|---|
| | 511 | """Return True if the object has a method for the HTTP method of the current request |
|---|
| | 512 | """ |
|---|
| | 513 | if cls is None: cls = obj |
|---|
| | 514 | cp_methodname = cherrypy.request.method |
|---|
| | 515 | methodname = cp_methodname.lower() |
|---|
| | 516 | method = getattr(cls, methodname, None) |
|---|
| | 517 | if callable(method) and getattr(method, 'exposed', False): |
|---|
| | 518 | return True |
|---|
| | 519 | raise cherrypy.HTTPError(405, '%s not allowed on %s' % ( |
|---|
| | 520 | cp_methodname, cherrypy.request.browser_url)) |
|---|
| | 521 | |
|---|
| | 522 | |
|---|
| | 523 | class RESTMethod(object): |
|---|
| | 524 | """Allows REST style dispatch based on different HTTP methods |
|---|
| | 525 | |
|---|
| | 526 | For an example usage see turbogears.tests.test_restmethod |
|---|
| | 527 | """ |
|---|
| | 528 | exposed = ExposedDescriptor() |
|---|
| | 529 | |
|---|
| | 530 | def __init__(self, *l, **kw): |
|---|
| | 531 | methodname = cherrypy.request.method.lower() |
|---|
| | 532 | self.result = getattr(self, methodname)(*l, **kw) |
|---|
| | 533 | |
|---|
| | 534 | def __iter__(self): |
|---|
| | 535 | return iter(self.result) |
|---|
| | 536 | |
|---|
| | 537 | |
|---|