Commit 5004e51c authored by Tomáš Peterka's avatar Tomáš Peterka Committed by Tomáš Peterka

[hal_json] Replace Base_returnFailureWithMessage with more generic Base_renderMessage

/reviewed-on nexedi/erp5!598
parent 2f1f2953
......@@ -15,6 +15,7 @@ def isFieldType(field, type_name):
from Products.Formulator.Errors import FormValidationError, ValidationError
from ZTUtils import make_query
# Kato: I do not understand why we throw away REQUEST from parameters (hidden in **kw)
# and use container.REQUEST just to introduce yet another global state. Maybe because
# container.REQUEST is used in other places.
......@@ -98,9 +99,10 @@ try:
# Form is OK, it's just this field - style so we return back form-wide error
# for which we don't have support out-of-the-box thus we manually craft it
# XXX TODO: Form-wide validation errors
return context.Base_returnFailureWithMessage(
return Base_renderMessage(
translate('Only ODT, ODS, Hal and HalRestricted skins are allowed for reports '\
'in Preferences - User Interface - Report Style'))
'in Preferences - User Interface - Report Style'),
level=WARNING)
except FormValidationError as validation_errors:
# Pack errors into the request
......@@ -198,9 +200,10 @@ if dialog_method != update_method and clean_kw.get('deferred_style', 0):
# Limit Reports in Deferred style to known working styles
if request_form.get('your_portal_skin', None) not in ("ODT", "ODS"):
# RJS own validation - deferred option works here only with ODS/ODT skins
return Base_returnFailureWithMessage(
return Base_renderMessage(
translate('Deferred reports are possible only with preference '\
'"Report Style" set to "ODT" or "ODS"'))
'"Report Style" set to "ODT" or "ODS"'),
level=WARNING)
# If the action form has report_view as it's method, it
if page_template != 'report_view':
......
"""
Script to return json for response header while displying failure
Return JSON with message to be displayed and set according HTTP STATUS for message severity.
:param message: {str}
:param level: {str | int} use ERP5Type.Log levels or simply strings like "info", "warning", or "error"
"""
import json
from Products.ERP5Type.Log import WARNING, ERROR
if isinstance(level, (str, unicode)):
if level.lower() == "error":
response_code = 500
elif level.lower().startswith("warn"):
response_code = 403
else:
response_code = 200
else:
if level == ERROR:
response_code = 500
elif level == WARNING:
response_code = 403
else:
response_code = 200
response = request.RESPONSE if request is not None else context.REQUEST.RESPONSE
# Set the response code and header info in the response
response.setStatus(int(response_code))
response.setStatus(response_code)
response.setHeader("Content-type", "application/json; charset=utf-8")
return json.dumps({"portal_status_message": str(message)})
......@@ -50,11 +50,11 @@
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>message, response_code=500, request=None</string> </value>
<value> <string>message, level="error", request=None</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_returnFailureWithMessage</string> </value>
<value> <string>Base_renderMessage</string> </value>
</item>
</dictionary>
</pickle>
......
......@@ -2,7 +2,8 @@ from Products.CMFCore.WorkflowCore import WorkflowException
from Products.Formulator.Errors import FormValidationError
from Products.DCWorkflow.DCWorkflow import ValidationFailed
from Products.ERP5Type.Message import translateString
from Products.ERP5Type.Log import log
from Products.ERP5Type.Log import log, WARNING
portal = context.getPortalObject()
request = kw.get("REQUEST", None) or context.REQUEST
......@@ -69,7 +70,6 @@ try:
doaction_param_list['workflow_action'],
**doaction_param_list)
except ValidationFailed, error_message:
response_code = 403
if getattr(error_message, 'msg', None):
# use of Message class to store message+mapping+domain
message = error_message.msg
......@@ -79,12 +79,11 @@ except ValidationFailed, error_message:
message = str(message)
else:
message = str(error_message)
return context.Base_returnFailureWithMessage(message, response_code, request)
return context.Base_renderMessage(message, WARNING, request)
except WorkflowException as error_message:
response_code = 403
if str(error_message) == "No workflow provides the '${action_id}' action.":
message = translateString("Workflow state may have been updated by other user. Please try again.")
return context.Base_returnFailureWithMessage(message, response_code, request)
return context.Base_renderMessage(message, WARNING, request)
else:
raise
else:
......
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