Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T1894 improve error messages on scan import letters #1986

Open
wants to merge 8 commits into
base: 14.0
Choose a base branch
from
40 changes: 26 additions & 14 deletions sbc_compassion/models/import_letters_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ImportLettersHistory(models.Model):
("pending", _("Analyzing")),
("open", _("Open")),
("ready", _("Ready")),
("failed", _("Failed")),
("done", _("Done")),
],
compute="_compute_state",
Expand All @@ -63,6 +64,12 @@ class ImportLettersHistory(models.Model):
"import.letter.config", "Import settings", readonly=False
)

failed_file_name = fields.Char(
string="failed file name",
help="Displays the name of the file that failed the PDF analysis.",
readonly=True,
)

@api.depends(
"import_line_ids",
"import_line_ids.status",
Expand Down Expand Up @@ -197,14 +204,20 @@ def run_analyze(self, generator=None):
self.ensure_one()
self.state = "pending"
self.env.user.notify_info("Letters import started...")

for current_file, nb_files_to_import, filename in generator():
logger.info(f"{current_file}/{nb_files_to_import} : {filename}")

self.env.user.notify_success("Letters import completed !")
# remove all the files (now they are inside import_line_ids)
self.data.unlink()
self.import_completed = True
try:
for current_file, nb_files_to_import, filename in generator():
logger.info(f"{current_file}/{nb_files_to_import} : {filename}")
logger.info(f"File {filename} analyzed successfully.")

except Exception as e:
logger.error(f"Error during import: {str(e)}", exc_info=True)
self.import_completed = False
self.state = "failed"
else:
self.import_completed = True
self.env.user.notify_success("Letters import completed successfully!")
# remove all the files (now they are inside import_line_ids)
self.data.unlink()

def pdf_to_image(self, pdf_data):
pdf = fitz.Document(
Expand Down Expand Up @@ -274,10 +287,9 @@ def _analyze_pdf(self, pdf_data, file_name):
# pylint: disable=invalid-commit
self._cr.commit()
except Exception:
message = f"Couldn't import file {file_name}"
self.env.user.notify_danger(message)
logger.error(
message,
exc_info=True,
)
self.import_completed = False
self.failed_file_name = file_name
self.state = "failed"
self.env.user.notify_danger(f"Couldn't import file {file_name}")
self._compute_state = False
return
4 changes: 3 additions & 1 deletion sbc_compassion/views/import_letters_history_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<field
name="state"
widget="statusbar"
statusbar_visible="draft,open,done"
statusbar_visible="draft,open,ready,failed,done"
attrs="{'invisible': [('state', '!=', 'failed')]}"
/>
<field name="import_completed" invisible="1" />
<button
Expand Down Expand Up @@ -99,6 +100,7 @@
decoration-info="state in ('open','pending')"
decoration-success="state == 'ready'"
decoration-muted="state == 'done'"
decoration-danger="state == 'failed'"
>
<field name="create_date" />
<field name="write_date" />
Expand Down
Loading