Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.core
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Boxiang Sun
slapos.core
Commits
314a8050
Commit
314a8050
authored
May 09, 2012
by
Łukasz Nowak
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Factorise json checking.
parent
a27a5131
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
47 deletions
+44
-47
master/product/Vifib/Tool/VifibRestApiV1Tool.py
master/product/Vifib/Tool/VifibRestApiV1Tool.py
+44
-47
No files found.
master/product/Vifib/Tool/VifibRestApiV1Tool.py
View file @
314a8050
...
@@ -60,6 +60,34 @@ def requireHeader(header_dict):
...
@@ -60,6 +60,34 @@ def requireHeader(header_dict):
return
wrapperRequireHeader
return
wrapperRequireHeader
return
outer
return
outer
def
requireJson
(
json_dict
):
def
outer
(
fn
):
def
wrapperRequireJson
(
self
,
*
args
,
**
kwargs
):
self
.
REQUEST
.
stdin
.
seek
(
0
)
try
:
self
.
jbody
=
json
.
load
(
self
.
REQUEST
.
stdin
)
except
Exception
:
self
.
REQUEST
.
response
.
setStatus
(
400
)
self
.
REQUEST
.
response
.
setBody
(
json
.
dumps
(
{
'error'
:
'Data is not json object.'
}))
return
self
.
REQUEST
.
response
else
:
error_dict
=
{}
for
key
,
type_
in
json_dict
.
iteritems
():
if
key
not
in
self
.
jbody
:
error_dict
[
key
]
=
'Missing.'
elif
not
isinstance
(
self
.
jbody
[
key
],
type_
):
error_dict
[
key
]
=
'%s is not %s.'
%
(
type
(
self
.
jbody
[
key
]).
__name__
,
type_
.
__name__
)
if
error_dict
:
self
.
REQUEST
.
response
.
setStatus
(
400
)
self
.
REQUEST
.
response
.
setBody
(
json
.
dumps
(
error_dict
))
return
self
.
REQUEST
.
response
return
fn
(
self
,
*
args
,
**
kwargs
)
wrapperRequireJson
.
__doc__
=
fn
.
__doc__
return
wrapperRequireJson
return
outer
def
responseSupport
(
anonymous
=
False
):
def
responseSupport
(
anonymous
=
False
):
def
outer
(
fn
):
def
outer
(
fn
):
def
wrapperResponseSupport
(
self
,
*
args
,
**
kwargs
):
def
wrapperResponseSupport
(
self
,
*
args
,
**
kwargs
):
...
@@ -102,16 +130,8 @@ class InstancePublisher(GenericPublisher):
...
@@ -102,16 +130,8 @@ class InstancePublisher(GenericPublisher):
@
requireHeader
({
'Accept'
:
'application/json'
,
@
requireHeader
({
'Accept'
:
'application/json'
,
'Content-Type'
:
'application/json'
})
'Content-Type'
:
'application/json'
})
@
requireJson
(
dict
(
log
=
unicode
))
def
__bang
(
self
):
def
__bang
(
self
):
self
.
REQUEST
.
stdin
.
seek
(
0
)
try
:
jbody
=
json
.
load
(
self
.
REQUEST
.
stdin
)
except
Exception
:
self
.
REQUEST
.
response
.
setStatus
(
400
)
self
.
REQUEST
.
response
.
setBody
(
json
.
dumps
(
{
'error'
:
'Data is not json object.'
}))
return
self
.
REQUEST
.
response
person
=
self
.
getPortalObject
().
ERP5Site_getAuthenticatedMemberPersonValue
()
person
=
self
.
getPortalObject
().
ERP5Site_getAuthenticatedMemberPersonValue
()
if
person
is
None
:
if
person
is
None
:
transaction
.
abort
()
transaction
.
abort
()
...
@@ -123,19 +143,6 @@ class InstancePublisher(GenericPublisher):
...
@@ -123,19 +143,6 @@ class InstancePublisher(GenericPublisher):
'There is system issue, please try again later.'
}))
'There is system issue, please try again later.'
}))
return
self
.
REQUEST
.
response
return
self
.
REQUEST
.
response
instance_path
=
'/'
.
join
(
self
.
REQUEST
[
'traverse_subpath'
][:
-
1
])
instance_path
=
'/'
.
join
(
self
.
REQUEST
[
'traverse_subpath'
][:
-
1
])
error_dict
=
{}
if
'log'
not
in
jbody
:
error_dict
[
'log'
]
=
"Missing."
elif
not
isinstance
(
jbody
[
'log'
],
unicode
):
error_dict
[
'log'
]
=
'Not a string.'
else
:
log
=
str
(
jbody
[
'log'
])
if
error_dict
:
self
.
REQUEST
.
response
.
setStatus
(
400
)
self
.
REQUEST
.
response
.
setBody
(
json
.
dumps
(
error_dict
))
return
self
.
REQUEST
.
response
try
:
try
:
software_instance
=
self
.
restrictedTraverse
(
instance_path
)
software_instance
=
self
.
restrictedTraverse
(
instance_path
)
if
getattr
(
software_instance
,
'getPortalType'
,
None
)
is
None
or
\
if
getattr
(
software_instance
,
'getPortalType'
,
None
)
is
None
or
\
...
@@ -156,7 +163,7 @@ class InstancePublisher(GenericPublisher):
...
@@ -156,7 +163,7 @@ class InstancePublisher(GenericPublisher):
'There is system issue, please try again later.'
}))
'There is system issue, please try again later.'
}))
else
:
else
:
try
:
try
:
software_instance
.
reportComputerPartitionBang
(
comment
=
log
)
software_instance
.
reportComputerPartitionBang
(
comment
=
self
.
jbody
[
'log'
]
)
except
Exception
:
except
Exception
:
LOG
(
'VifibRestApiV1Tool'
,
ERROR
,
LOG
(
'VifibRestApiV1Tool'
,
ERROR
,
'Problem while trying to generate instance dict:'
,
error
=
True
)
'Problem while trying to generate instance dict:'
,
error
=
True
)
...
@@ -169,16 +176,17 @@ class InstancePublisher(GenericPublisher):
...
@@ -169,16 +176,17 @@ class InstancePublisher(GenericPublisher):
@
requireHeader
({
'Accept'
:
'application/json'
,
@
requireHeader
({
'Accept'
:
'application/json'
,
'Content-Type'
:
'application/json'
})
'Content-Type'
:
'application/json'
})
@
requireJson
(
dict
(
slave
=
bool
,
software_release
=
unicode
,
title
=
unicode
,
software_type
=
unicode
,
parameter
=
dict
,
sla
=
dict
,
status
=
unicode
))
def
__request
(
self
):
def
__request
(
self
):
response
=
self
.
REQUEST
.
response
response
=
self
.
REQUEST
.
response
self
.
REQUEST
.
stdin
.
seek
(
0
)
try
:
jbody
=
json
.
load
(
self
.
REQUEST
.
stdin
)
except
Exception
:
response
.
setStatus
(
400
)
response
.
setBody
(
json
.
dumps
({
'error'
:
'Data is not json object.'
}))
return
response
person
=
self
.
getPortalObject
().
ERP5Site_getAuthenticatedMemberPersonValue
()
person
=
self
.
getPortalObject
().
ERP5Site_getAuthenticatedMemberPersonValue
()
if
person
is
None
:
if
person
is
None
:
transaction
.
abort
()
transaction
.
abort
()
...
@@ -191,10 +199,6 @@ class InstancePublisher(GenericPublisher):
...
@@ -191,10 +199,6 @@ class InstancePublisher(GenericPublisher):
return
response
return
response
request_dict
=
{}
request_dict
=
{}
error_dict
=
{}
if
'slave'
in
jbody
:
if
not
isinstance
(
jbody
[
'slave'
],
bool
):
error_dict
[
'slave'
]
=
'Not boolean.'
for
k_j
,
k_i
in
(
for
k_j
,
k_i
in
(
(
'software_release'
,
'software_release'
),
(
'software_release'
,
'software_release'
),
(
'title'
,
'software_title'
),
(
'title'
,
'software_title'
),
...
@@ -204,18 +208,11 @@ class InstancePublisher(GenericPublisher):
...
@@ -204,18 +208,11 @@ class InstancePublisher(GenericPublisher):
(
'slave'
,
'shared'
),
(
'slave'
,
'shared'
),
(
'status'
,
'state'
)
(
'status'
,
'state'
)
):
):
try
:
if
k_j
in
(
'sla'
,
'parameter'
):
if
k_j
in
(
'sla'
,
'parameter'
):
request_dict
[
k_i
]
=
xml_marshaller
.
xml_marshaller
.
dumps
(
jbody
[
k_j
])
request_dict
[
k_i
]
=
xml_marshaller
.
xml_marshaller
.
dumps
(
self
.
jbody
[
k_j
])
else
:
else
:
request_dict
[
k_i
]
=
jbody
[
k_j
]
request_dict
[
k_i
]
=
self
.
jbody
[
k_j
]
except
KeyError
:
error_dict
[
k_j
]
=
'Missing.'
if
error_dict
:
response
.
setStatus
(
400
)
response
.
setBody
(
json
.
dumps
(
error_dict
))
return
response
try
:
try
:
person
.
requestSoftwareInstance
(
**
request_dict
)
person
.
requestSoftwareInstance
(
**
request_dict
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment