Commit 70ef39ed authored by Roque's avatar Roque

discussion_tool: hardcoded json form definition fully removed

- hateoas script only returns the form definition
- the form rendering is done in the JS side (controller gadget)
parent 80d7bff2
......@@ -408,7 +408,7 @@ def getFieldRawProperties(field, meta_type=None, key=None, key_prefix=None):
for key in field.values.keys():
# sometimes, field.values returns a key as string and also as a tuple
if type(key) is str:
result["values"][key] = ""
result["values"][key] = field.values[key]
for key in field.tales.keys():
if field.tales[key]:
result["tales"][key] = str(field.tales[key])
......@@ -858,7 +858,7 @@ def renderForm(traversed_document, form, response_dict, key_prefix=None, selecti
# prevent allowing editing if user doesn't have permission
include_action = False
# [HARDCODED] edit is allow until I set the permissions properly
# [HARDCODED] when adding hateoas script url to appcache, it fails due to lack of permissions
# TODO: solve this
include_action = True
if (include_action):
......@@ -917,8 +917,7 @@ def renderForm(traversed_document, form, response_dict, key_prefix=None, selecti
is_portal=False,
mode='traverse',
restricted=1,
view='view',
form_definition=True
view='view'
)
}
use_relation_form_page_template = (form.pt == "relation_form")
......@@ -1158,7 +1157,7 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
response=None, view=None, mode=None,
query=None, select_list=None, limit=None, form=None,
relative_url=None, restricted=None, list_method=None,
default_param_json=None, form_relative_url=None, extra_param_json=None, form_definition=False):
default_param_json=None, form_relative_url=None, extra_param_json=None):
if relative_url:
try:
traversed_document = site_root.restrictedTraverse(str(relative_url))
......@@ -2113,7 +2112,8 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
else:
raise NotImplementedError("Unsupported mode %s" % mode)
#if form has my_form_definition field, set it for the json-form
# if form has my_form_definition field, set it with fields_raw_properties and form_definition
# my_form_definition will be used for rendering in JS side
if "_embedded" in result_dict:
if "_view" in result_dict["_embedded"]:
if "my_form_definition" in result_dict["_embedded"]["_view"]:
......@@ -2122,67 +2122,8 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
default_form_definition["_actions"] = result_dict["_embedded"]["_view"]["_actions"]
if result_dict["fields_raw_properties"]:
default_form_definition["fields_raw_properties"] = result_dict["fields_raw_properties"].copy()
# HARDCODED FOR TEST
default_form_definition["TESTING_raw_properties"] = result_dict["fields_raw_properties"].copy()
result_dict.pop('fields_raw_properties', None)
result_dict["_embedded"]["_view"]["my_form_definition"]["default"] = default_form_definition
# [HARDCODED] hardcoded rendered form for "HTML Post" until JS side render is done
if result_dict["title"] == "HTML Post":
result_dict["_embedded"]["_view"]["my_form_definition"]["default"]["fields_raw_properties"] = {
"my_title": {
"title": "Title",
"default": "Undefined title",
"editable": 1,
"key": "title",
"type": "StringField"
},
"my_text_content": {
"editable": 1,
"key": "text_content",
#"renderjs_extra": '{"editor": "fck_editor",' +
# '"maximize": true}',
#"type": "GadgetField",
"type": "TextAreaField",
#"url": "gadget_editor.html",
#"sandbox": "public"
},
"my_source_reference": {
"title": "source reference",
"default": "Undefined ref",
"editable": 0,
"key": "source_reference",
"type": "StringField"
},
"my_portal_type": {
"title": "Portal type",
"default": "Undefined title",
"editable": 0,
"key": "portal_type",
"type": "StringField"
},
"your_modification_date": {
"title": "modification_date",
"default": "",
"editable": 0,
"key": "modification_date",
"type": "StringField"
},
"my_translated_validation_state_title": {
"title": "translated",
"default": "Undefined translated",
"editable": 0,
"key": "translated",
"type": "StringField"
},
"form_id": {
"title": "form id",
"default": "Undefined form id",
"editable": 0,
"hidden": 1,
"key": "form_id",
"type": "StringField"
}
}
return result_dict
mime_type = 'application/hal+json'
......
......@@ -3,6 +3,74 @@
(function (document, window, rJS, RSVP, jIO, console, XMLHttpRequest) {
"use strict";
function renderField(field_id, field_definition, document) {
var result = {};
for (var key in field_definition.values) {
// order to get the final value (based on Field.py get_value)
// 1.tales, 2.override, 3.form-def-value, 4.context-default
var raw_value = field_definition.values[key],
tales_expr = field_definition.tales[key],
override = field_definition.overrides[key],
final_value;
final_value = undefined;
if (tales_expr !== undefined && tales_expr !== null && tales_expr !== '') {
try {
final_value = eval(tales_expr);
} catch (e) {} // TALES expressions are usually python code, so ignore
}
if (final_value === undefined) {
if (override !== undefined && override !== null && override !== '') {
final_value = override;
} else if (raw_value !== undefined && raw_value !== null && raw_value !== '') {
final_value = raw_value;
} else if (document && document.hasOwnProperty(key)) {
final_value = document[key];
}
}
if (final_value !== undefined && final_value !== null && final_value !== '') {
result[key] = final_value;
}
}
result.type = field_definition.type;
result.key = field_id;
if (document && document.hasOwnProperty(field_id)) {
result["default"] = document[field_id];
}
return result;
}
function renderForm(form_definition, document) {
var raw_properties = form_definition.fields_raw_properties;
var form_json = {
erp5_document: {
"_embedded": {"_view": {}},
"_links": {}
},
form_definition: form_definition
}, i, j, fields, field_info, my_element, element_id;
for (i = 0; i < form_definition.group_list.length; i += 1) {
fields = form_definition.group_list[i][1];
for (j = 0; j < fields.length; j += 1) {
my_element = fields[j][0];
if (my_element.startsWith("my_")) {
element_id = my_element.replace("my_", "");
} else if (my_element.startsWith("your_")) {
element_id = my_element.replace("your_", "");
} else {
element_id = my_element;
}
if (raw_properties.hasOwnProperty(my_element)) {
field_info = raw_properties[my_element];
var rendered_field = renderField(element_id, field_info, document);
form_json.erp5_document._embedded._view[my_element] = rendered_field;
}
}
}
form_json.erp5_document._embedded._view._actions = form_definition._actions;
form_json.erp5_document._links = form_definition._links;
return form_json;
}
rJS(window)
/////////////////////////////////////////////////////////////////
......@@ -44,7 +112,7 @@
'_viewAsJio';
return new RSVP.Queue()
.push(function () {
// manual request to hateoas script to test if the script json dict result is correctly cached
// manual request to hateoas script to test if the script json dict result is correctly cached in appcache
var xmlHttp = new XMLHttpRequest(),
url = "https://softinst112382.host.vifib.net/erp5/web_site_module/officejs_discussion_tool/hateoas/ERP5Document_getHateoas?mode=traverse&relative_url=portal_skins%2Ferp5_officejs_jio_connector%2FHTMLPost_viewAsJio&view=jio_view";
xmlHttp.open("GET", url, false); // false for synchronous request
......@@ -56,41 +124,14 @@
// jio.get ends up calling hateoas script
return jio_storage.get(form_path)
.push(function (result) {
//console.log("jio_storage.get(" + form_path + ") RESULT:");
//console.log(result);
return result.form_definition;
});
});
});
})
.declareMethod("generateJsonRenderForm", function (form_definition, document) {
var form_json = {
erp5_document: {
"_embedded": {"_view": {}},
"_links": {}
},
form_definition: form_definition
}, i, j, fields, field_info, my_element, element_id;
for (i = 0; i < form_definition.group_list.length; i += 1) {
fields = form_definition.group_list[i][1];
for (j = 0; j < fields.length; j += 1) {
my_element = fields[j][0];
if (my_element.startsWith("my_")) {
element_id = my_element.replace("my_", "");
} else if (my_element.startsWith("your_")) {
element_id = my_element.replace("your_", "");
}
field_info = form_definition.field_info_dict[my_element];
if (document && document.hasOwnProperty(element_id)) {
field_info["default"] = document[element_id];
}
form_json.erp5_document._embedded._view[my_element] = field_info;
}
}
form_json.erp5_document._embedded._view._actions = form_definition._actions;
form_json.erp5_document._links = form_definition._links;
return form_json;
.declareMethod("renderForm", function (form_definition, document) {
return renderForm(form_definition, document);
})
.allowPublicAcquisition('submitContent', function (options) {
var gadget = this,
......@@ -184,7 +225,7 @@
.onStateChange(function (modification_dict) {
var fragment = document.createElement('div'),
gadget = this;
return gadget.generateJsonRenderForm(gadget.state.form_definition, gadget.state.doc)
return gadget.renderForm(gadget.state.form_definition, gadget.state.doc)
.push(function (form_json) {
if (!modification_dict.hasOwnProperty('child_gadget_url')) {
return gadget.getDeclaredGadget('fg')
......
......@@ -225,7 +225,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>973.64002.24510.31556</string> </value>
<value> <string>974.12826.51404.40908</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -243,7 +243,7 @@
</tuple>
<state>
<tuple>
<float>1551377466.25</float>
<float>1551979865.57</float>
<string>UTC</string>
</tuple>
</state>
......
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