diff --git a/report_qweb_encrypt/__manifest__.py b/report_qweb_encrypt/__manifest__.py index 347f2012..afc2fc68 100644 --- a/report_qweb_encrypt/__manifest__.py +++ b/report_qweb_encrypt/__manifest__.py @@ -12,10 +12,7 @@ "depends": [ "web", ], - "data": [ - "views/ir_actions_report.xml", - "templates/assets.xml" - ], + "data": ["views/ir_actions_report.xml", "templates/assets.xml"], "installable": True, "maintainers": ["kittiu"], } diff --git a/report_qweb_encrypt/controllers/main.py b/report_qweb_encrypt/controllers/main.py index 4bbdceeb..01546835 100644 --- a/report_qweb_encrypt/controllers/main.py +++ b/report_qweb_encrypt/controllers/main.py @@ -1,11 +1,14 @@ # Copyright 2020 Creu Blanca # Copyright 2020 Ecosoft Co., Ltd. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo.addons.web.controllers import main as report -from odoo.http import route, request -from werkzeug.urls import url_decode import json +from werkzeug.urls import url_decode + +from odoo.http import request, route + +from odoo.addons.web.controllers import main as report + class ReportController(report.ReportController): @route() @@ -17,9 +20,9 @@ class ReportController(report.ReportController): requestcontent = json.loads(data) url, ttype = requestcontent[0], requestcontent[1] if ( - ttype in ["qweb-pdf"] and - result.headers["Content-Type"] == "application/pdf" and - "?" in url + ttype 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: @@ -28,6 +31,7 @@ class ReportController(report.ReportController): Report = request.env["ir.actions.report"] data = result.get_data() encrypted_data = Report._encrypt_pdf( - data, context["encrypt_password"]) + data, context["encrypt_password"] + ) result.set_data(encrypted_data) return result diff --git a/report_qweb_encrypt/models/ir_actions_report.py b/report_qweb_encrypt/models/ir_actions_report.py index 06921845..df8dec37 100644 --- a/report_qweb_encrypt/models/ir_actions_report.py +++ b/report_qweb_encrypt/models/ir_actions_report.py @@ -1,13 +1,13 @@ # Copyright 2020 Creu Blanca # Copyright 2020 Ecosoft Co., Ltd. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -import time import logging -from odoo import fields, models, _ +import time from io import BytesIO -from odoo.tools.safe_eval import safe_eval -from odoo.exceptions import ValidationError +from odoo import _, fields, models +from odoo.exceptions import ValidationError +from odoo.tools.safe_eval import safe_eval _logger = logging.getLogger(__name__) try: @@ -20,13 +20,12 @@ class IrActionsReport(models.Model): _inherit = "ir.actions.report" encrypt = fields.Selection( - [("manual", "Manual Input Password"), - ("auto", "Auto Generated Password")], + [("manual", "Manual Input Password"), ("auto", "Auto Generated Password")], string="Encryption", help="* Manual Input Password: allow user to key in password on the fly. " "This option available only on document print action.\n" "* Auto Generated Password: system will auto encrypt password when PDF " - "created, based on provided python syntax." + "created, based on provided python syntax.", ) encrypt_password = fields.Char( help="Python code syntax to gnerate password.", @@ -34,7 +33,8 @@ class IrActionsReport(models.Model): def render_qweb_pdf(self, res_ids=None, data=None): document, ttype = super(IrActionsReport, self).render_qweb_pdf( - res_ids=res_ids, data=data) + res_ids=res_ids, data=data + ) password = self._get_pdf_password(res_ids[:1]) document = self._encrypt_pdf(document, password) return document, ttype @@ -51,12 +51,14 @@ class IrActionsReport(models.Model): elif self.encrypt == "auto" and self.encrypt_password: obj = self.env[self.model].browse(res_id) try: - encrypt_password = safe_eval(self.encrypt_password, - {'object': obj, 'time': time}) + encrypt_password = safe_eval( + self.encrypt_password, {"object": obj, "time": time} + ) except: raise ValidationError( _("Python code used for encryption password is invalid.\n%s") - % self.encrypt_password) + % self.encrypt_password + ) return encrypt_password def _encrypt_pdf(self, data, password): 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 index 5e45630e..0c6b9c64 100644 --- a/report_qweb_encrypt/static/src/js/report/action_manager_report.js +++ b/report_qweb_encrypt/static/src/js/report/action_manager_report.js @@ -6,77 +6,91 @@ odoo.define("report_qweb_encrypt.Dialog", function (require) { var ActionManager = require("web.ActionManager"); var framework = require("web.framework"); var Dialog = require("web.Dialog"); - var core = require('web.core'); + var core = require("web.core"); var _t = core._t; var EncryptDialog = Dialog.extend({ events: _.extend({}, Dialog.prototype.events, { - change: '_onChange', + change: "_onChange", }), _setValue: function () { - this.value = this.$el.find('.o_password').val() + 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', + classes: "btn-primary", close: true, click: function (event) { var password = this.value || false; - owner._executeReportAction(action, action_options, password) + 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(); + 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 === 'manual' - && action.report_type === 'qweb-pdf' - && password === undefined) { + if ( + action.encrypt === "manual" && + action.report_type === "qweb-pdf" && + password === undefined + ) { EncryptDialog.askPassword(this, action, options); return $.Deferred(); - } - else if (action.encrypt === 'manual') { + } else if (action.encrypt === "manual") { action.context = _.extend({}, action.context, { encrypt_password: password, - }) + }); } - return this._super(action, options, password) + return this._super(action, options, password); }, _makeReportUrls: function (action) { var reportUrls = this._super.apply(this, arguments); - if (action.encrypt === 'manual' && 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; - }); + if (action.encrypt === "manual" && 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 index f3200b18..e313587f 100644 --- a/report_qweb_encrypt/templates/assets.xml +++ b/report_qweb_encrypt/templates/assets.xml @@ -6,7 +6,10 @@ -->