diff --git a/report_qweb_encrypt/README.rst b/report_qweb_encrypt/README.rst new file mode 100644 index 00000000..967084e6 --- /dev/null +++ b/report_qweb_encrypt/README.rst @@ -0,0 +1,80 @@ +=================== +Report Qweb Encrypt +=================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Freporting--engine-lightgray.png?logo=github + :target: https://github.com/OCA/reporting-engine/tree/12.0/report_qweb_encrypt + :alt: OCA/reporting-engine +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/reporting-engine-12-0/reporting-engine-12-0-report_qweb_encrypt + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/143/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows you to encrypt pdf files with a password when +downloading them. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To make a report encryptable mark the field `Encryptable` in the report record. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Creu Blanca + +Contributors +~~~~~~~~~~~~ + +* Enric Tobella +* Jaime Arroyo + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/reporting-engine `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/report_qweb_encrypt/__init__.py b/report_qweb_encrypt/__init__.py new file mode 100644 index 00000000..91c5580f --- /dev/null +++ b/report_qweb_encrypt/__init__.py @@ -0,0 +1,2 @@ +from . import controllers +from . import models diff --git a/report_qweb_encrypt/__manifest__.py b/report_qweb_encrypt/__manifest__.py new file mode 100644 index 00000000..13eb7269 --- /dev/null +++ b/report_qweb_encrypt/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2020 Creu Blanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + 'name': 'Report Qweb Encrypt', + 'summary': """ + Allow to encrypt qweb pdfs""", + 'version': '12.0.1.0.0', + 'license': 'AGPL-3', + 'author': 'Creu Blanca,Odoo Community Association (OCA)', + 'website': 'https://github.com/OCA/reporting-engine', + 'depends': [ + 'web', + ], + 'data': [ + 'views/ir_actions_report.xml', + 'templates/assets.xml' + ], + 'demo': [ + ], +} diff --git a/report_qweb_encrypt/controllers/__init__.py b/report_qweb_encrypt/controllers/__init__.py new file mode 100644 index 00000000..12a7e529 --- /dev/null +++ b/report_qweb_encrypt/controllers/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/report_qweb_encrypt/controllers/main.py b/report_qweb_encrypt/controllers/main.py new file mode 100644 index 00000000..0d1c8c56 --- /dev/null +++ b/report_qweb_encrypt/controllers/main.py @@ -0,0 +1,42 @@ +from odoo.addons.web.controllers import main as report +from odoo.http import route +from werkzeug.urls import url_decode +import json +import logging +from io import BytesIO + +_logger = logging.getLogger(__name__) +try: + from PyPDF2 import PdfFileReader, PdfFileWriter +except ImportError as err: + _logger.debug(err) + + +class ReportController(report.ReportController): + @route() + def report_download(self, data, token): + result = super().report_download(data, token) + requestcontent = json.loads(data) + url, type = requestcontent[0], requestcontent[1] + if ( + type in ['qweb-pdf'] and + result.headers['Content-Type'] == "application/pdf" and + '?' in url + ): + url_data = dict(url_decode(url.split('?')[1]).items()) + if 'context' in url_data: + context_data = json.loads(url_data['context']) + if 'encrypt_password' in context_data: + # We need to encrypt here because this function is not + # passing context, so we need to implement this again + + data = result.get_data() + output_pdf = PdfFileWriter() + in_buff = BytesIO(data) + pdf = PdfFileReader(in_buff) + output_pdf.appendPagesFromReader(pdf) + output_pdf.encrypt(context_data['encrypt_password']) + buff = BytesIO() + output_pdf.write(buff) + result.set_data(buff.getvalue()) + return result diff --git a/report_qweb_encrypt/models/__init__.py b/report_qweb_encrypt/models/__init__.py new file mode 100644 index 00000000..a248cf21 --- /dev/null +++ b/report_qweb_encrypt/models/__init__.py @@ -0,0 +1 @@ +from . import ir_actions_report diff --git a/report_qweb_encrypt/models/ir_actions_report.py b/report_qweb_encrypt/models/ir_actions_report.py new file mode 100644 index 00000000..93c2aaa3 --- /dev/null +++ b/report_qweb_encrypt/models/ir_actions_report.py @@ -0,0 +1,33 @@ +# Copyright 2020 Creu Blanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models +import logging +from io import BytesIO + +_logger = logging.getLogger(__name__) +try: + from PyPDF2 import PdfFileReader, PdfFileWriter +except ImportError as err: + _logger.debug(err) + + +class IrActionsReport(models.Model): + + _inherit = 'ir.actions.report' + + encrypt = fields.Boolean() + + def render_qweb_pdf(self, res_ids=None, data=None): + document, type = super(IrActionsReport, self).render_qweb_pdf( + res_ids=res_ids, data=data) + if self.encrypt and self.env.context.get('encrypt_password', False): + output_pdf = PdfFileWriter() + in_buff = BytesIO(document) + pdf = PdfFileReader(in_buff) + output_pdf.appendPagesFromReader(pdf) + output_pdf.encrypt(self.env.context.get('encrypt_password')) + buff = BytesIO() + output_pdf.write(buff) + document = buff.getvalue() + return document, type diff --git a/report_qweb_encrypt/readme/CONTRIBUTORS.rst b/report_qweb_encrypt/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..94e7b0a4 --- /dev/null +++ b/report_qweb_encrypt/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Enric Tobella +* Jaime Arroyo diff --git a/report_qweb_encrypt/readme/DESCRIPTION.rst b/report_qweb_encrypt/readme/DESCRIPTION.rst new file mode 100644 index 00000000..f111d795 --- /dev/null +++ b/report_qweb_encrypt/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module allows you to encrypt pdf files with a password when +downloading them. diff --git a/report_qweb_encrypt/readme/USAGE.rst b/report_qweb_encrypt/readme/USAGE.rst new file mode 100644 index 00000000..aed8cb93 --- /dev/null +++ b/report_qweb_encrypt/readme/USAGE.rst @@ -0,0 +1 @@ +To make a report encryptable mark the field `Encryptable` in the report record. diff --git a/report_qweb_encrypt/static/description/icon.png b/report_qweb_encrypt/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/report_qweb_encrypt/static/description/icon.png differ diff --git a/report_qweb_encrypt/static/src/js/report/action_manager_report.js b/report_qweb_encrypt/static/src/js/report/action_manager_report.js new file mode 100644 index 00000000..e70ae09b --- /dev/null +++ b/report_qweb_encrypt/static/src/js/report/action_manager_report.js @@ -0,0 +1,80 @@ +// © 2017 Creu Blanca +// License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html). +odoo.define("report_qweb_encrypt.Dialog", function (require) { + "use strict"; + + var ActionManager = require("web.ActionManager"); + var framework = require("web.framework"); + var Dialog = require("web.Dialog"); + var core = require('web.core'); + + var _t = core._t; + + var EncryptDialog = Dialog.extend({ + events: _.extend({} , Dialog.prototype.events, { + change: '_onChange', + }), + _setValue: function () { + this.value = this.$el.find('.o_password').val() + }, + _onChange: function (event) { + this._setValue(); + }, + + }) + EncryptDialog.askPassword = function (owner, action, action_options, options) { + var buttons = [ + { + text: _t("Ok"), + classes: 'btn-primary', + close: true, + click: function (event) { + var password = this.value || false; + owner._executeReportAction(action, action_options, password) + }, + }, + { + text: _t("Cancel"), + close: true, + click: false, + } + ]; + return new EncryptDialog(owner, _.extend({ + size: 'small', + buttons: buttons, + $content: $('
'), + title: _t("Encrypt"), + }, options)).open(); + }; + + ActionManager.include({ + _executeReportAction: function (action, options, password) { + if (action.encrypt && password === undefined) { + EncryptDialog.askPassword(this, action, options); + return $.Deferred() + } + else if (action.encrypt) { + action.context = _.extend({}, action.context, { + encrypt_password: password, + }) + } + return this._super(action, options, password) + }, + _makeReportUrls: function (action) { + var reportUrls = this._super.apply(this, arguments); + if (action.encrypt && action.context.encrypt_password) { + if (_.isUndefined(action.data) || _.isNull(action.data) || + (_.isObject(action.data) && _.isEmpty(action.data))) { + var serializedOptionsPath = '?context=' + encodeURIComponent(JSON.stringify({ + encrypt_password: action.context.encrypt_password, + })); + reportUrls = _.mapObject(reportUrls, function (value) { + return value += serializedOptionsPath; + }); + } + } + return reportUrls; + } + }); + +}); diff --git a/report_qweb_encrypt/templates/assets.xml b/report_qweb_encrypt/templates/assets.xml new file mode 100644 index 00000000..f3200b18 --- /dev/null +++ b/report_qweb_encrypt/templates/assets.xml @@ -0,0 +1,13 @@ + + + +