Commit 623fdf1e authored by Roque's avatar Roque

discussion tool: removing hardcoded form definition in hateoas script

parent 9e6c6ba7
......@@ -392,6 +392,7 @@ def getFieldDefault(form, field, key, value=None):
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 = {}
......@@ -422,6 +423,9 @@ 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
......@@ -885,6 +889,7 @@ 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,
......@@ -897,7 +902,6 @@ def renderForm(traversed_document, form, response_dict, key_prefix=None, selecti
form_definition=True
)
}
use_relation_form_page_template = (form.pt == "relation_form")
if use_relation_form_page_template:
# Provide the list of possible listboxes
......@@ -955,6 +959,7 @@ 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
......@@ -1136,7 +1141,6 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=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):
if relative_url:
try:
traversed_document = site_root.restrictedTraverse(str(relative_url))
......@@ -1192,7 +1196,6 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
'status': statusLevelToString(portal_status_level)
}
if (restricted == 1) and (portal.portal_membership.isAnonymousUser()):
#if False:
#when appcache tries to call this script it fails due unauthorized if not logged in
......@@ -1329,8 +1332,10 @@ 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
......@@ -1477,11 +1482,25 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
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
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
elif relative_url == 'portal_workflow':
result_dict['_links']['action_worklist'] = {
"href": url_template_dict['worklist_template'] % {
......@@ -2087,9 +2106,15 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
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: achieve this iterating the group_list fields and calling renderField
# TODO: CHECK differences with result_dict["field_info_dict"] like key is "field_my_title" instead of "title" and specially my_text_content
if result_dict["title"] == "HTML Post":
result_dict["_embedded"]["_view"]["my_form_definition"]["default"]["field_info_dict"] = {
"my_title": {
......@@ -2144,7 +2169,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