Skip to content

Commit

Permalink
[FIX] base_import_async: remove default values from context when crea…
Browse files Browse the repository at this point in the history
…ting the attachment.

Some default values might be present in context depending on the action we came from
when clicking on 'import' button. These default values are not intended to
be default values for the ir.attachment record.
In some cases they cause an error because a field with the same name exists on
ir.attachment, as for e.g. the 'default_type'='opportunity' value
present in the standard crm.lead action context.
  • Loading branch information
marielejeune committed Sep 18, 2023
1 parent bcd89a6 commit db24c87
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions base_import_async/models/base_import_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,17 @@ def _create_csv_attachment(self, fields, data, options, file_name):
writer.writerow(fields)
for row in data:
writer.writerow(row)
# create attachment
# create attachment. Remove default values from context
context = self.env.context
context_copy = {}

Check warning on line 93 in base_import_async/models/base_import_import.py

View check run for this annotation

Codecov / codecov/patch

base_import_async/models/base_import_import.py#L92-L93

Added lines #L92 - L93 were not covered by tests
for key in context.keys():
if not key.startswith("default_"):
context_copy[key] = context[key]

Check warning on line 96 in base_import_async/models/base_import_import.py

View check run for this annotation

Codecov / codecov/patch

base_import_async/models/base_import_import.py#L96

Added line #L96 was not covered by tests
datas = base64.encodebytes(f.getvalue().encode(encoding))
attachment = self.env["ir.attachment"].create(
{"name": file_name, "datas": datas}
attachment = (

Check warning on line 98 in base_import_async/models/base_import_import.py

View check run for this annotation

Codecov / codecov/patch

base_import_async/models/base_import_import.py#L98

Added line #L98 was not covered by tests
self.env["ir.attachment"]
.with_context(**context_copy)
.create({"name": file_name, "datas": datas})
)
return attachment

Expand Down

0 comments on commit db24c87

Please sign in to comment.