data sources: handle manual edit variations when importing formdefs (#47069)
This commit is contained in:
parent
e97eb01a23
commit
f4ced75dd4
|
@ -343,6 +343,24 @@ def test_invalid_field_type():
|
|||
FormDef.import_from_xml(BytesIO(export), include_id=True)
|
||||
|
||||
|
||||
def test_invalid_data_source():
|
||||
# manually edited exports
|
||||
formdef = FormDef()
|
||||
formdef.name = 'foo'
|
||||
formdef.fields = [fields.StringField(
|
||||
id='1', type='string',
|
||||
data_source={'type': 'xxx'})]
|
||||
export = ET.tostring(export_to_indented_xml(formdef))
|
||||
export = export.replace(b'\n', b'').replace(b' ', b'')
|
||||
export = export.replace(b'<data_source><type>xxx</type></data_source>', b'<data_source><type/></data_source>')
|
||||
formdef2 = FormDef.import_from_xml(BytesIO(export))
|
||||
assert formdef2.fields[0].data_source == {}
|
||||
|
||||
export = export.replace(b'<data_source><type/></data_source>', b'<data_source> </data_source>')
|
||||
formdef2 = FormDef.import_from_xml(BytesIO(export))
|
||||
assert formdef2.fields[0].data_source == {}
|
||||
|
||||
|
||||
def test_unknown_data_source():
|
||||
formdef = FormDef()
|
||||
formdef.name = 'foo'
|
||||
|
|
|
@ -273,6 +273,8 @@ def get_object(data_source):
|
|||
if not data_source:
|
||||
return None
|
||||
ds_type = data_source.get('type')
|
||||
if ds_type is None:
|
||||
return None
|
||||
if ds_type in ('json', 'jsonp', 'geojson', 'formula'):
|
||||
named_data_source = NamedDataSource()
|
||||
named_data_source.data_source = data_source
|
||||
|
|
|
@ -328,6 +328,20 @@ class Field(object):
|
|||
elif node.text:
|
||||
self.condition = {'type': 'python', 'value': force_str(node.text).strip()}
|
||||
|
||||
def data_source_init_with_xml(self, node, charset, include_id=False):
|
||||
self.data_source = {}
|
||||
if node is None:
|
||||
return
|
||||
if node.findall('type'):
|
||||
self.data_source = {
|
||||
'type': xml_node_text(node.find('type')),
|
||||
'value': xml_node_text(node.find('value')),
|
||||
}
|
||||
if self.data_source.get('type') is None:
|
||||
self.data_source = {}
|
||||
elif self.data_source.get('value') is None:
|
||||
del self.data_source['value']
|
||||
|
||||
def prefill_init_with_xml(self, node, charset, include_id=False):
|
||||
self.prefill = {}
|
||||
if node is not None and node.findall('type'):
|
||||
|
|
Loading…
Reference in New Issue