Commit 39b74652 authored by Łukasz Nowak's avatar Łukasz Nowak

Use decorator in order to extract person document.

parent 071b62c3
...@@ -98,7 +98,8 @@ def responseSupport(anonymous=False): ...@@ -98,7 +98,8 @@ def responseSupport(anonymous=False):
response.setHeader('Access-Control-Allow-Origin', '*') response.setHeader('Access-Control-Allow-Origin', '*')
response.setHeader('Access-Control-Allow-Methods', 'DELETE, PUT, POST, ' response.setHeader('Access-Control-Allow-Methods', 'DELETE, PUT, POST, '
'GET, OPTIONS') 'GET, OPTIONS')
if not anonymous and getSecurityManager().getUser().getId() is None: if not anonymous:
if getSecurityManager().getUser().getId() is None:
# force login # force login
response.setStatus(401) response.setStatus(401)
response.setHeader('WWW-Authenticate', 'Bearer realm="%s"'% response.setHeader('WWW-Authenticate', 'Bearer realm="%s"'%
...@@ -106,6 +107,17 @@ def responseSupport(anonymous=False): ...@@ -106,6 +107,17 @@ def responseSupport(anonymous=False):
response.setHeader('Location', self.getPortalObject()\ response.setHeader('Location', self.getPortalObject()\
.portal_preferences.getPreferredRestApiV1TokenServerUrl()) .portal_preferences.getPreferredRestApiV1TokenServerUrl())
return response return response
else:
self.person = self.getPortalObject().ERP5Site_getAuthenticatedMemberPersonValue()
if self.person is None:
transaction.abort()
LOG('VifibRestApiV1Tool', ERROR,
'Currenty logged in user %r has no Person document.'%
self.getPortalObject().getAuthenticatedMember())
self.REQUEST.response.setStatus(500)
self.REQUEST.response.setBody(json.dumps({'error':
'There is system issue, please try again later.'}))
return self.REQUEST.response
return fn(self, *args, **kwargs) return fn(self, *args, **kwargs)
wrapperResponseSupport.__doc__ = fn.__doc__ wrapperResponseSupport.__doc__ = fn.__doc__
return wrapperResponseSupport return wrapperResponseSupport
...@@ -141,6 +153,7 @@ def extractInstance(fn): ...@@ -141,6 +153,7 @@ def extractInstance(fn):
return self.REQUEST.response return self.REQUEST.response
wrapperExtractInstance.__doc__ = fn.__doc__ wrapperExtractInstance.__doc__ = fn.__doc__
return wrapperExtractInstance return wrapperExtractInstance
class GenericPublisher(Implicit): class GenericPublisher(Implicit):
@responseSupport(True) @responseSupport(True)
def OPTIONS(self, *args, **kwargs): def OPTIONS(self, *args, **kwargs):
...@@ -163,16 +176,6 @@ class InstancePublisher(GenericPublisher): ...@@ -163,16 +176,6 @@ class InstancePublisher(GenericPublisher):
@requireJson(dict(log=unicode)) @requireJson(dict(log=unicode))
@extractInstance @extractInstance
def __bang(self): def __bang(self):
person = self.getPortalObject().ERP5Site_getAuthenticatedMemberPersonValue()
if person is None:
transaction.abort()
LOG('VifibRestApiV1Tool', ERROR,
'Currenty logged in user %r has no Person document.'%
self.getPortalObject().getAuthenticatedMember())
self.REQUEST.response.setStatus(500)
self.REQUEST.response.setBody(json.dumps({'error':
'There is system issue, please try again later.'}))
return self.REQUEST.response
try: try:
self.software_instance.reportComputerPartitionBang(comment=self.jbody['log']) self.software_instance.reportComputerPartitionBang(comment=self.jbody['log'])
except Exception: except Exception:
...@@ -198,17 +201,6 @@ class InstancePublisher(GenericPublisher): ...@@ -198,17 +201,6 @@ class InstancePublisher(GenericPublisher):
)) ))
def __request(self): def __request(self):
response = self.REQUEST.response response = self.REQUEST.response
person = self.getPortalObject().ERP5Site_getAuthenticatedMemberPersonValue()
if person is None:
transaction.abort()
LOG('VifibRestApiV1Tool', ERROR,
'Currenty logged in user %r has no Person document.'%
self.getPortalObject().getAuthenticatedMember())
response.setStatus(500)
response.setBody(json.dumps({'error':
'There is system issue, please try again later.'}))
return response
request_dict = {} request_dict = {}
for k_j, k_i in ( for k_j, k_i in (
('software_release', 'software_release'), ('software_release', 'software_release'),
...@@ -226,7 +218,7 @@ class InstancePublisher(GenericPublisher): ...@@ -226,7 +218,7 @@ class InstancePublisher(GenericPublisher):
request_dict[k_i] = self.jbody[k_j] request_dict[k_i] = self.jbody[k_j]
try: try:
person.requestSoftwareInstance(**request_dict) self.person.requestSoftwareInstance(**request_dict)
except Exception: except Exception:
transaction.abort() transaction.abort()
LOG('VifibRestApiV1Tool', ERROR, LOG('VifibRestApiV1Tool', ERROR,
......
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