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

[13.0][IMP] report_csv: add encoding option #711

Merged
merged 1 commit into from
Jul 24, 2023

Conversation

AungKoKoLin1997
Copy link
Contributor

@qrtl
This PR make an enhancement for generating CSV files with encoding.

@AungKoKoLin1997 AungKoKoLin1997 changed the title [12.0][IMP] report_csv: add encoding option [13.0][IMP] report_csv: add encoding option Jan 18, 2023
Copy link
Sponsor Member

@yostashiro yostashiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AungKoKoLin1997 Can you please add simple tests with encodings with 'cp932' and something random like 'xyz'?

@@ -9,6 +9,9 @@ class ReportAction(models.Model):
_inherit = "ir.actions.report"

report_type = fields.Selection(selection_add=[("csv", "csv")])
encoding = fields.Char(
help="Encoding to be applied to the generated CSV file. " "e.g. cp932"
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
help="Encoding to be applied to the generated CSV file. " "e.g. cp932"
help="Encoding to be applied to the generated CSV file. e.g. cp932"

Copy link
Sponsor Member

@yostashiro yostashiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed to include this in the previous review.

@@ -46,6 +46,9 @@ def create_csv_report(self, docids, data):
file = csv.DictWriter(file_data, **self.csv_report_options())
self.generate_csv_report(file, data, objs)
file_data.seek(0)
encoding = self._context.get("encoding")
if encoding:
return file_data.read().encode(encoding), "csv"
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return file_data.read().encode(encoding), "csv"
try:
return file_data.read().encode(encoding), "csv"
except Exception as e:
raise UserError(_("Failed to encode the data with the encoding set in the report."))

@yostashiro
Copy link
Sponsor Member

Please also update USAGE.rst to include a comment about encoding.

@AungKoKoLin1997 AungKoKoLin1997 force-pushed the 13.0-imp-report-csv branch 2 times, most recently from 151896d to d5bd55f Compare January 18, 2023 06:53
Comment on lines 51 to 60
<report
id="partner_csv"
model="res.partner"
string="Print to CSV"
report_type="csv"
encoding="cp932"
name="module_name.report_name"
file="res_partner"
attachment_use="False"
/>
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the report tag doesn't take the encoding parameter. Did you try this yourself?

We may simply add the following comment instead of a lengthy explanation:

Update encoding with an appropriate value (e.g. cp932) as necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. You are right. We can only pass value to encoding when we create record with "ir.actions.report".

Copy link
Sponsor Member

@yostashiro yostashiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

Comment on lines 1 to 8
In order to add encode error handing in csv report you need to choose
encode error handling in reports.

* Go to Settings > Technical > Reports > Your csv report
* Add encoding first
* Choose encode error handling

Note: You can't see encode error handling if you don't add encoding first.
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In order to add encode error handing in csv report you need to choose
encode error handling in reports.
* Go to Settings > Technical > Reports > Your csv report
* Add encoding first
* Choose encode error handling
Note: You can't see encode error handling if you don't add encoding first.
In case the exported CSV report should be encoded in another system than UTF-8, following fields of the report record (*Settings > Technical > Reports*) should be populated accordingly.
* Encoding: set an encoding system (such as cp932)
* Encode Error Handling: select 'Ignore' or 'Replace' as necessary.
* 'Ignore': in case of an encoding error, the problematic character will be removed from the exported file.
* 'Replace': in case of an encoding error, the problematic character will be replaced with '?' symbol.
* Leaving the field blank: in case of an encoding error, the report generation fails with an error message.

@yostashiro
Copy link
Sponsor Member

@AungKoKoLin1997 Can you go ahead and create PRs for the upstream versions?

if error_handling:
return file_data.read().encode(encoding, errors=error_handling), "csv"
try:
logging.info("Encdonig__________")
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logging.info("Encdonig__________")

Copy link
Member

@fkantelberg fkantelberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well. Can you update the forward ports with the tests too?

@OCA-git-bot
Copy link
Contributor

This PR has the approved label and has been created more than 5 days ago. It should therefore be ready to merge by a maintainer (or a PSC member if the concerned addon has no declared maintainer). 🤖

@thomaspaulb
Copy link

@AungKoKoLin1997 Can you squash commits? Then I'll merge.

@AungKoKoLin1997
Copy link
Contributor Author

@thomaspaulb I squashed the commits. Please merge it. Thank you.

@thomaspaulb
Copy link

/ocabot merge patch

@OCA-git-bot
Copy link
Contributor

On my way to merge this fine PR!
Prepared branch 13.0-ocabot-merge-pr-711-by-thomaspaulb-bump-patch, awaiting test results.

@OCA-git-bot OCA-git-bot merged commit b9cdde5 into OCA:13.0 Jul 24, 2023
6 checks passed
@OCA-git-bot
Copy link
Contributor

Congratulations, your PR was merged at 5b91680. Thanks a lot for contributing to OCA. ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants