Commit 94df7891 authored by Tomáš Peterka's avatar Tomáš Peterka Committed by Tomáš Peterka

[hal_json] Support has_changed parameter to Dialog Methods

/reviewed-on !652
parent 8e3cbb95
......@@ -16,6 +16,7 @@ return context.ERP5Document_getHateoas(
select_list=select_list,
limit=limit,
form=form,
form_data=form_data,
relative_url=relative_url,
list_method=list_method,
default_param_json=default_param_json,
......
......@@ -50,7 +50,7 @@
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>REQUEST=None, response=None, view=None, mode=\'root\', query=None, select_list=None, limit=10, local_roles=None, form=None, relative_url=None, list_method=None, default_param_json=None, form_relative_url=None, bulk_list="[]", sort_on=None, selection_domain=None, extra_param_json=None, portal_status_message=\'\', portal_status_level=None</string> </value>
<value> <string>REQUEST=None, response=None, view=None, mode=\'root\', query=None, select_list=None, limit=10, local_roles=None, form=None, form_data=None, relative_url=None, list_method=None, default_param_json=None, form_relative_url=None, bulk_list="[]", sort_on=None, selection_domain=None, extra_param_json=None, portal_status_message=\'\', portal_status_level=None</string> </value>
</item>
<item>
<key> <string>id</string> </key>
......
......@@ -83,6 +83,7 @@ if dialog_method == 'Folder_delete':
md5_object_uid_list=kw['md5_object_uid_list'])
form = getattr(context, dialog_id)
form_data = None
extra_param = json.loads(extra_param_json or "{}")
# form can be a python script that returns the form
......@@ -95,7 +96,7 @@ try:
# data. Otherwise, field appears as non editable.
editable_mode = request.get('editable_mode', 1)
request.set('editable_mode', 1)
form.validate_all_to_request(request)
form_data = form.validate_all_to_request(request)
request.set('editable_mode', editable_mode)
default_skin = portal.portal_skins.getDefaultSkin()
allowed_styles = ("ODT", "ODS", "Hal", "HalRestricted")
......@@ -108,7 +109,8 @@ try:
return context.Base_renderForm(dialog_id,
message=translate('Only ODT, ODS, Hal and HalRestricted skins are allowed for reports '\
'in Preferences - User Interface - Report Style'),
level=WARNING)
level=WARNING,
form_data=form_data)
except FormValidationError as validation_errors:
# Pack errors into the request
......@@ -186,7 +188,8 @@ elif query == "" and select_all == 0 and dialog_method != update_method: # do n
message=translate("All documents are selected! Submit again to proceed or Cancel and narrow down your search."),
level=WARNING,
keep_items={'_select_all': 1},
query=query)
query=query,
form_data=form_data)
# The old way was to set inquire kw for "list_selection_name" and update
# it with kw["uids"] which means a long URL to call this script
......@@ -195,6 +198,11 @@ elif query == "" and select_all == 0 and dialog_method != update_method: # do n
if dialog_category == "object_search" :
portal.portal_selections.setSelectionParamsFor(kw['selection_name'], kw)
# 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)
......@@ -279,7 +287,7 @@ if True:
meta_type = ""
if meta_type in ("ERP5 Form", "ERP5 Report"):
return context.ERP5Document_getHateoas(REQUEST=request, form=dialog_form, mode="form")
return context.ERP5Document_getHateoas(REQUEST=request, form=dialog_form, mode="form", form_data=form_data)
return dialog_form(**kw)
......
......@@ -26,6 +26,10 @@ Parameters for mode == 'search'
Parameters for mode == 'form'
:param form: {instace} of a form - obviously this call can be only internal (Script-to-Script)
:param form_data: {dict} cleaned (validated) form data stored in dict where the key is (prefixed) field.id. We do not use it to
obtain the value of the field because of how the validation itself work. Take a look in
Formulator/Form.validata_all_to_request where REQUEST is modified inplace and in case of first error
an exception is thrown which prevents the return thus form_data are empty in case of partial success.
:param extra_param_json: {dict} extra fields to be added to the rendered form
Parameters for mode == 'traverse'
......@@ -1239,6 +1243,11 @@ def renderForm(traversed_document, form, response_dict, key_prefix=None, selecti
# end-if report_section
if form.pt == "form_dialog":
# Insert hash of current values into the form so scripts can see whether data has
# changed if they provide multi-step process
if form_data is not None:
extra_param_json["form_hash"] = form.hash_validated_data(form_data)
# extra_param_json is a special field in forms (just like form_id). extra_param_json field holds JSON
# metadata about the form (its hash and dynamic fields)
renderHiddenField(response_dict, 'extra_param_json', json.dumps(extra_param_json))
......
......@@ -56,7 +56,7 @@
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>REQUEST=None, response=None, view=None, mode=\'root\', query=None, select_list=None, limit=10, local_roles=None, form=None, relative_url=None, restricted=0, list_method=None, default_param_json=None, form_relative_url=None, bulk_list="[]", sort_on=None, selection_domain=None, extra_param_json=None, portal_status_message=\'\', portal_status_level=None</string> </value>
<value> <string>REQUEST=None, response=None, view=None, mode=\'root\', query=None, select_list=None, limit=10, local_roles=None, form=None, form_data=None, relative_url=None, restricted=0, list_method=None, default_param_json=None, form_relative_url=None, bulk_list="[]", sort_on=None, selection_domain=None, extra_param_json=None, portal_status_message=\'\', portal_status_level=None</string> </value>
</item>
<item>
<key> <string>id</string> </key>
......
......@@ -2,4 +2,12 @@ from Products.ERP5Type.Log import log
log("Folder method received dialog_id, form_id, uids and {!s}".format(kwargs.keys()))
return context.Base_redirect(form_id, keep_items={"portal_status_message": "Did nothing."})
if 'has_changed' not in kwargs or kwargs['has_changed'] is None:
message = "Did nothing."
else:
if kwargs['has_changed']:
message = "Data has changed."
else:
message = "Data the same."
return context.Base_redirect(form_id, keep_items={"portal_status_message": message})
......@@ -73,7 +73,9 @@
<item>
<key> <string>left</string> </key>
<value>
<list/>
<list>
<string>custom_variable</string>
</list>
</value>
</item>
<item>
......
......@@ -68,6 +68,11 @@
<td>//div[contains(@data-gadget-url, 'gadget_erp5_field_listbox.html')]//table/tbody/tr[3]/td[1]//p</td>
<td>22</td></tr>
<tr><td>type</td>
<td>//input[@name="field_custom_variable"]</td>
<td>couscous</td></tr>
<tr><td>waitForElementPresent</td>
<td>//div[contains(@data-gadget-url, 'gadget_erp5_field_listbox.html')]//nav/a[@data-i18n="Next"]</td><td></td></tr>
<tr><td>click</td>
......@@ -86,6 +91,7 @@
<td>//div[contains(@data-gadget-url, 'gadget_erp5_field_listbox.html')]//table/tbody/tr[3]/td[1]//p</td>
<td>14</td></tr>
<tr><th rowspan="1" colspan="3">Updating the dialog must not trigger warning about all selected</th></tr>
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/update_dialog" />
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_content_loaded" />
......@@ -100,10 +106,51 @@
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_notification" />
</tal:block>
<tr><td>type</td>
<td>//input[@name="field_custom_variable"]</td>
<td>couscous</td></tr>
<tr><th rowspan="1" colspan="3">Second submission must work as advertised</th></tr>
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/submit_dialog" />
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_content_loaded" />
<tal:block tal:define="notification_configuration python: {'class': 'success', 'text': 'Did nothing.'}">
<tal:block tal:define="notification_configuration python: {'class': 'success', 'text': 'Data the same.'}">
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_notification" />
</tal:block>
<tr><th rowspan="1" colspan="3">Let's try has_changed to kick in on simple data change</th></tr>
<tr><td>waitForElementPresent</td>
<td>//div[@data-role="header"]//a[@data-i18n="Actions"]</td><td></td></tr>
<tr><td>click</td>
<td>//div[@data-role="header"]//a[@data-i18n="Actions"]</td><td></td></tr>
<tr><td>waitForElementPresent</td>
<td>//ul[@data-role="listview"]//a[@data-i18n="Empty Mass Action"]</td><td></td></tr>
<tr><td>click</td>
<td>//ul[@data-role="listview"]//a[@data-i18n="Empty Mass Action"]</td><td></td></tr>
<tr><td>waitForElementPresent</td>
<td>//input[@name="field_custom_variable"]</td><td></td></tr>
<tr><td>type</td>
<td>//input[@name="field_custom_variable"]</td>
<td>tajine</td></tr>
<tr><th rowspan="1" colspan="3">Warn the user and compute form_hash in the background</th></tr>
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/submit_dialog" />
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_content_loaded" />
<tal:block tal:define="notification_configuration python: {'class': 'error', 'text': 'All documents are selected! Submit again to proceed or Cancel and narrow down your search.'}">
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_notification" />
</tal:block>
<tr><td>waitForElementPresent</td>
<td>//input[@name="field_custom_variable"]</td><td></td></tr>
<tr><td>type</td>
<td>//input[@name="field_custom_variable"]</td>
<td>couscous</td></tr>
<tr><th rowspan="1" colspan="3">Submit BUT pass has_changed true to the dialog method</th></tr>
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/submit_dialog" />
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_content_loaded" />
<tal:block tal:define="notification_configuration python: {'class': 'success', 'text': 'Data has changed.'}">
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_notification" />
</tal:block>
......
......@@ -26,6 +26,7 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import hashlib
from copy import deepcopy
......@@ -766,6 +767,15 @@ class ERP5Form(Base, ZMIForm, ZopePageTemplate):
raise FormValidationError(errors, result)
return result
security.declareProtected('View', 'hash_validated_data')
def hash_validated_data(self, validated_data):
return hashlib.sha256(
"".join(
str(validated_data[key])
for key in sorted(validated_data.keys())
if isinstance(validated_data[key], (str, unicode, int, long, float, DateTime)))
).hexdigest()
# FTP/DAV Access
manage_FTPget = ZMIForm.get_xml
......
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