Browse Source

[IMP] : black, isort, prettier

myc-14.0-py3o
Kitti U 3 years ago
parent
commit
01f5f982be
  1. 5
      report_qweb_encrypt/__manifest__.py
  2. 18
      report_qweb_encrypt/controllers/main.py
  3. 24
      report_qweb_encrypt/models/ir_actions_report.py
  4. 78
      report_qweb_encrypt/static/src/js/report/action_manager_report.js
  5. 5
      report_qweb_encrypt/templates/assets.xml
  6. 1
      report_qweb_encrypt/tests/test_report_qweb_encrypt.py
  7. 23
      report_qweb_encrypt/views/ir_actions_report.xml
  8. 1
      setup/report_qweb_encrypt/odoo/addons/report_qweb_encrypt
  9. 6
      setup/report_qweb_encrypt/setup.py

5
report_qweb_encrypt/__manifest__.py

@ -12,10 +12,7 @@
"depends": [ "depends": [
"web", "web",
], ],
"data": [
"views/ir_actions_report.xml",
"templates/assets.xml"
],
"data": ["views/ir_actions_report.xml", "templates/assets.xml"],
"installable": True, "installable": True,
"maintainers": ["kittiu"], "maintainers": ["kittiu"],
} }

18
report_qweb_encrypt/controllers/main.py

@ -1,11 +1,14 @@
# Copyright 2020 Creu Blanca # Copyright 2020 Creu Blanca
# Copyright 2020 Ecosoft Co., Ltd. # Copyright 2020 Ecosoft Co., Ltd.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # 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 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): class ReportController(report.ReportController):
@route() @route()
@ -17,9 +20,9 @@ class ReportController(report.ReportController):
requestcontent = json.loads(data) requestcontent = json.loads(data)
url, ttype = requestcontent[0], requestcontent[1] url, ttype = requestcontent[0], requestcontent[1]
if ( 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()) url_data = dict(url_decode(url.split("?")[1]).items())
if "context" in url_data: if "context" in url_data:
@ -28,6 +31,7 @@ class ReportController(report.ReportController):
Report = request.env["ir.actions.report"] Report = request.env["ir.actions.report"]
data = result.get_data() data = result.get_data()
encrypted_data = Report._encrypt_pdf( encrypted_data = Report._encrypt_pdf(
data, context["encrypt_password"])
data, context["encrypt_password"]
)
result.set_data(encrypted_data) result.set_data(encrypted_data)
return result return result

24
report_qweb_encrypt/models/ir_actions_report.py

@ -1,13 +1,13 @@
# Copyright 2020 Creu Blanca # Copyright 2020 Creu Blanca
# Copyright 2020 Ecosoft Co., Ltd. # Copyright 2020 Ecosoft Co., Ltd.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import time
import logging import logging
from odoo import fields, models, _
import time
from io import BytesIO 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__) _logger = logging.getLogger(__name__)
try: try:
@ -20,13 +20,12 @@ class IrActionsReport(models.Model):
_inherit = "ir.actions.report" _inherit = "ir.actions.report"
encrypt = fields.Selection( encrypt = fields.Selection(
[("manual", "Manual Input Password"),
("auto", "Auto Generated Password")],
[("manual", "Manual Input Password"), ("auto", "Auto Generated Password")],
string="Encryption", string="Encryption",
help="* Manual Input Password: allow user to key in password on the fly. " help="* Manual Input Password: allow user to key in password on the fly. "
"This option available only on document print action.\n" "This option available only on document print action.\n"
"* Auto Generated Password: system will auto encrypt password when PDF " "* 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( encrypt_password = fields.Char(
help="Python code syntax to gnerate password.", 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): def render_qweb_pdf(self, res_ids=None, data=None):
document, ttype = super(IrActionsReport, self).render_qweb_pdf( 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]) password = self._get_pdf_password(res_ids[:1])
document = self._encrypt_pdf(document, password) document = self._encrypt_pdf(document, password)
return document, ttype return document, ttype
@ -51,12 +51,14 @@ class IrActionsReport(models.Model):
elif self.encrypt == "auto" and self.encrypt_password: elif self.encrypt == "auto" and self.encrypt_password:
obj = self.env[self.model].browse(res_id) obj = self.env[self.model].browse(res_id)
try: try:
encrypt_password = safe_eval(self.encrypt_password,
{'object': obj, 'time': time})
encrypt_password = safe_eval(
self.encrypt_password, {"object": obj, "time": time}
)
except: except:
raise ValidationError( raise ValidationError(
_("Python code used for encryption password is invalid.\n%s") _("Python code used for encryption password is invalid.\n%s")
% self.encrypt_password)
% self.encrypt_password
)
return encrypt_password return encrypt_password
def _encrypt_pdf(self, data, password): def _encrypt_pdf(self, data, password):

