Commit b52627e3 authored by Łukasz Nowak's avatar Łukasz Nowak

Make method more extensible.

parent e9c91a5c
...@@ -190,22 +190,25 @@ def responseSupport(anonymous=False): ...@@ -190,22 +190,25 @@ def responseSupport(anonymous=False):
return wrapperResponseSupport return wrapperResponseSupport
return outer return outer
def extractInstance(fn): def extractDocument(portal_type):
def wrapperExtractInstance(self, *args, **kwargs): if not isinstance(portal_type, (list, tuple)):
portal_type = [portal_type]
def outer(fn):
def wrapperExtractDocument(self, *args, **kwargs):
if not self.REQUEST['traverse_subpath']: if not self.REQUEST['traverse_subpath']:
self.REQUEST.response.setStatus(404) self.REQUEST.response.setStatus(404)
return self.REQUEST.response return self.REQUEST.response
instance_path = self.REQUEST['traverse_subpath'][:2] path = self.REQUEST['traverse_subpath'][:2]
try: try:
software_instance = self.getPortalObject().restrictedTraverse(instance_path) document = self.getPortalObject().restrictedTraverse(path)
if getattr(software_instance, 'getPortalType', None) is None or \ if getattr(document, 'getPortalType', None) is None or \
software_instance.getPortalType() not in ('Software Instance', document.getPortalType() not in portal_type:
'Slave Instance'): raise WrongRequest('%r is neiter of %s' % (path, ', '.join(
raise WrongRequest('%r is not an instance' % instance_path) portal_type)))
self.software_instance_url = software_instance.getRelativeUrl() self.document_url = document.getRelativeUrl()
except WrongRequest: except WrongRequest:
LOG('VifibRestApiV1Tool', ERROR, LOG('VifibRestApiV1Tool', ERROR,
'Problem while trying to find instance:', error=True) 'Problem while trying to find document:', error=True)
self.REQUEST.response.setStatus(404) self.REQUEST.response.setStatus(404)
except (Unauthorized, KeyError): except (Unauthorized, KeyError):
self.REQUEST.response.setStatus(404) self.REQUEST.response.setStatus(404)
...@@ -219,8 +222,9 @@ def extractInstance(fn): ...@@ -219,8 +222,9 @@ def extractInstance(fn):
self.REQUEST['traverse_subpath'] = self.REQUEST['traverse_subpath'][2:] self.REQUEST['traverse_subpath'] = self.REQUEST['traverse_subpath'][2:]
return fn(self, *args, **kwargs) return fn(self, *args, **kwargs)
return self.REQUEST.response return self.REQUEST.response
wrapperExtractInstance.__doc__ = fn.__doc__ wrapperExtractDocument.__doc__ = fn.__doc__
return wrapperExtractInstance return wrapperExtractDocument
return outer
class GenericPublisher(Implicit): class GenericPublisher(Implicit):
@responseSupport(True) @responseSupport(True)
...@@ -246,13 +250,13 @@ class InstancePublisher(GenericPublisher): ...@@ -246,13 +250,13 @@ class InstancePublisher(GenericPublisher):
title=(unicode, encode_utf8), title=(unicode, encode_utf8),
connection=dict connection=dict
), ['title', 'connection']) ), ['title', 'connection'])
@extractInstance @extractDocument(['Software Instance', 'Slave Instance'])
def PUT(self): def PUT(self):
"""Instance PUT support""" """Instance PUT support"""
d = {} d = {}
try: try:
self.REQUEST.response.setStatus(204) self.REQUEST.response.setStatus(204)
software_instance = self.restrictedTraverse(self.software_instance_url) software_instance = self.restrictedTraverse(self.document_url)
if 'title' in self.jbody and \ if 'title' in self.jbody and \
software_instance.getTitle() != self.jbody['title']: software_instance.getTitle() != self.jbody['title']:
software_instance.setTitle(self.jbody['title']) software_instance.setTitle(self.jbody['title'])
...@@ -279,10 +283,10 @@ class InstancePublisher(GenericPublisher): ...@@ -279,10 +283,10 @@ class InstancePublisher(GenericPublisher):
@requireHeader({'Accept': 'application/json', @requireHeader({'Accept': 'application/json',
'Content-Type': 'application/json'}) 'Content-Type': 'application/json'})
@requireJson(dict(log=unicode)) @requireJson(dict(log=unicode))
@extractInstance @extractDocument(['Software Instance', 'Slave Instance'])
def __bang(self): def __bang(self):
try: try:
self.restrictedTraverse(self.software_instance_url self.restrictedTraverse(self.document_url
).reportComputerPartitionBang(comment=self.jbody['log']) ).reportComputerPartitionBang(comment=self.jbody['log'])
except Exception: except Exception:
LOG('VifibRestApiV1Tool', ERROR, LOG('VifibRestApiV1Tool', ERROR,
...@@ -340,11 +344,11 @@ class InstancePublisher(GenericPublisher): ...@@ -340,11 +344,11 @@ class InstancePublisher(GenericPublisher):
return self.REQUEST.response return self.REQUEST.response
@requireHeader({'Accept': 'application/json'}) @requireHeader({'Accept': 'application/json'})
@extractInstance @extractDocument(['Software Instance', 'Slave Instance'])
@supportModifiedSince('software_instance_url') @supportModifiedSince('document_url')
def __instance_info(self): def __instance_info(self):
certificate = False certificate = False
software_instance = self.restrictedTraverse(self.software_instance_url) software_instance = self.restrictedTraverse(self.document_url)
if self.REQUEST['traverse_subpath'] and self.REQUEST[ if self.REQUEST['traverse_subpath'] and self.REQUEST[
'traverse_subpath'][-1] == 'certificate': 'traverse_subpath'][-1] == 'certificate':
certificate = True certificate = True
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment