Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
erp5
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
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Cédric Le Ninivin
erp5
Commits
107ffe46
Commit
107ffe46
authored
Apr 11, 2024
by
Titouan Soulard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
erp5_api_style: updates calls to `logError`
parent
a4fc2e76
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
31 deletions
+36
-31
bt5/erp5_api_style/SkinTemplateItem/portal_skins/erp5_api_style/ERP5Site_asjIOStyle.py
...teItem/portal_skins/erp5_api_style/ERP5Site_asjIOStyle.py
+36
-31
No files found.
bt5/erp5_api_style/SkinTemplateItem/portal_skins/erp5_api_style/ERP5Site_asjIOStyle.py
View file @
107ffe46
import
json
from
zExceptions
import
NotFound
# an external script is need to extract content from body: REQUEST.get('BODY')
mode
=
mode
or
context
.
REQUEST
.
form
.
get
(
"mode"
,
"get"
)
text_content
=
text_content
or
context
.
REQUEST
.
form
.
get
(
"text_content"
,
""
)
...
...
@@ -7,20 +10,9 @@ web_section_value = context.getWebSectionValue()
if
web_section_value
:
context
.
REQUEST
.
set
(
"web_section_relative_url"
,
web_section_value
.
getRelativeUrl
())
import
json
portal
=
context
.
getPortalObject
()
response
=
container
.
REQUEST
.
RESPONSE
# XXX Hardcoded, to be removed
def
logError
(
e
,
error_name
=
""
,
error_code
=
400
,
detail_list
=
None
):
return
portal
.
ERP5Site_logApiErrorAndReturn
(
error_code
=
error_code
,
error_message
=
e
,
error_name
=
error_name
,
detail_list
=
detail_list
,
)
def
getCorrespondingActionAndReturn
(
traversed_document
,
action_id
,
text_data
,
list_error
=
False
):
return
traversed_document
.
getTypeInfo
().
getDefaultViewFor
(
traversed_document
,
view
=
action_id
...
...
@@ -56,7 +48,6 @@ def json_loads_byteified(json_text):
ignore_dicts
=
True
)
mode_dict
=
{
"put"
:
"configuration_put_action_type"
,
"get"
:
"configuration_get_action_type"
,
...
...
@@ -64,17 +55,26 @@ mode_dict = {
"allDocs"
:
"configuration_alldocs_action_type"
,
}
# Not expected to be called anytime since allowed modes are already restricted in component
if
mode
not
in
mode_dict
:
r
eturn
"Used Mode is not defined in the mode list %s"
%
mode_dict
.
keys
(
)
r
aise
NotImplementedError
(
"Used Mode is not defined in the mode list %s"
%
mode_dict
.
keys
()
)
# Check JSON Form
if
not
data_dict
:
try
:
data
=
json_loads_byteified
(
text_content
)
except
BaseException
as
e
:
return
logError
(
str
(
e
),
error_name
=
"API-JSON-INVALID-JSON"
)
return
portal
.
ERP5Site_logApiErrorAndReturn
(
error_code
=
400
,
error_message
=
str
(
e
),
error_name
=
"API-JSON-INVALID-JSON"
,
)
if
not
isinstance
(
data
,
dict
):
return
logError
(
"Did not received a JSON Object"
,
error_name
=
"API-JSON-NOT-JSON-OBJECT"
)
return
portal
.
ERP5Site_logApiErrorAndReturn
(
error_code
=
400
,
error_message
=
"Did not received a JSON Object"
,
error_name
=
"API-JSON-NOT-JSON-OBJECT"
,
)
else
:
data
=
data_dict
...
...
@@ -85,25 +85,28 @@ if mode in ("get", "put"):
try
:
document_id
=
data
[
"id"
]
except
KeyError
:
return
logError
(
"Cannot find id property"
,
error_name
=
"API-JSON-NO-ID-PROPERTY"
)
return
portal
.
ERP5Site_logApiErrorAndReturn
(
error_code
=
400
,
error_message
=
"Cannot find id property"
,
error_name
=
"API-JSON-NO-ID-PROPERTY"
,
)
except
TypeError
:
return
logError
(
"Did not received a JSON Object"
,
error_name
=
"API-JSON-NOT-JSON-OBJECT"
)
return
portal
.
ERP5Site_logApiErrorAndReturn
(
error_code
=
400
,
error_message
=
"Did not received a JSON Object"
,
error_name
=
"API-JSON-NOT-JSON-OBJECT"
,
)
try
:
document
=
portal
.
restrictedTraverse
(
str
(
document_id
))
except
KeyError
:
r
eturn
logError
(
"Document has not been found"
,
error_name
=
"API-DOCUMENT-NOT-FOUND"
,
error_code
=
404
)
r
aise
NotFound
(
"Document has not been found"
)
else
:
document
=
context
.
jIOWebSection_getObjectFromData
(
data
)
if
document
is
None
:
return
logError
(
"The data you provided did not allow to find the corresponding document"
,
error_name
=
"API-JSON-DOCUMENT-NOT-FOUND"
,
error_code
=
404
,
)
raise
NotFound
(
"Document has not been found"
)
else
:
document
=
context
result_list
=
[]
error_dict
=
{}
match
=
False
...
...
@@ -121,16 +124,17 @@ for erp5_action_key in erp5_action_dict.keys():
document
,
view_action
[
'id'
],
data
,
list_error
=
True
)
match
=
True
# If an object is created, 201 code is exp
t
ected
# If an object is created, 201 code is expected
if
mode
==
"post"
:
if
response
.
getStatus
()
<
400
:
response
.
setStatus
(
201
)
return
result
except
ValueError
as
e
:
if
mode
in
(
"put"
,
"get"
):
return
logError
(
"data did not match schema: %s"
%
e
,
error_name
=
"INPUT-DATA-DOES-NOT-MATCH-EXPECTED-DATA"
return
portal
.
ERP5Site_logApiErrorAndReturn
(
error_code
=
400
,
error_message
=
"Data did not match schema: %s"
%
str
(
e
),
error_name
=
"API-JSON-SCHEMA-ERROR"
,
)
try
:
error_dict
.
update
(
json
.
loads
(
str
(
e
)))
...
...
@@ -151,6 +155,7 @@ if not match:
error_message
=
"Data did not validate against interface schemas"
if
error_dict
:
error_kw
[
"detail_list"
]
=
sorted
(
error_dict
.
items
())
return
logError
(
error_message
,
**
error_kw
)
raise
ValueError
(
"Unreachable code reached"
)
return
portal
.
ERP5Site_logApiErrorAndReturn
(
error_message
=
error_message
,
**
error_kw
)
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