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

[16.0][REF][ADD] report_csv, report_format_option #864

Closed
wants to merge 1 commit into from

Conversation

AungKoKoLin1997
Copy link
Contributor

This PR refactors the encoding part of the report_csv module into a new module to enable the use of encoding features across multiple reporting modules by adding this new module as a dependency.

@qrtl QT4120

show_encoding = fields.Boolean(compute="_compute_show_encoding")
line_ending = fields.Char(
help="Line ending to be applied to the generated report. e.g., crlf"
)

This comment was marked as outdated.

_inherit = "ir.actions.report"

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"

)
show_encoding = fields.Boolean(compute="_compute_show_encoding")
line_ending = fields.Char(
help="Line ending to be applied to the generated report. e.g., crlf"
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

nitpick

Suggested change
help="Line ending to be applied to the generated report. e.g., crlf"
help="Line ending to be applied to the generated report. e.g. crlf"

@@ -0,0 +1,2 @@
This is a technical module to add encoding fields to ir.actions.report model and it does nothing by itself.
The module is expected to be a dependency of other reporting modules (e.g., report_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.

nitpick

Suggested change
The module is expected to be a dependency of other reporting modules (e.g., report_csv).
The module is expected to be a dependency of other reporting modules (e.g. report_csv).

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.

Code review + checked presentation in runboat.

compute="_compute_show_encoding",
help="Technical field to control the visibility of the encoding field.",
)
line_ending = fields.Char(
Copy link
Sponsor Member

@yostashiro yostashiro Apr 1, 2024

Choose a reason for hiding this comment

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

@AungKoKoLin1997 Can we actually make it a selection field, pre-defining 'LF', 'CRLF' and 'CR'?

I think we can also extend method _render_qweb_text() in this module to handle the conversion of line ending and encoding.

def _compute_show_encoding(self):
"""Extend this method to show the encoding field in the report form."""
for report in self:
report.show_encoding = False

This comment was marked as resolved.

def _compute_show_line_ending(self):
"""Extend this method to show the line ending field in the report form."""
for report in self:
report.show_line_ending = False

This comment was marked as resolved.

Comment on lines 23 to 25
[("LF", "LF (\\n)"), ("CRLF", "CRLF (\\r\\n)"), ("CR", "CR (\\r)")],
default="LF",
help="Select the type of line ending.",
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
[("LF", "LF (\\n)"), ("CRLF", "CRLF (\\r\\n)"), ("CR", "CR (\\r)")],
default="LF",
help="Select the type of line ending.",
[("crlf", "CRLF (\\r\\n)"), ("cr", "CR (\\r)")],
help="Select the type of line ending in case the report needs to be output with other line ending than 'LF'.",

Comment on lines 49 to 66
report = self._get_report(report_ref)
# If specific encoding is set on the report, use it; otherwise, fallback to utf-8
encoding = report.encoding or "utf-8"
# Decode content assuming it is 'utf-8' to work with it as a string
content_str = content.decode("utf-8")
# Adjust line endings if specified, while content is still a string
line_ending = report.line_ending or ""
if line_ending == "CRLF":
content_str = content_str.replace("\n", "\r\n")
elif report.line_ending == "CR":
content_str = content_str.replace("\n", "\r")
# Then encode with the desired encoding
encode_options = {}
if report.encode_error_handling:
encode_options["errors"] = report.encode_error_handling
# Re-encode the content with or without the errors parameter
content_encoded = content_str.encode(encoding, **encode_options)
return content_encoded, content_type
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
report = self._get_report(report_ref)
# If specific encoding is set on the report, use it; otherwise, fallback to utf-8
encoding = report.encoding or "utf-8"
# Decode content assuming it is 'utf-8' to work with it as a string
content_str = content.decode("utf-8")
# Adjust line endings if specified, while content is still a string
line_ending = report.line_ending or ""
if line_ending == "CRLF":
content_str = content_str.replace("\n", "\r\n")
elif report.line_ending == "CR":
content_str = content_str.replace("\n", "\r")
# Then encode with the desired encoding
encode_options = {}
if report.encode_error_handling:
encode_options["errors"] = report.encode_error_handling
# Re-encode the content with or without the errors parameter
content_encoded = content_str.encode(encoding, **encode_options)
return content_encoded, content_type
if not report.encoding and not report.line_ending:
return content, content_type
report = self._get_report(report_ref)
content_str = content.decode("utf-8")
if report.line_ending == "crlf":
content_str = content_str.replace("\n", "\r\n")
elif report.line_ending == "cr":
content_str = content_str.replace("\n", "\r")
# If specific encoding is set on the report, use it; otherwise, fallback to utf-8
encoding = report.encoding or "utf-8"
encode_options = {}
if report.encode_error_handling:
encode_options["errors"] = report.encode_error_handling
content = content_str.encode(encoding, **encode_options)
return content, content_type

@yostashiro
Copy link
Sponsor Member

@AungKoKoLin1997 Can you please add tests to report_format_option?

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.

Please apply the suggested change, squash commits and update the commit message with rationale.

Comment on lines 9 to 12
* Line Ending: Select the type of line ending—'CRLF', 'CR' or leave blank, as necessary.
* 'CRLF': Stands for 'Carriage Return' + 'Line Feed'. It's used as a line ending in Windows, signifying the end of a line and the start of a new one.
* 'CR': Stands for 'Carriage Return'. It's used in classic Mac OS as a line ending, moving the cursor to the beginning of the line.
* Leaving this field blank will default to using 'LF' (Line Feed), which is the standard line ending in Unix/Linux systems, denoting the end of a line.
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Let's make it less verbose (and more maintainable).

Suggested change
* Line Ending: Select the type of line ending—'CRLF', 'CR' or leave blank, as necessary.
* 'CRLF': Stands for 'Carriage Return' + 'Line Feed'. It's used as a line ending in Windows, signifying the end of a line and the start of a new one.
* 'CR': Stands for 'Carriage Return'. It's used in classic Mac OS as a line ending, moving the cursor to the beginning of the line.
* Leaving this field blank will default to using 'LF' (Line Feed), which is the standard line ending in Unix/Linux systems, denoting the end of a line.
* Line Ending: Select the type of line ending, 'CRLF' or 'CR', as necessary.
* 'CRLF': 'Carriage Return' + 'Line Feed' (Windows)
* 'CR': 'Carriage Return' (classic Mac OS)
* Leaving this field blank defaults to using 'LF' (Line Feed), the default behavior of Odoo.

@AungKoKoLin1997 AungKoKoLin1997 changed the title [16.0][REF] report_csv: refactor encoding part into new module [16.0][REF][ADD] report_csv, report_format_option Apr 3, 2024
@AungKoKoLin1997 AungKoKoLin1997 force-pushed the 16.0-ref-report_csv branch 2 times, most recently from ededd9a to 9ae9c03 Compare April 3, 2024 06:05
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.

Code review. LGTM.


<!-- Demo report template -->
<template id="demo_report_template">
<t t-call="web.html_container">
Copy link

Choose a reason for hiding this comment

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

Does this text report need to be html report?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. You right. We don't need it.

This commit refactors the encoding part of the report_csv module into a new module(report_format_option) to enable
the use of encoding features across multiple reporting modules by adding report_format_option as a dependency.
Copy link

@kanda999 kanda999 left a comment

Choose a reason for hiding this comment

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

Functinal and code review

@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). 🤖

@HviorForgeFlow
Copy link
Member

Question @pedrobaeza
How should we merge this PR? Seems that report_csv needs some major version bump while the new module does not.

Copy link
Member

@pedrobaeza pedrobaeza left a comment

Choose a reason for hiding this comment

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

Well, there's no debate, as you can't do such change.

@@ -8,9 +8,8 @@
"category": "Reporting",
"version": "16.0.2.0.0",
"license": "AGPL-3",
"depends": ["base", "web"],
"depends": ["base", "web", "report_format_option"],
Copy link
Member

Choose a reason for hiding this comment

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

This is incorrect, as you can't change dependencies of a merged module.

Copy link
Member

Choose a reason for hiding this comment

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

AFAIK the purpose is to split a feature to be reusable by other report modules out of the scope of the CSV.
There should be a way to achieve this change.

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

@pedrobaeza Do you mean that this goes against the stable policy and report_csv should stay as is? In that case, we will try to do this in the 17.0 migration, and we may just propose to add report_format_option in 16.0 anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, you can't do it on an existing version, or you will break existing installations. I also see that the extracted code is not worth for such "reusing", but that's only my opinion.

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Fine by me. We need report_format_option anyway for a client to apply some format conversions on text reports. That's why we proposed to split the common feature off report_csv to avoid redundancy. We will try to do this in 17.0.

@AungKoKoLin1997 Can you please close this PR and create another one just to add report_format_option?

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

Successfully merging this pull request may close these issues.

None yet

6 participants