Commit c8c23c98 authored by Tomáš Peterka's avatar Tomáš Peterka

[hal_json] Add is_updating parameter to Dialog Scripts and keep_items to more UI Scripts

parent 52282904
......@@ -5,10 +5,11 @@ Responsible for validating form data and redirecting to the form action.
Please note that the new UI has deprecated use of Selections. Your scripts
will no longer receive `selection_name` nor `selection` in their arguments.
There are runtime values hidden in every form (injected by getHateoas Script):
There are runtime values hidden in every dialog form (injected by getHateoas Script):
form_id - previous form ID (backward compatibility reasons)
dialog_id - current form dialog ID
dialog_method - method to be called - can be either update_method or dialog_method of the Dialog Form
extra_param_json - JSON serialized extra parameters for the dialog script
"""
from Products.ERP5Type.Log import log, DEBUG, INFO, WARNING, ERROR
......@@ -85,6 +86,7 @@ if dialog_method == 'Base_createRelation':
form = getattr(context, dialog_id)
form_data = None
extra_param = json.loads(extra_param_json or "{}")
is_updating = (dialog_method == update_method)
# form can be a python script that returns the form
if not hasattr(form, 'validate_all_to_request'):
......@@ -174,21 +176,22 @@ if len(listbox_id_list):
# First check for an query in form parameters - if they are there
# that means previous view was a listbox with selected stuff so recover here
query = extra_param.get("query", None)
select_all = int(extra_param.pop("_select_all", "0"))
select_all = extra_param.get("_select_all", 0)
if query != "" or (query == "" and select_all > 0):
if query or (query == "" and select_all > 0):
listbox = getattr(context, form_id).Form_getListbox()
if listbox is not None:
kw['uids'] = [int(getattr(document, "uid"))
for document in context.Base_searchUsingListbox(listbox, query)]
else:
log('Action {} should not specify `uids` as its parameters when it does not take object list from the previous view!'.format(dialog_method), level=ERROR)
elif query == "" and select_all == 0 and dialog_method != update_method: # do not interrupt on UPDATE
elif query == "" and select_all == 0 and not is_updating: # do not interrupt on UPDATE
extra_param["_select_all"] = 1
return context.Base_renderForm(
dialog_id,
message=translate("All documents are selected! Submit again to proceed or Cancel and narrow down your search."),
level=WARNING,
keep_items={'_select_all': 1},
keep_items=extra_param,
query=query,
form_data=form_data)
......@@ -201,15 +204,17 @@ if dialog_category == "object_search" :
# Notify the underlying script whether user did modifications
form_hash = form.hash_validated_data(form_data)
if "form_hash" in extra_param:
kw['has_changed'] = (form_hash != extra_param.pop('form_hash'))
# Add rest of extra param into arguments of the target method
kw.update(extra_param)
kw.update(**extra_param)
kw.update(keep_items=extra_param) # better backward compatibility
# Finally we will call the Dialog Method
# Handle deferred style, unless we are executing the update action
if dialog_method != update_method and kw.get('deferred_style', 0):
if not is_updating and kw.get('deferred_style', 0):
kw['deferred_portal_skin'] = kw.get('portal_skin', None)
# XXX Hardcoded Deferred style name
kw['portal_skin'] = 'Deferred'
......@@ -223,7 +228,8 @@ if dialog_method != update_method and kw.get('deferred_style', 0):
return context.Base_renderForm(dialog_id,
message=translate('Deferred reports are possible only with preference '\
'"Report Style" set to "ODT" or "ODS"'),
level=WARNING)
level=WARNING,
keep_items=extra_param)
# If the action form has report_view as it's method, it
if page_template != 'report_view':
......@@ -238,7 +244,7 @@ if dialog_method != update_method and kw.get('deferred_style', 0):
# if we are not in Deferred mode - then it points to `Base_activateSimpleView`
if True:
if dialog_method != update_method:
if not is_updating:
# When we are not executing the update action, we have to change the skin
# manually,
if 'portal_skin' in kw:
......@@ -267,7 +273,7 @@ if True:
# Only in case we are not updating but executing - then proceed with direct
# execution based on Skin selection
if dialog_method != update_method:
if not is_updating:
# RJS: If we are in deferred mode - call the form directly and return
# dialog method is now `Base_activateSimpleView` - the only script in
# deferred portal_skins folder
......@@ -288,7 +294,11 @@ if True:
meta_type = ""
if meta_type in ("ERP5 Form", "ERP5 Report"):
return context.ERP5Document_getHateoas(REQUEST=request, form=dialog_form, mode="form", form_data=form_data)
return context.Base_renderForm(dialog_id, keep_items=extra_param, form_data=form_data, REQUEST=request)
# do not override "is_updating" form field value
if 'is_updating' not in kw:
kw['is_updating'] = is_updating
return dialog_form(**kw)
......
"""
This script factorises code required to redirect to the appropriate
page from a script. It should probably be extended, reviewed and documented
so that less code is copied and pasted in dialog scripts.
"""UI Script to redirect the user to `context` with optional custom view `form_id`.
TODO: improve API and extensively document. ERP5Site_redirect may
be redundant.
:param keep_items: is used mainly to pass "portal_status_message" to be showed to the user
the new UI supports "portal_status_level" with values "success" or "error"
"""
from ZTUtils import make_query
import json
......@@ -40,8 +37,16 @@ response.setHeader("X-Location", "urn:jio:get:%s" % context.getRelativeUrl())
# therefor we don't need to be afraid of clashes
response.setHeader("Content-type", "application/json; charset=utf-8")
portal_status_level = keep_items.pop("portal_status_level", "success")
if portal_status_level in ("warning", "error", "fatal"):
portal_status_level = "error"
if portal_status_level in ("info", "debug", "success"):
portal_status_level = "success"
result_dict = {
'portal_status_message': "%s" % keep_items.pop("portal_status_message", ""),
'portal_status_level': "%s" % portal_status_level,
'_links': {
"self": {
# XXX Include query parameters
......
......@@ -9,16 +9,22 @@ This script differs from Base_redirect that it keeps the form values in place.
:param REQUEST: request
:param **kwargs: should contain parameters to ERP5Document_getHateoas such as 'query' to replace Selections
"""
keep_items = keep_items or {}
import json
request = REQUEST or context.REQUEST
form = getattr(context, form_id)
if not message and "portal_status_message" in keep_items:
message = keep_items.pop("portal_status_message")
if keep_items is not None:
if not message and "portal_status_message" in keep_items:
message = keep_items.pop("portal_status_message")
if not level and "portal_status_level" in keep_items:
level = keep_items.pop("portal_status_level")
if not level and "portal_status_level" in keep_items:
level = keep_items.pop("portal_status_level")
if not keep_items:
keep_items = REQUEST.get('extra_param_json', None)
if isinstance(keep_items, str):
keep_items = json.loads(keep_items)
return context.ERP5Document_getHateoas(form=form, mode='form', REQUEST=REQUEST, extra_param_json=keep_items,
portal_status_message=message, portal_status_level=level, **kwargs)
......@@ -23,7 +23,7 @@ else:
response_code = 200
response = request.RESPONSE if request is not None else context.REQUEST.RESPONSE
response = REQUEST.RESPONSE if REQUEST is not None else context.REQUEST.RESPONSE
# Set the response code and header info in the response
response.setStatus(response_code)
response.setHeader("Content-type", "application/json; charset=utf-8")
......
......@@ -50,7 +50,7 @@
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>message, level="error", request=None</string> </value>
<value> <string>message, level="error", REQUEST=None</string> </value>
</item>
<item>
<key> <string>id</string> </key>
......
......@@ -1148,7 +1148,7 @@ def renderForm(traversed_document, form, response_dict, key_prefix=None, selecti
if 'uids' in dialog_method_kwargs:
# If we do not have "query" in the REQUEST but the Dialog Method requires uids
# then we still should inject empty "query" in the dialog call
extra_param_json["query"] = query or REQUEST.get("query", "")
extra_param_json["query"] = query or extra_param_json.get("query", "") or REQUEST.get("query", "")
else:
# In form_view we place only form_id in the request form
renderHiddenField(response_dict, 'form_id', form.id)
......
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