Commit 80d7bff2 authored by Roque's avatar Roque

discussion tool: hateoas script returns form definition with properties raw values"

- it still returns a hardcoded rendered json form until JS side (controller) does the rendering
parent 623fdf1e
......@@ -389,10 +389,33 @@ def getFieldDefault(form, field, key, value=None):
return "%s" % value
return value
def getFieldRawProperties(field, meta_type=None, key=None, key_prefix=None):
""" Return the raw properties of the field """
if meta_type is None:
meta_type = field.meta_type
if key is None:
key = field.generate_field_key(key_prefix=key_prefix)
if meta_type == "ProxyField":
meta_type = field.getRecursiveTemplateField().meta_type
result = {
"type": meta_type,
"key": key,
"values": {},
"tales": {},
"overrides": field.overrides,
"message_values": field.message_values
}
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] = ""
for key in field.tales.keys():
if field.tales[key]:
result["tales"][key] = str(field.tales[key])
return result
def renderField(traversed_document, field, form, value=MARKER, meta_type=None, key=None, key_prefix=None, selection_params=None, request_field=True):
"""Extract important field's attributes into `result` dictionary."""
log("[DEBUG] rendering field %s for traversed_document %s and form %s " % (field.id, traversed_document, form))
if selection_params is None:
selection_params = {}
......@@ -423,9 +446,6 @@ def renderField(traversed_document, field, form, value=MARKER, meta_type=None, k
"description": field.get_value("description"),
}
if field.get_value("extra"):
result["renderjs_extra"] = field.get_value("extra")
if "Field" in meta_type:
# fields have default value and can be required (unlike boxes)
result["required"] = field.get_value("required") if field.has_value("required") else None
......@@ -889,7 +909,6 @@ def renderForm(traversed_document, form, response_dict, key_prefix=None, selecti
'name': form.id
}
log("[DEBUG] calling calculateHateoas of form_definition")
response_dict['_embedded'] = {
'form_definition': calculateHateoas(
traversed_document=form,
......@@ -959,7 +978,6 @@ def renderForm(traversed_document, form, response_dict, key_prefix=None, selecti
if not field.get_value("enabled"):
continue
try:
log("[DEBUG] calling renderField")
response_dict[field.id] = renderField(traversed_document, field, form, key_prefix=key_prefix, selection_params=selection_params, request_field=not use_relation_form_page_template)
if field_errors.has_key(field.id):
response_dict[field.id]["error_text"] = field_errors[field.id].error_text
......@@ -1332,10 +1350,8 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
# REQUEST but expect all (formerly) URL query parameters to appear in their **kw
# thus we send extra_param_json (=rjs way of passing parameters to REQUEST) as
# selection_params so they get into callable's **kw.
log("[DEBUG] CALLING RENDERFORM from calculatehateoas. with form %s" % view_instance.id)
renderForm(traversed_document, view_instance, embedded_dict,
selection_params=extra_param_json, extra_param_json=extra_param_json)
log("[DEBUG] returned from renderForm call in calculatehateoas")
result_dict['_embedded'] = {
'_view': embedded_dict
......@@ -1465,41 +1481,39 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
'method': 'POST',
'name': 'Bulk'
}
# Handle also other kind of users: instance, computer, master
person = portal.portal_membership.getAuthenticatedMember().getUserValue()
if person is not None and portal.portal_membership.checkPermission('View', person):
result_dict['_links']['me'] = {
"href": default_document_uri_template % {
"root_url": site_root.absolute_url(),
"relative_url": person.getRelativeUrl(),
"relative_url": person.getRelativeUrl(),
"script_id": script.id
},
# '_relative_url': person.getRelativeUrl()
#'_relative_url': person.getRelativeUrl()
}
else:
traversed_document_portal_type = traversed_document.getPortalType()
if traversed_document_portal_type in ("ERP5 Form", "ERP5 Report"):
renderFormDefinition(traversed_document, result_dict)
log(result_dict["group_list"])
if response is not None:
response.setHeader("Cache-Control", "private, max-age=1800")
response.setHeader("Vary", "Cookie,Authorization,Accept-Encoding")
response.setHeader("Last-Modified", DateTime().rfc822())
REQUEST.set("X-HATEOAS-CACHE", 1)
field_info_dict = {}
# check if it's the first call to calculateHateoas so traversed_document is rendered as form
fields_raw_properties = {}
# check if it's the first call to calculateHateoas so nothing was rendered yet
if REQUEST != None and response != None:
for group in traversed_document.Form_getGroupTitleAndId():
if 'hidden' in group['gid']:
continue
for field in traversed_document.get_fields_in_group(group['goid']):
my_field = renderField(traversed_document, field, traversed_document, key_prefix=None, selection_params={}, request_field=True)
field_info_dict[field.id] = my_field
if field_info_dict:
result_dict['field_info_dict'] = field_info_dict
fields_raw_properties[field.id] = getFieldRawProperties(field, key_prefix=None)
if fields_raw_properties:
result_dict['fields_raw_properties'] = fields_raw_properties
elif relative_url == 'portal_workflow':
result_dict['_links']['action_worklist'] = {
......@@ -2103,20 +2117,18 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
if "_embedded" in result_dict:
if "_view" in result_dict["_embedded"]:
if "my_form_definition" in result_dict["_embedded"]["_view"]:
result_dict["_embedded"]["_view"]["my_form_definition"]["default"] = result_dict["_embedded"]["_view"]["_embedded"]["form_definition"].copy()
result_dict["_embedded"]["_view"]["my_form_definition"]["default"]["group_list"] = result_dict["group_list"]
result_dict["_embedded"]["_view"]["my_form_definition"]["default"]["_actions"] = result_dict["_embedded"]["_view"]["_actions"]
log('result_dict["field_info_dict"]')
log(result_dict["field_info_dict"])
result_dict["_embedded"]["_view"]["my_form_definition"]["default"]["field_info_dict"] = result_dict["field_info_dict"].copy()
result_dict.pop('field_info_dict', None)
#'''
# [HARDCODED] the form definition must contain a field_info_dict with all the rendered fields
# for now is hardcoded only for "HTML Post"
# TODO: CHECK differences with result_dict["field_info_dict"] like key is "field_my_title" instead of "title" and specially my_text_content
default_form_definition = result_dict["_embedded"]["_view"]["_embedded"]["form_definition"].copy()
default_form_definition["group_list"] = result_dict["group_list"]
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"]["field_info_dict"] = {
result_dict["_embedded"]["_view"]["my_form_definition"]["default"]["fields_raw_properties"] = {
"my_title": {
"title": "Title",
"default": "Undefined title",
......@@ -2127,11 +2139,12 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
"my_text_content": {
"editable": 1,
"key": "text_content",
"renderjs_extra": '{"editor": "fck_editor",' +
'"maximize": true}',
"type": "GadgetField",
"url": "gadget_editor.html",
"sandbox": "public"
#"renderjs_extra": '{"editor": "fck_editor",' +
# '"maximize": true}',
#"type": "GadgetField",
"type": "TextAreaField",
#"url": "gadget_editor.html",
#"sandbox": "public"
},
"my_source_reference": {
"title": "source reference",
......@@ -2169,7 +2182,7 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
"key": "form_id",
"type": "StringField"
}
}#'''
}
return result_dict
mime_type = 'application/hal+json'
......
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