Skip to content

Commit

Permalink
Merge pull request #209 from collective/208-better-reporting-of-faile…
Browse files Browse the repository at this point in the history
…d-exported-objects
  • Loading branch information
pbauer authored Jul 5, 2023
2 parents 624a0bd + 1082828 commit 5f035a6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/collective/exportimport/export_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __call__(
download_to_server=False,
migration=True,
include_revisions=False,
write_errors=False
):
self.portal_type = portal_type or []
if isinstance(self.portal_type, str):
Expand Down Expand Up @@ -141,6 +142,7 @@ def __call__(
("2", _(u"as blob paths")),
)
self.include_revisions = include_revisions
self.write_errors = write_errors or self.request.form.get("write_errors")

self.update()

Expand Down Expand Up @@ -175,6 +177,7 @@ def __call__(
filename = self.path.split("/")[-1]
filename = "{}.json".format(filename)

self.errors = []
content_generator = self.export_content()

number = 0
Expand All @@ -197,9 +200,13 @@ def __call__(
f.write(",")
json.dump(datum, f, sort_keys=True, indent=4)
if number:
if self.errors and self.write_errors:
f.write(",")
errors = {"unexported_paths": self.errors}
json.dump(errors, f, indent=4)
f.write("]")
msg = _(u"Exported {} items ({}) as {} to {}").format(
number, ", ".join(self.portal_type), filename, filepath
msg = _(u"Exported {} items ({}) as {} to {} with {} errors").format(
number, ", ".join(self.portal_type), filename, filepath, len(self.errors)
)
logger.info(msg)
api.portal.show_message(msg, self.request)
Expand All @@ -221,8 +228,12 @@ def __call__(
f.write(",")
json.dump(datum, f, sort_keys=True, indent=4)
if number:
if self.errors and self.write_errors:
f.write(",")
errors = {"unexported_paths": self.errors}
json.dump(errors, f, indent=4)
f.write("]")
msg = _(u"Exported {} {}").format(number, self.portal_type)
msg = _(u"Exported {} {} with {} errors").format(number, self.portal_type, len(self.errors))
logger.info(msg)
api.portal.show_message(msg, self.request)
response = self.request.response
Expand Down Expand Up @@ -292,10 +303,14 @@ def export_content(self):
try:
obj = brain.getObject()
except Exception:
logger.exception(u"Error getting brain %s", brain.getPath(), exc_info=True)
msg = u"Error getting brain {}".format(brain.getPath())
self.errors.append({'path':None, 'message': msg})
logger.exception(msg, exc_info=True)
continue
if obj is None:
logger.error(u"brain.getObject() is None %s", brain.getPath())
msg = u"brain.getObject() is None {}".format(brain.getPath())
logger.error(msg)
self.errors.append({'path':None, 'message': msg})
continue
obj = self.global_obj_hook(obj)
if not obj:
Expand All @@ -313,7 +328,9 @@ def export_content(self):

yield item
except Exception:
logger.exception(u"Error exporting %s", obj.absolute_url(), exc_info=True)
msg = u"Error exporting {}".format(obj.absolute_url())
self.errors.append({'path':obj.absolute_url(), 'message':msg})
logger.exception(msg, exc_info=True)

def portal_types(self):
"""A list with info on all content types with existing items."""
Expand Down
16 changes: 16 additions & 0 deletions src/collective/exportimport/templates/export_content.pt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@
</label>
</div>

<div class="field mb-3">
<label>
<input
type="checkbox"
class="form-check-input"
name="write_errors:boolean"
id="write_errors"
tal:attributes="checked python: 'checked' if view.write_errors else None"
/>
<span i18n:translate="">Write out Errors to file.</span>
<span class="formHelp" i18n:translate="">
Checking this box puts a list of object paths at the end of the export file that failed to export.
</span>
</label>
</div>

<div class="field mb-3">
<div class="form-check">
<input class="form-check-input" type="radio" name="download_to_server:int" value="0" id="download_local" checked="checked">
Expand Down

0 comments on commit 5f035a6

Please sign in to comment.