Skip to content

Commit

Permalink
[ADD] report_pdf_zip_download
Browse files Browse the repository at this point in the history
  • Loading branch information
AungKoKoLin1997 committed Oct 24, 2024
1 parent e685488 commit 94c2e6e
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 0 deletions.
2 changes: 2 additions & 0 deletions report_pdf_zip_download/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import controllers
from . import models
17 changes: 17 additions & 0 deletions report_pdf_zip_download/__manifest__.py
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,
}
1 change: 1 addition & 0 deletions report_pdf_zip_download/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import main
63 changes: 63 additions & 0 deletions report_pdf_zip_download/controllers/main.py
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
)
1 change: 1 addition & 0 deletions report_pdf_zip_download/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import ir_actions_report
13 changes: 13 additions & 0 deletions report_pdf_zip_download/models/ir_actions_report.py
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."
)
1 change: 1 addition & 0 deletions report_pdf_zip_download/readme/CONFIGURE.rst
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.
3 changes: 3 additions & 0 deletions report_pdf_zip_download/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Quartile <https://www.quartile.co>`__:

* Aung Ko Ko Lin
1 change: 1 addition & 0 deletions report_pdf_zip_download/readme/DESCRIPTION.rst
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 report_pdf_zip_download/static/src/js/action_manager_report.js
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);
}
});

});
13 changes: 13 additions & 0 deletions report_pdf_zip_download/views/ir_actions_report_views.xml
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>
8 changes: 8 additions & 0 deletions report_pdf_zip_download/views/web_client_templates.xml
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>
6 changes: 6 additions & 0 deletions setup/report_pdf_zip_download/setup.py
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,
)

0 comments on commit 94c2e6e

Please sign in to comment.