Commit 09c476f8 authored by Ayush Tiwari's avatar Ayush Tiwari

[erp5_hal_json_style]: Add url_paramter_dict to contruct URLs in listbox fields

parent bbedf149
...@@ -1652,6 +1652,7 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None, ...@@ -1652,6 +1652,7 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
# extract form field definition into `editable_field_dict` # extract form field definition into `editable_field_dict`
editable_field_dict = {} editable_field_dict = {}
url_parameter_dict = {}
listbox_form = None listbox_form = None
listbox_field_id = None listbox_field_id = None
source_field_meta_type = source_field.meta_type if source_field is not None else "" source_field_meta_type = source_field.meta_type if source_field is not None else ""
...@@ -1662,6 +1663,16 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None, ...@@ -1662,6 +1663,16 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
listbox_field_id = source_field.id listbox_field_id = source_field.id
listbox_form = getattr(traversed_document, source_field.aq_parent.id) listbox_form = getattr(traversed_document, source_field.aq_parent.id)
url_parameter_dict = dict(source_field.get_value('url_parameter_dict'))
# support only selection_name for stat methods&url columns because any `selection` is deprecated
# and should be removed. Selection_name can be passed in catalog_kw by e.g. reports so it has precedence.
# Romain wants full backward compatibility so putting `selection` back in parameters
selection_name = catalog_kw.get('selection_name', source_field.get_value('selection_name'))
if selection_name and 'selection_name' not in catalog_kw:
catalog_kw['selection_name'] = selection_name
if 'selection' not in catalog_kw:
catalog_kw['selection'] = context.getPortalObject().portal_selections.getSelectionFor(selection_name, REQUEST)
# field TALES expression evaluated by Base_getRelatedObjectParameter requires that # field TALES expression evaluated by Base_getRelatedObjectParameter requires that
REQUEST.other['form_id'] = listbox_form.id REQUEST.other['form_id'] = listbox_form.id
...@@ -1769,6 +1780,35 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None, ...@@ -1769,6 +1780,35 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
# given search_result. This name can unfortunately mean almost anything from # given search_result. This name can unfortunately mean almost anything from
# a key name to Python Script with variable number of input parameters. # a key name to Python Script with variable number of input parameters.
contents_item[select] = getAttrFromAnything(search_result, select, property_getter, {'brain': search_result}) contents_item[select] = getAttrFromAnything(search_result, select, property_getter, {'brain': search_result})
if url_parameter_dict:
if select in url_parameter_dict:
contents_item[select] = {
'column_value': contents_item[select],
'url_column': True
}
generate_view = False
for key in url_parameter_dict[select]:
value = getattr(search_result, url_parameter_dict[select][key], None)
if key == 'view':
generate_view = True
elif callable(value):
try:
contents_item[select][key] = value(selection=catalog_kw['selection'], selection_name=catalog_kw['selection_name'], column_id=select, index=result_index)
except (TypeError, ValueError):
contents_item[select][key] = value()
else:
contents_item[select][key] = url_parameter_dict[select][key]
if 'jio_key' not in contents_item[select] and url_parameter_dict[select] and 'absolute_url' not in contents_item[select]:
contents_item[select]['jio_key'] = traversed_document.getRelativeUrl()
if generate_view:
contents_item[select]['view'] = url_template_dict["traverse_generator"] % {
"root_url": site_root.absolute_url(),
"script_id": script.id,
"relative_url": contents_item[select]['jio_key'].replace("/", "%2F"),
"view": url_parameter_dict[select]['view']
}
# endfor select # endfor select
REQUEST.other.pop('cell', None) REQUEST.other.pop('cell', None)
contents_list.append(contents_item) contents_list.append(contents_item)
...@@ -1801,6 +1841,8 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None, ...@@ -1801,6 +1841,8 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
if count_method != "" and count_method.getMethodName() != list_method: if count_method != "" and count_method.getMethodName() != list_method:
count_kw = dict(catalog_kw) count_kw = dict(catalog_kw)
# Drop not needed parameters # Drop not needed parameters
count_kw.pop('selection', None)
count_kw.pop('selection_name', None)
count_kw.pop("sort_on", None) count_kw.pop("sort_on", None)
count_kw.pop("limit", None) count_kw.pop("limit", None)
count_method_result = getattr(traversed_document, count_method.getMethodName())(REQUEST=REQUEST, **count_kw) count_method_result = getattr(traversed_document, count_method.getMethodName())(REQUEST=REQUEST, **count_kw)
...@@ -1812,14 +1854,6 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None, ...@@ -1812,14 +1854,6 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
# XXX: we should check whether they asked for it # XXX: we should check whether they asked for it
stat_method = source_field.get_value('stat_method') stat_method = source_field.get_value('stat_method')
stat_columns = source_field.get_value('stat_columns') stat_columns = source_field.get_value('stat_columns')
# support only selection_name for stat methods because any `selection` is deprecated
# and should be removed. Selection_name can be passed in catalog_kw by e.g. reports so it has precedence.
# Romain wants full backward compatibility so putting `selection` back in parameters
selection_name = catalog_kw.get('selection_name', source_field.get_value('selection_name'))
if selection_name and 'selection_name' not in catalog_kw:
catalog_kw['selection_name'] = selection_name
if 'selection' not in catalog_kw:
catalog_kw['selection'] = context.getPortalObject().portal_selections.getSelectionFor(selection_name, REQUEST)
contents_stat = {} contents_stat = {}
if len(stat_columns) > 0: if len(stat_columns) > 0:
......
...@@ -77,12 +77,20 @@ ...@@ -77,12 +77,20 @@
{{#if editable}} {{#if editable}}
<div class="editable_div" data-column="{{column}}" data-line="{{line}}"></div> <div class="editable_div" data-column="{{column}}" data-line="{{line}}"></div>
{{else}} {{else}}
{{#if href}}
<a href="{{href}}" class="ui-link"> <a href="{{href}}" class="ui-link">
<div class="editable_div" data-column="{{column}}" data-line="{{line}}"></div> <div class="editable_div" data-column="{{column}}" data-line="{{line}}"></div>
</a> </a>
{{else}}
<div class="editable_div" data-column="{{column}}" data-line="{{line}}"></div>
{{/if}}
{{/if}} {{/if}}
{{else}} {{else}}
{{#if href}}
<a href="{{href}}" class="ui-link">{{default}}</a> <a href="{{href}}" class="ui-link">{{default}}</a>
{{else}}
<p>{{default}}</p>
{{/if}}
{{/if}} {{/if}}
</td> </td>
{{/each}} {{/each}}
...@@ -117,12 +125,20 @@ ...@@ -117,12 +125,20 @@
{{#if editable}} {{#if editable}}
<div class="editable_div" data-column="{{column}}" data-line="{{line}}"></div> <div class="editable_div" data-column="{{column}}" data-line="{{line}}"></div>
{{else}} {{else}}
{{#if href}}
<a href="{{href}}" class="ui-link"> <a href="{{href}}" class="ui-link">
<div class="editable_div" data-column="{{column}}" data-line="{{line}}"></div> <div class="editable_div" data-column="{{column}}" data-line="{{line}}"></div>
</a> </a>
{{else}}
<div class="editable_div" data-column="{{column}}" data-line="{{line}}"></div>
{{/if}}
{{/if}} {{/if}}
{{else}} {{else}}
{{#if href}}
<a href="{{href}}" class="ui-link">{{default}}</a> <a href="{{href}}" class="ui-link">{{default}}</a>
{{else}}
<p>{{default}}</p>
{{/if}}
{{/if}} {{/if}}
</td> </td>
{{/each}} {{/each}}
......
...@@ -234,7 +234,7 @@ ...@@ -234,7 +234,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>965.63630.22597.59392</string> </value> <value> <string>965.9107.618.7594</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -252,8 +252,8 @@ ...@@ -252,8 +252,8 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1520243676.37</float> <float>1518446005.97</float>
<string>UTC</string> <string>GMT+1</string>
</tuple> </tuple>
</state> </state>
</object> </object>
......
...@@ -468,9 +468,11 @@ ...@@ -468,9 +468,11 @@
.push(function () { .push(function () {
var lines = gadget.state.lines, var lines = gadget.state.lines,
promise_list = [], promise_list = [],
url_promise_list = [],
allDocs_result = gadget.state.allDocs_result, allDocs_result = gadget.state.allDocs_result,
counter, pagination_message = '',
pagination_message = ''; value,
counter;
column_list = JSON.parse(gadget.state.column_list_json); column_list = JSON.parse(gadget.state.column_list_json);
// for actual allDocs_result structure see ref:gadget_erp5_jio.js // for actual allDocs_result structure see ref:gadget_erp5_jio.js
...@@ -496,17 +498,42 @@ ...@@ -496,17 +498,42 @@
} }
}) })
); );
for (j = 0; j < column_list.length; j += 1) {
value = allDocs_result.data.rows[i].value[column_list[j][0]] || "";
if (value.url_column) {
if (value.jio_key) {
url_promise_list.push(
gadget.getUrlFor({
command: 'change',
options: {
jio_key : value.jio_key,
view: value.view || 'view'
}
}));
} else if (value.absolute_url) {
url_promise_list.push(value.absolute_url);
} else {
url_promise_list.push(false);
}
}
}
} }
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return RSVP.all(promise_list); return RSVP.all([
RSVP.all(promise_list),
RSVP.all(url_promise_list)
]);
}) })
.push(function (result_list) {
.push(function (line_link_list) {
var row_list = [], var row_list = [],
value, value,
cell_list, cell_list,
url_value,
index = 0,
listbox_tbody_template, listbox_tbody_template,
line_link_list = result_list[0],
url_column_list = result_list[1],
setNonEditable = function (cell) {cell.editable = false; }; setNonEditable = function (cell) {cell.editable = false; };
// reset list of UIDs of editable sub-documents // reset list of UIDs of editable sub-documents
gadget.props.listbox_uid_dict = { gadget.props.listbox_uid_dict = {
...@@ -520,6 +547,15 @@ ...@@ -520,6 +547,15 @@
cell_list = []; cell_list = [];
for (j = 0; j < column_list.length; j += 1) { for (j = 0; j < column_list.length; j += 1) {
value = allDocs_result.data.rows[i].value[column_list[j][0]] || ""; value = allDocs_result.data.rows[i].value[column_list[j][0]] || "";
//url column
// get url value
if (value.url_column) {
url_value = url_column_list[index];
value = value.column_value || "";
index += 1;
} else {
url_value = line_link_list[i];
}
// value can be simply just a value in case of non-editable field // value can be simply just a value in case of non-editable field
// thus we construct "field_json" manually and insert the value in "default" // thus we construct "field_json" manually and insert the value in "default"
if (value.constructor !== Object) { if (value.constructor !== Object) {
...@@ -528,7 +564,7 @@ ...@@ -528,7 +564,7 @@
'default': value 'default': value
}; };
} }
value.href = line_link_list[i]; value.href = url_value;
value.editable = value.editable && gadget.state.editable; value.editable = value.editable && gadget.state.editable;
value.line = i; value.line = i;
value.column = j; value.column = j;
......
...@@ -236,7 +236,7 @@ ...@@ -236,7 +236,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>966.8130.3423.18312</string> </value> <value> <string>965.22051.43640.11997</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -254,8 +254,8 @@ ...@@ -254,8 +254,8 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1520872134.75</float> <float>1518018366.8</float>
<string>UTC</string> <string>GMT+1</string>
</tuple> </tuple>
</state> </state>
</object> </object>
......
...@@ -52,6 +52,7 @@ from Products.PythonScripts.Utility import allow_class ...@@ -52,6 +52,7 @@ from Products.PythonScripts.Utility import allow_class
from Products.PageTemplates.PageTemplateFile import PageTemplateFile from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from warnings import warn from warnings import warn
import cgi import cgi
from ZTUtils import make_query
DEFAULT_LISTBOX_DISPLAY_STYLE = 'table' DEFAULT_LISTBOX_DISPLAY_STYLE = 'table'
DEFAULT_LISTBOX_PAGE_NAVIGATION_TEMPLATE = 'ListBox_viewSliderPageNavigationRenderer' DEFAULT_LISTBOX_PAGE_NAVIGATION_TEMPLATE = 'ListBox_viewSliderPageNavigationRenderer'
...@@ -350,6 +351,15 @@ class ListBoxWidget(Widget.Widget): ...@@ -350,6 +351,15 @@ class ListBoxWidget(Widget.Widget):
required=0) required=0)
property_names.append('url_columns') property_names.append('url_columns')
url_parameter_dict = fields.ListTextAreaField('url_parameter_dict',
title="URL Parameter Dict",
description=(
"An optional dict of parameter which is used to construct a custom URL."),
default={},
required=0)
property_names.append('url_parameter_dict')
untranslatable_columns = fields.ListTextAreaField('untranslatable_columns', untranslatable_columns = fields.ListTextAreaField('untranslatable_columns',
title="Untranslatable Columns", title="Untranslatable Columns",
description=( description=(
...@@ -2291,6 +2301,7 @@ class ListBoxHTMLRendererLine(ListBoxRendererLine): ...@@ -2291,6 +2301,7 @@ class ListBoxHTMLRendererLine(ListBoxRendererLine):
brain = self.getBrain() brain = self.getBrain()
encoding = renderer.getEncoding() encoding = renderer.getEncoding()
url_column_dict = dict(renderer.getUrlColumnList()) url_column_dict = dict(renderer.getUrlColumnList())
url_parameter_dict = renderer.field.get_value('url_parameter_dict')
selection = renderer.getSelection() selection = renderer.getSelection()
selection_name = renderer.getSelectionName() selection_name = renderer.getSelectionName()
ignore_layout = int(request.get('ignore_layout', ignore_layout = int(request.get('ignore_layout',
...@@ -2322,9 +2333,28 @@ class ListBoxHTMLRendererLine(ListBoxRendererLine): ...@@ -2322,9 +2333,28 @@ class ListBoxHTMLRendererLine(ListBoxRendererLine):
no_link = self.isSummary() no_link = self.isSummary()
url_method = None url_method = None
url = None url = None
if url_parameter_dict:
if sql in url_parameter_dict:
result_dict = url_parameter_dict[sql].copy()
for key in result_dict:
value = getattr(brain, result_dict[key], None)
if key != 'view':
if callable(value):
try:
result_dict[key] = value(selection=selection, selection_name=selection.getName(), column_id=sql, index=self.index)
except TypeError:
result_dict[key] = value()
if result_dict:
#this one is not good, should get restrivedocumet's path
if 'jio_key' not in result_dict:
url = request.physicalPathToURL(brain.getPath())
else:
url = self.getObject().getPortalObject().restrictedTraverse(result_dict['jio_key']).absolute_url()
if 'parameter' in result_dict:
url = '%s?%s' % (url, make_query(result_dict['parameter']))
# Find an URL method. # Find an URL method.
if url_column_dict.has_key(sql): elif url_column_dict.has_key(sql):
url_method_id = url_column_dict.get(sql) url_method_id = url_column_dict.get(sql)
if url_method_id != sql: if url_method_id != sql:
if url_method_id not in (None, ''): if url_method_id not in (None, ''):
......
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