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
Labels
Merge Requests
137
Merge Requests
137
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
Jobs
Commits
Open sidebar
nexedi
erp5
Commits
6a893b8d
Commit
6a893b8d
authored
Aug 29, 2017
by
Tomáš Peterka
Committed by
Tomáš Peterka
Aug 29, 2017
2
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[json_style] Add FormBoxField to getHateoas and refactor json_style/Base_edit
parent
a5a2f1cd
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
219 additions
and
270 deletions
+219
-270
bt5/erp5_hal_json_style/SkinTemplateItem/portal_skins/erp5_hal_json_style/Base_edit.py
...emplateItem/portal_skins/erp5_hal_json_style/Base_edit.py
+59
-72
bt5/erp5_hal_json_style/SkinTemplateItem/portal_skins/erp5_hal_json_style/ERP5Document_getHateoas.py
...rtal_skins/erp5_hal_json_style/ERP5Document_getHateoas.py
+160
-198
No files found.
bt5/erp5_hal_json_style/SkinTemplateItem/portal_skins/erp5_hal_json_style/Base_edit.py
View file @
6a893b8d
"""
This script validates a form to the current REQUEST,
processes the REQUEST to extract form data and editors,
then updates the current context with the form data
by calling edit on it or by invoking editors.
"""Handle form - REQUEST interaction.
- Validate a form to the current REQUEST
- Extract form data and editors from REQUEST,
- Update current context with form data by calling edit or invoking editors
:param silent: int (0|1) means that the edit action is not invoked by a form
submit but rather by an internal code thus the return value
contains as much usefull info as possible
TODO: split the generic form validation logic
from the context update logic
...
...
@@ -36,7 +40,7 @@ if not silent_mode and not request.AUTHENTICATED_USER.has_permission('Modify por
try
:
# Validate
form
.
validate_all_to_request
(
request
,
key_prefix
=
key_prefix
)
except
FormValidationError
,
validation_errors
:
except
FormValidationError
as
validation_errors
:
# Pack errors into the request
field_errors
=
form
.
ErrorFields
(
validation_errors
)
request
.
set
(
'field_errors'
,
field_errors
)
...
...
@@ -51,37 +55,39 @@ except FormValidationError, validation_errors:
request
.
RESPONSE
.
setStatus
(
400
)
return
context
.
ERP5Document_getHateoas
(
form
=
form
,
REQUEST
=
request
,
mode
=
'form'
)
def
editListBox
(
listbox_field
,
listbox
):
""" Function called to edit a listbox
"""
if
listbox
is
not
None
:
gv
=
{}
if
listbox_field
.
has_value
(
'global_attributes'
):
hidden_attributes
=
map
(
lambda
x
:
x
[
0
],
listbox_field
.
get_value
(
'global_attributes'
))
for
k
in
hidden_attributes
:
gv
[
k
]
=
getattr
(
request
,
k
,
None
)
for
url
,
v
in
listbox
.
items
():
v
.
update
(
gv
)
# Form: '' -> ERP5: None
encapsulated_editor_list
=
[]
cleaned_v
=
{}
for
key
,
value
in
v
.
items
():
if
hasattr
(
value
,
'edit'
):
encapsulated_editor_list
.
append
(
value
)
else
:
if
value
==
''
:
value
=
None
cleaned_v
[
key
]
=
value
if
cleaned_v
:
if
listbox_edit
is
None
:
obj
=
context
.
restrictedTraverse
(
url
)
obj
.
edit
(
edit_order
=
edit_order
,
**
cleaned_v
)
else
:
listbox_edit
(
url
,
edit_order
,
cleaned_v
)
"""Go through every item in the listbox and call its `edit` with modified values."""
if
listbox
is
None
:
return
# extract hidden (global) attributes from request to be used in listbox's update
global_attr
=
{
hidden_key
:
getattr
(
request
,
hidden_key
,
None
)
for
hidden_key
,
_
in
listbox_field
.
get_value
(
'global_attributes'
)}
\
if
listbox_field
.
has_value
(
'global_attributes'
)
\
else
{}
for
item_url
,
item_value
in
listbox
.
items
():
item_value
.
update
(
global_attr
)
# Form: '' -> ERP5: None
editor_list
=
[]
value_dict
=
{}
for
key
,
value
in
item_value
.
items
():
# for every value decide whether it is an attribute or an editor
if
hasattr
(
value
,
'edit'
):
editor_list
.
append
(
value
)
else
:
value_dict
[
key
]
=
value
if
value
!=
''
else
None
if
value_dict
:
if
listbox_edit
is
None
:
obj
=
context
.
restrictedTraverse
(
item_url
)
obj
.
edit
(
edit_order
=
edit_order
,
**
value_dict
)
for
editor
in
editor_list
:
editor
.
edit
(
obj
)
else
:
listbox_edit
(
item_url
,
edit_order
,
value_dict
)
for
encapsulated_editor
in
encapsulated_editor_list
:
encapsulated_editor
.
edit
(
obj
)
def
editMatrixBox
(
matrixbox_field
,
matrixbox
):
""" Function called to edit a Matrix box
...
...
@@ -184,43 +190,24 @@ def editMatrixBox(matrixbox_field, matrixbox):
else
:
return
"Cell %s does not exist"
%
str
(
k
)
field_prefix_len
=
len
(
field_prefix
)
def
parseField
(
f
):
"""
Parse given form field, to put them in
kw or in encapsulated_editor_list
"""
k
=
f
.
id
if
f
.
has_value
(
'alternate_name'
):
k
=
f
.
get_value
(
'alternate_name'
)
or
f
.
id
v
=
getattr
(
request
,
k
,
MARKER
)
if
hasattr
(
v
,
'edit'
):
# This is an encapsulated editor
# call it
encapsulated_editor_list
.
append
(
v
)
elif
v
is
not
MARKER
:
if
k
.
startswith
(
field_prefix
):
# We only take into account
# the object attributes
k
=
k
[
field_prefix_len
:]
# Form: '' -> ERP5: None
if
v
==
''
:
v
=
None
kw
[
k
]
=
v
# Some initilizations
kw
=
{}
encapsulated_editor_list
=
[]
MARKER
=
[]
edit_kwargs
=
{}
# keyword arguments for `edit` function on context
encapsulated_editor_list
=
[]
# editors placed inside REQUEST object
MARKER
=
[]
# placeholder for an empty value
message
=
Base_translateString
(
"Data updated."
)
try
:
# We process all the field in form and
# we check if they are in the request,
# then we edit them
# extract all listbox's object form fields from the request and `edit` the object
for
field
in
form
.
get_fields
():
parseField
(
field
)
# Dispatch field either to `edit_kwargs` (in case of simple fields) or to `encapsulated_editor_list` in case of editors
field_name
=
field
.
id
if
not
field
.
has_value
(
'alternate_name'
)
else
(
field
.
get_value
(
'alternate_name'
)
or
field
.
id
)
field_value
=
getattr
(
request
,
field_name
,
MARKER
)
if
hasattr
(
field_value
,
'edit'
):
# field is an encapsulated editor; call it later
encapsulated_editor_list
.
append
(
field_value
)
elif
field_value
is
not
MARKER
and
field_name
.
startswith
(
field_prefix
):
# object own attribute (fix value Form: '' -> ERP5: None)
edit_kwargs
[
field_name
[
len
(
field_prefix
):]]
=
field_value
if
field_value
!=
''
else
None
## XXX We need to find a way not to use meta_type.
field_meta_type
=
field
.
meta_type
...
...
@@ -229,19 +216,19 @@ try:
if
(
field_meta_type
==
'ListBox'
):
editListBox
(
field
,
request
.
get
(
field
.
id
))
el
if
(
field_meta_type
==
'MatrixBox'
):
if
(
field_meta_type
==
'MatrixBox'
):
editMatrixBox
(
field
,
request
.
get
(
field
.
id
))
# Return parsed values
if
silent_mode
:
return
(
kw
,
encapsulated_editor_list
),
'edit'
if
silent_mode
:
return
(
edit_kwargs
,
encapsulated_editor_list
),
'edit'
# Maybe we should build a list of objects we need
# Update basic attributes
context
.
edit
(
REQUEST
=
request
,
edit_order
=
edit_order
,
**
kw
)
context
.
edit
(
REQUEST
=
request
,
edit_order
=
edit_order
,
**
edit_kwargs
)
for
encapsulated_editor
in
encapsulated_editor_list
:
encapsulated_editor
.
edit
(
context
)
except
ActivityPendingError
,
e
:
message
=
Base_translateString
(
"%s"
%
e
)
except
ActivityPendingError
as
e
:
message
=
Base_translateString
(
str
(
e
)
)
if
message_only
:
return
message
...
...
bt5/erp5_hal_json_style/SkinTemplateItem/portal_skins/erp5_hal_json_style/ERP5Document_getHateoas.py
View file @
6a893b8d
This diff is collapsed.
Click to expand it.
Boxiang Sun
@Daetalus
mentioned in merge request
!364 (merged)
·
Aug 31, 2017
mentioned in merge request
!364 (merged)
mentioned in merge request !364
Toggle commit list
Boxiang Sun
@Daetalus
mentioned in commit
e1c1fda0
·
Aug 31, 2017
mentioned in commit
e1c1fda0
mentioned in commit e1c1fda06483546a78a56819b54a655fa7055540
Toggle commit list
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