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