Commit 15ea7f15 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin Committed by Titouan Soulard

erp5_json_form: Integrate dedicated method to provide clean and cached schemas

parent c456cffc
......@@ -29,6 +29,7 @@ import json
import jsonschema
from erp5.component.document.JSONType import JSONType
from erp5.component.document.TextDocument import TextDocument
import re
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions, PropertySheet
......@@ -73,7 +74,7 @@ class JSONForm(JSONType, TextDocument):
"""
Validate contained JSON with the Schema defined in the Portal Type.
"""
defined_schema = json.loads(self.getTextContent() or "")
defined_schema = json.loads(self.getInputJSONSchema() or "")
try:
jsonschema.validate(json_data, defined_schema, format_checker=jsonschema.FormatChecker())
except jsonschema.exceptions.ValidationError as err:
......@@ -86,3 +87,26 @@ class JSONForm(JSONType, TextDocument):
}
return err
return True
security.declarePublic('getInputJSONSchema')
def getInputJSONSchema(self, REQUEST=None):
"""
Clean way to retrive the schema
* Set proper cache headers
* Properly set the $id property
"""
schema = self.getTextContent()
regex = re.compile(r'"\$id": "(([^"])*)"')
result = regex.search(schema)
schema_id = self.absolute_url().strip() + "/getInputJSONSchema"
if result:
schema = schema.replace(result.group(1), schema_id, 1)
#print data
else:
schema = schema.replace("{", '{\n "$id": "%s",' % schema_id, 1)
if REQUEST is not None:
control = []
control.append( 'max-age=3600' )
control.append( 'private' )
REQUEST.RESPONSE.setHeader('Cache-control', ', '.join( control ) )
return schema
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