Commit 30ca53e8 authored by Romain Courteaud's avatar Romain Courteaud

[erp5_hal_json_style] Do not generate link for empty value, as it will not be clickable in UI

parent cc46d6d4
...@@ -1814,72 +1814,74 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None, ...@@ -1814,72 +1814,74 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
key='field_%s_%s' % (editable_field.id, brain_uid)) key='field_%s_%s' % (editable_field.id, brain_uid))
# By default, we won't be generating views in the URL # Do not generate link for empty value, as it will not be clickable in UI
url_parameter_dict = None if default_field_value not in ('', None):
if select in url_column_dict: # By default, we won't be generating views in the URL
# Check if we get URL parameters using listbox field `url_columns` url_parameter_dict = None
try: if select in url_column_dict:
url_column_method = getattr(brain, url_column_dict[select]) # Check if we get URL parameters using listbox field `url_columns`
# Result of `url_column_method` must be a dictionary in the format try:
# {'command': <command_name, ex: 'raw', 'push_history'>, url_column_method = getattr(brain, url_column_dict[select])
# 'options': {'url': <Absolute URL>, 'jio_key': <Relative URL of object>, 'view': <id of the view>}} # Result of `url_column_method` must be a dictionary in the format
url_parameter_dict = url_column_method(url_dict=True, # {'command': <command_name, ex: 'raw', 'push_history'>,
brain=brain, # 'options': {'url': <Absolute URL>, 'jio_key': <Relative URL of object>, 'view': <id of the view>}}
selection=catalog_kw['selection'], url_parameter_dict = url_column_method(url_dict=True,
selection_name=catalog_kw['selection_name'], brain=brain,
column_id=select) selection=catalog_kw['selection'],
except AttributeError as e: selection_name=catalog_kw['selection_name'],
# In case the URL method is invalid or empty, we expect to have no link column_id=select)
# for the column to maintain compatibility with old UI, hence we create except AttributeError as e:
# an empty url_parameter_dict for these cases. # In case the URL method is invalid or empty, we expect to have no link
url_parameter_dict = {} # for the column to maintain compatibility with old UI, hence we create
if url_column_dict[select]: # an empty url_parameter_dict for these cases.
log("Invalid URL method {!s} on column {}".format(url_column_dict[select], select), level=800) url_parameter_dict = {}
if url_column_dict[select]:
elif getattr(brain, 'getListItemUrlDict', None) is not None: log("Invalid URL method {!s} on column {}".format(url_column_dict[select], select), level=800)
# Check if we can get URL result from the brain
try: elif getattr(brain, 'getListItemUrlDict', None) is not None:
url_parameter_dict = brain.getListItemUrlDict( # Check if we can get URL result from the brain
select, result_index, catalog_kw['selection_name'] try:
) url_parameter_dict = brain.getListItemUrlDict(
except (ConflictError, RuntimeError): select, result_index, catalog_kw['selection_name']
raise )
except: except (ConflictError, RuntimeError):
log('could not evaluate the url method getListItemUrlDict with %r' % brain, raise
level=800) except:
log('could not evaluate the url method getListItemUrlDict with %r' % brain,
if isinstance(url_parameter_dict, dict): level=800)
# We need to put URL into rendered field so just ensure it is a dict
if not isinstance(contents_item[select], dict): if isinstance(url_parameter_dict, dict):
contents_item[select] = { # We need to put URL into rendered field so just ensure it is a dict
'default': contents_item[select], if not isinstance(contents_item[select], dict):
} contents_item[select] = {
# We should be generating view if there is extra params for view in 'default': contents_item[select],
# view_kw. These parameters are required to create url at hateoas side
# using the URL template as necessary
if 'view_kw' not in url_parameter_dict:
contents_item[select]['url_value'] = url_parameter_dict
else:
# Get extra parameters either from url_result_dict or from brain
extra_url_param_dict = url_parameter_dict['view_kw'].get('extra_param_json', {})
url_template_id = 'traverse_generator'
if extra_url_param_dict:
url_template_id = 'traverse_generator_action'
# Explicity populate url_value dict. This way we can ensure that whatever been
# sent via url_parameter_dict goes directly in url_value dict
contents_item[select]['url_value'] = {}
contents_item[select]['url_value']['command'] = url_parameter_dict['command']
contents_item[select]['url_value']['options'] = url_parameter_dict['options']
# Generate `view` to be used to construct URL
contents_item[select]['url_value']['options']['view'] = url_template_dict[url_template_id] % {
"root_url": site_root.absolute_url(),
"script_id": script.id,
"relative_url": url_parameter_dict['view_kw']['jio_key'].replace("/", "%2F"),
"view": url_parameter_dict['view_kw']['view'],
"extra_param_json": urlsafe_b64encode(
json.dumps(ensureSerializable(extra_url_param_dict)))
} }
# We should be generating view if there is extra params for view in
# view_kw. These parameters are required to create url at hateoas side
# using the URL template as necessary
if 'view_kw' not in url_parameter_dict:
contents_item[select]['url_value'] = url_parameter_dict
else:
# Get extra parameters either from url_result_dict or from brain
extra_url_param_dict = url_parameter_dict['view_kw'].get('extra_param_json', {})
url_template_id = 'traverse_generator'
if extra_url_param_dict:
url_template_id = 'traverse_generator_action'
# Explicity populate url_value dict. This way we can ensure that whatever been
# sent via url_parameter_dict goes directly in url_value dict
contents_item[select]['url_value'] = {}
contents_item[select]['url_value']['command'] = url_parameter_dict['command']
contents_item[select]['url_value']['options'] = url_parameter_dict['options']
# Generate `view` to be used to construct URL
contents_item[select]['url_value']['options']['view'] = url_template_dict[url_template_id] % {
"root_url": site_root.absolute_url(),
"script_id": script.id,
"relative_url": url_parameter_dict['view_kw']['jio_key'].replace("/", "%2F"),
"view": url_parameter_dict['view_kw']['view'],
"extra_param_json": urlsafe_b64encode(
json.dumps(ensureSerializable(extra_url_param_dict)))
}
# endfor select # endfor select
contents_list.append(contents_item) contents_list.append(contents_item)
......
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