78
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 ActionManager = require("web.ActionManager");
var framework = require("web.framework"); var framework = require("web.framework");
var Dialog = require("web.Dialog"); var Dialog = require("web.Dialog");
var core = require('web.core');
var core = require("web.core");
var _t = core._t; var _t = core._t;
var EncryptDialog = Dialog.extend({ var EncryptDialog = Dialog.extend({
events: _.extend({}, Dialog.prototype.events, { events: _.extend({}, Dialog.prototype.events, {
change: '_onChange',
change: "_onChange",
}), }),
_setValue: function () { _setValue: function () {
this.value = this.$el.find('.o_password').val()
this.value = this.$el.find(".o_password").val();
}, },
_onChange: function (event) { _onChange: function (event) {
this._setValue(); this._setValue();
}, },
})
});
EncryptDialog.askPassword = function (owner, action, action_options, options) { EncryptDialog.askPassword = function (owner, action, action_options, options) {
var buttons = [ var buttons = [
{ {
text: _t("Ok"), text: _t("Ok"),
classes: 'btn-primary',
classes: "btn-primary",
close: true, close: true,
click: function (event) { click: function (event) {
var password = this.value || false; var password = this.value || false;
owner._executeReportAction(action, action_options, password)
owner._executeReportAction(action, action_options, password);
}, },
}, },
{ {
text: _t("Cancel"), text: _t("Cancel"),
close: true, close: true,
click: false, click: false,
}
},
]; ];
return new EncryptDialog(owner, _.extend({
size: 'small',
buttons: buttons,
$content: $('<div><input class="o_password" type="password"/></div>'),
title: _t("Encrypt"),
}, options)).open();
return new EncryptDialog(
owner,
_.extend(
{
size: "small",
buttons: buttons,
$content: $(
'<div><input class="o_password" type="password"/></div>'
),
title: _t("Encrypt"),
},
options
)
).open();
}; };
ActionManager.include({ ActionManager.include({
_executeReportAction: function (action, options, password) { _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); EncryptDialog.askPassword(this, action, options);
return $.Deferred(); return $.Deferred();
}
else if (action.encrypt === 'manual') {
} else if (action.encrypt === "manual") {
action.context = _.extend({}, action.context, { action.context = _.extend({}, action.context, {
encrypt_password: password, encrypt_password: password,
})
});
} }
return this._super(action, options, password)
return this._super(action, options, password);
}, },
_makeReportUrls: function (action) { _makeReportUrls: function (action) {
var reportUrls = this._super.apply(this, arguments); 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; return reportUrls;
}
},
}); });
}); });

5
report_qweb_encrypt/templates/assets.xml

@ -6,7 +6,10 @@
--> -->
<template id="assets_backend" inherit_id="web.assets_backend"> <template id="assets_backend" inherit_id="web.assets_backend">
<xpath expr="." position="inside"> <xpath expr="." position="inside">
<script type="text/javascript" src="/report_qweb_encrypt/static/src/js/report/action_manager_report.js"/>
<script
type="text/javascript"
src="/report_qweb_encrypt/static/src/js/report/action_manager_report.js"
/>
</xpath> </xpath>
</template> </template>

1
report_qweb_encrypt/tests/test_report_qweb_encrypt.py

@ -5,7 +5,6 @@ from odoo.tests.common import HttpCase
class TestReportQwebEncrypt(HttpCase): class TestReportQwebEncrypt(HttpCase):
def test_report_qweb_no_encrypt(self): def test_report_qweb_no_encrypt(self):
ctx = {"force_report_rendering": True} ctx = {"force_report_rendering": True}
report = self.env.ref("web.action_report_internalpreview") report = self.env.ref("web.action_report_internalpreview")

23
report_qweb_encrypt/views/ir_actions_report.xml

@ -1,22 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2020 Creu Blanca <!-- Copyright 2020 Creu Blanca
Copyright 2020 Ecosoft Co., LTd. Copyright 2020 Ecosoft Co., LTd.
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo> <odoo>
<record model="ir.ui.view" id="ir_actions_report_form_view"> <record model="ir.ui.view" id="ir_actions_report_form_view">
<field name="name">ir.actions.report.form (in report_qweb_encrypt)</field> <field name="name">ir.actions.report.form (in report_qweb_encrypt)</field>
<field name="model">ir.actions.report</field> <field name="model">ir.actions.report</field>
<field name="inherit_id" ref="base.act_report_xml_view"/>
<field name="inherit_id" ref="base.act_report_xml_view" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="paperformat_id" position="after"> <field name="paperformat_id" position="after">
<label for="encrypt" attrs="{'invisible': [('report_type', 'not in', ('qweb-pdf', 'qweb-html'))]}"/>
<div name="encrypt" attrs="{'invisible': [('report_type', 'not in', ('qweb-pdf', 'qweb-html'))]}">
<field name="encrypt"/>
<field name="encrypt_password"
<label
for="encrypt"
attrs="{'invisible': [('report_type', 'not in', ('qweb-pdf', 'qweb-html'))]}"
/>
<div
name="encrypt"
attrs="{'invisible': [('report_type', 'not in', ('qweb-pdf', 'qweb-html'))]}"
>
<field name="encrypt" />
<field
name="encrypt_password"
attrs="{'invisible': [('encrypt', '!=', 'auto')]}" attrs="{'invisible': [('encrypt', '!=', 'auto')]}"
placeholder="python syntax, i.e., (object.default_code or 'secretcode')"/>
placeholder="python syntax, i.e., (object.default_code or 'secretcode')"
/>
</div> </div>
</field> </field>
</field> </field>

1
setup/report_qweb_encrypt/odoo/addons/report_qweb_encrypt

@ -0,0 +1 @@
../../../../report_qweb_encrypt

6
setup/report_qweb_encrypt/setup.py

@ -0,0 +1,6 @@
import setuptools
setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
Loading…
Cancel
Save