Commit 94508ae2 authored by Georgios Dagkakis's avatar Georgios Dagkakis

BusinessTemplate: don't export in a different file for missing attribute case.

Only xml metadata should be exported in this case.
In the previous version the check of hasattr could pass
if attribute is defined as an attribute of the class and not of the instance,
causing also AttributeError later when the code tries to invoke delattr.

Also, restructure export. First try to delete property and then
export as separate file.

Test also added.
parent b46932af
......@@ -857,20 +857,33 @@ class ObjectTemplateItem(BaseTemplateItem):
if property_and_extension_exported_separately_dict:
for record_id, record in property_and_extension_exported_separately_dict.iteritems():
extension = record
exported_property_type = record_id
if hasattr(obj, exported_property_type):
exported_property = getattr(obj, exported_property_type)
if isinstance(exported_property, unicode):
exported_property = str(exported_property.encode('utf-8'))
elif not isinstance(exported_property, str):
exported_property = str(exported_property)
# we copy the object from the context. Sometimes this changes
# output_encoding, so we keep it here to restore.
reset_output_encoding = False
if hasattr(obj, 'output_encoding'):
reset_output_encoding = True
output_encoding = obj.output_encoding
obj = obj._getCopy(context)
try:
exported_property = getattr(obj, record_id)
# Delete this attribute from the object.
# in case the related Portal Type does not exist, the object may be broken.
# So we cannot delattr, but we can delete the key of its its broken state
if isinstance(obj, ERP5BaseBroken):
del obj.__Broken_state__[record_id]
obj._p_changed = 1
else:
delattr(obj, record_id)
except (AttributeError, KeyError):
# property was not set on instance,
# do nothing, only .xml metadata will be exported
pass
else:
# export a separate file with the data
if isinstance(exported_property, unicode):
exported_property = str(exported_property.encode('utf-8'))
elif not isinstance(exported_property, str):
exported_property = str(exported_property)
f = StringIO(exported_property)
bta.addObject(f, key, path=path, ext=extension)
......@@ -878,16 +891,11 @@ class ObjectTemplateItem(BaseTemplateItem):
# again remove useless properties
obj = self.removeProperties(obj, 1, keep_workflow_history = True)
# in case the related Portal Type does not exist, the object may be broken.
# So we cannot delattr, but we can delet the het of its its broken state
if isinstance(obj, ERP5BaseBroken):
del obj.__Broken_state__[exported_property_type]
if reset_output_encoding:
if isinstance(obj, ERP5BaseBroken):
self._objects[obj_key].__Broken_state__['output_encoding'] = output_encoding
obj._p_changed = 1
else:
delattr(obj, exported_property_type)
if reset_output_encoding:
obj.output_encoding = output_encoding
transaction.savepoint(optimistic=True)
......
......@@ -1023,3 +1023,48 @@ AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
self.assertTrue(os.path.exists(os.path.join(self.export_dir, 'bt')))
self.assertTrue(os.path.exists(test_component_path+'.xml'))
self.assertTrue(os.path.exists(test_component_path+'.py'))
def test_twoFileImportExportForFileWithNoData(self):
"""
Test Business Template Import And Export With File
that has no data attribute. Only .xml metadata is exported
"""
file_title = "foo"
file_document_kw = {"title": file_title,
"portal_type": "File"}
file_page = self.portal.document_module.newContent(**file_document_kw)
# 'data' is not in the __dict__ of the File instance
self.assertFalse('data' in file_page.__dict__)
# Nonetheless, 'data' is defined in File class as empty string
self.assertEquals(getattr(file_page, 'data'), '')
file_document_kw['id'] = file_id = file_page.getId()
self.template.edit(template_path_list=['document_module/'+file_id,])
file_document_path = os.path.join(self.cfg.instancehome, self.export_dir,
'PathTemplateItem', 'document_module',
file_id)
self.template.build()
self.tic()
self.template.export(path=self.export_dir, local=True)
self.tic()
self.assertTrue(os.path.exists(file_document_path+'.xml'))
# check that there is no other file exported
self.assertEqual(len(os.listdir(file_document_path.rsplit('/', 1)[0])), 1)
import_template = self._importBusinessTemplate()
self.portal.document_module.manage_delObjects([file_id])
import_template.install()
file_page = self.portal.document_module[file_id]
for property_id, property_value in file_document_kw.iteritems():
self.assertEqual(getattr(file_page, property_id), property_value)
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