-
-
Notifications
You must be signed in to change notification settings - Fork 795
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
[12.0][ADD] report_pdf_zip_download #942
Open
AungKoKoLin1997
wants to merge
1
commit into
OCA:12.0
Choose a base branch
from
qrtl:12.0-add-batch_report_pdf_zip
base: 12.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import controllers | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# © 2017 Creu Blanca | ||
# Copyright 2024 Quartile (https://www.quartile.co) | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
{ | ||
"name": "Report PDF ZIP Download", | ||
"category": "Report", | ||
"version": "12.0.1.0.0", | ||
"author": "Quartile, Odoo Community Association (OCA)", | ||
"website": "https://github.com/OCA/reporting-engine", | ||
"license": "AGPL-3", | ||
"depends": ["web"], | ||
"data": [ | ||
"views/ir_actions_report_views.xml", | ||
"views/web_client_templates.xml", | ||
], | ||
"installable": True, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright 2024 Quartile (https://www.quartile.co) | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
import zipfile | ||
from odoo.http import content_disposition | ||
from odoo import http | ||
from odoo.http import request | ||
from odoo.addons.web.controllers.main import ReportController | ||
from io import BytesIO | ||
from datetime import datetime | ||
import json | ||
from odoo.tools.safe_eval import safe_eval | ||
import time | ||
|
||
|
||
class ExtendedReportController(ReportController): | ||
|
||
@http.route() | ||
def report_routes(self, reportname, docids=None, converter=None, **data): | ||
report = request.env['ir.actions.report']._get_report_from_name(reportname) | ||
report_name = report.report_file | ||
doc_ids = [] | ||
if converter == "zip": | ||
if docids: | ||
doc_ids = [int(i) for i in docids.split(',')] | ||
context = dict(request.env.context) | ||
if data.get('options'): | ||
data.update(json.loads(data.pop('options'))) | ||
if data.get('context'): | ||
data['context'] = json.loads(data['context']) | ||
if data['context'].get('lang'): | ||
del data['context']['lang'] | ||
context.update(data['context']) | ||
attachments = [] | ||
for doc_id in doc_ids: | ||
pdf_content, _ = report.with_context(context).render_qweb_pdf( | ||
[doc_id], data=data | ||
) | ||
if report.print_report_name: | ||
obj = request.env[report.model].browse(doc_id) | ||
report_name = safe_eval(report.print_report_name, | ||
{'object': obj, 'time': time}) | ||
report_name = report_name.replace('/', '_') | ||
pdf_name = f'{report_name}.pdf' | ||
attachments.append((pdf_name, pdf_content)) | ||
# Generate the ZIP file | ||
zip_filename = f"{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip" | ||
bitIO = BytesIO() | ||
with zipfile.ZipFile(bitIO, "w", zipfile.ZIP_DEFLATED) as zip_file: | ||
for pdf_name, pdf_content in attachments: | ||
zip_file.writestr(pdf_name, pdf_content) | ||
zip_content = bitIO.getvalue() | ||
headers = [ | ||
('Content-Type', 'application/zip'), | ||
('Content-Disposition', content_disposition(zip_filename)) | ||
] | ||
return request.make_response( | ||
zip_content, | ||
headers=headers | ||
) | ||
return super(ExtendedReportController, self).report_routes( | ||
reportname, docids, converter, **data | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import ir_actions_report |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2024 Quartile (https://www.quartile.co) | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import models, fields | ||
|
||
|
||
class IrActionsReport(models.Model): | ||
_inherit = "ir.actions.report" | ||
|
||
zip_download = fields.Boolean( | ||
help="If enabled, the report will be downloaded as a zip " | ||
"file for multiple records." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
To enable the ZIP file feature, please check the 'Zip Download' option under the 'Advanced Properties' tab for the specific report. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* `Quartile <https://www.quartile.co>`__: | ||
|
||
* Aung Ko Ko Lin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This module generates one PDF report per record, and the reports are compiled into a ZIP file if the user prints multiple records at once. If the user prints a single record, it will follow Odoo's standard behavior and will generate just a single PDF file. |
91 changes: 91 additions & 0 deletions
91
report_pdf_zip_download/static/src/js/action_manager_report.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// © 2017 Creu Blanca | ||
// Copyright 2024 Quartile (https://www.quartile.co) | ||
// License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
odoo.define("report_pdf_zip_download.report", function (require) { | ||
"use strict"; | ||
|
||
var core = require("web.core"); | ||
var ActionManager = require("web.ActionManager"); | ||
var crash_manager = require("web.crash_manager"); | ||
var framework = require("web.framework"); | ||
var session = require("web.session"); | ||
var _t = core._t; | ||
|
||
ActionManager.include({ | ||
|
||
_downloadReportZIP: function (url, actions) { | ||
framework.blockUI(); | ||
var def = $.Deferred(); | ||
var type = "zip"; | ||
var cloned_action = _.clone(actions); | ||
|
||
if (_.isUndefined(cloned_action.data) || | ||
_.isNull(cloned_action.data) || | ||
(_.isObject(cloned_action.data) && _.isEmpty(cloned_action.data))) | ||
{ | ||
if (cloned_action.context.active_ids) { | ||
url += "/" + cloned_action.context.active_ids.join(','); | ||
} | ||
} else { | ||
url += "?options=" + encodeURIComponent(JSON.stringify(cloned_action.data)); | ||
url += "&context=" + encodeURIComponent(JSON.stringify(cloned_action.context)); | ||
} | ||
|
||
var blocked = !session.get_file({ | ||
url: url, | ||
data: { | ||
data: JSON.stringify([url, type]), | ||
}, | ||
success: def.resolve.bind(def), | ||
error: function () { | ||
crash_manager.rpc_error.apply(crash_manager, arguments); | ||
def.reject(); | ||
}, | ||
complete: framework.unblockUI, | ||
}); | ||
if (blocked) { | ||
var message = _t('A popup window with your report was blocked. You ' + | ||
'may need to change your browser settings to allow ' + | ||
'popup windows for this page.'); | ||
this.do_warn(_t('Warning'), message, true); | ||
} | ||
return def; | ||
}, | ||
|
||
_triggerDownload: function (action, options, type) { | ||
var self = this; | ||
var reportUrls = this._makeReportUrls(action); | ||
if (type === "zip") { | ||
return this._downloadReportZIP(reportUrls[type], action).then(function () { | ||
if (action.close_on_report_download) { | ||
var closeAction = {type: 'ir.actions.act_window_close'}; | ||
return self.doAction(closeAction, _.pick(options, 'on_close')); | ||
} else { | ||
return options.on_close(); | ||
} | ||
}); | ||
} | ||
return this._super.apply(this, arguments); | ||
}, | ||
|
||
_makeReportUrls: function (action) { | ||
var reportUrls = this._super.apply(this, arguments); | ||
reportUrls.zip = '/report/zip/' + action.report_name; | ||
return reportUrls; | ||
}, | ||
|
||
_executeReportAction: function (action, options) { | ||
var self = this; | ||
|
||
console.log(action.data); | ||
console.log(action.context); | ||
if (action.context.active_ids && action.context.active_ids.length > 1) { | ||
if (action.report_type === 'qweb-pdf' && action.zip_download === true) { | ||
return self._triggerDownload(action, options, 'zip'); | ||
} | ||
} | ||
return this._super.apply(this, arguments); | ||
} | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<odoo> | ||
<record id="act_report_xml_view_inherit" model="ir.ui.view"> | ||
<field name="name">ir.actions.report</field> | ||
<field name="model">ir.actions.report</field> | ||
<field name="inherit_id" ref="base.act_report_xml_view" /> | ||
<field name="arch" type="xml"> | ||
<field name="attachment" position="after"> | ||
<field name="zip_download" attrs="{'invisible':[('report_type','not in',['qweb-pdf'])]}" /> | ||
</field> | ||
</field> | ||
</record> | ||
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<odoo> | ||
<template id="assets_backend" inherit_id="web.assets_backend"> | ||
<xpath expr="." position="inside"> | ||
<script type="text/javascript" src="/report_pdf_zip_download/static/src/js/action_manager_report.js"/> | ||
</xpath> | ||
</template> | ||
</odoo> |
1 change: 1 addition & 0 deletions
1
setup/report_pdf_zip_download/odoo/addons/report_pdf_zip_download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../report_pdf_zip_download |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import setuptools | ||
|
||
setuptools.setup( | ||
setup_requires=['setuptools-odoo'], | ||
odoo_addon=True, | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel that there must be a better way to handle zip downloads, but I don't have a concrete suggestion...