Browse Source

[ADD] report_qweb_encrypt

myc-14.0-py3o
Enric Tobella 4 years ago
committed by Kitti U
parent
commit
f3348c8d3d
  1. 80
      report_qweb_encrypt/README.rst
  2. 2
      report_qweb_encrypt/__init__.py
  3. 21
      report_qweb_encrypt/__manifest__.py
  4. 1
      report_qweb_encrypt/controllers/__init__.py
  5. 42
      report_qweb_encrypt/controllers/main.py
  6. 1
      report_qweb_encrypt/models/__init__.py
  7. 33
      report_qweb_encrypt/models/ir_actions_report.py
  8. 2
      report_qweb_encrypt/readme/CONTRIBUTORS.rst
  9. 2
      report_qweb_encrypt/readme/DESCRIPTION.rst
  10. 1
      report_qweb_encrypt/readme/USAGE.rst
  11. BIN
      report_qweb_encrypt/static/description/icon.png
  12. 80
      report_qweb_encrypt/static/src/js/report/action_manager_report.js
  13. 13
      report_qweb_encrypt/templates/assets.xml
  14. 20
      report_qweb_encrypt/views/ir_actions_report.xml

80
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 <https://github.com/OCA/reporting-engine/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 <https://github.com/OCA/reporting-engine/issues/new?body=module:%20report_qweb_encrypt%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
Do not contact contributors directly about support or help with technical issues.
Credits
=======
Authors
~~~~~~~
* Creu Blanca
Contributors
~~~~~~~~~~~~
* Enric Tobella <etobella@creublanca.es>
* Jaime Arroyo <jaime.arroyo@creublanca.es>
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 <https://github.com/OCA/reporting-engine/tree/12.0/report_qweb_encrypt>`_ project on GitHub.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

2
report_qweb_encrypt/__init__.py

@ -0,0 +1,2 @@
from . import controllers
from . import models

21
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': [
],
}

1
report_qweb_encrypt/controllers/__init__.py

@ -0,0 +1 @@
from . import main

42
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

1
report_qweb_encrypt/models/__init__.py

@ -0,0 +1 @@
from . import ir_actions_report

33
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

2
report_qweb_encrypt/readme/CONTRIBUTORS.rst

@ -0,0 +1,2 @@
* Enric Tobella <etobella@creublanca.es>
* Jaime Arroyo <jaime.arroyo@creublanca.es>

2
report_qweb_encrypt/readme/DESCRIPTION.rst

@ -0,0 +1,2 @@
This module allows you to encrypt pdf files with a password when
downloading them.

1
report_qweb_encrypt/readme/USAGE.rst

@ -0,0 +1 @@
To make a report encryptable mark the field `Encryptable` in the report record.

BIN
report_qweb_encrypt/static/description/icon.png

After

Width: 128  |  Height: 128  |  Size: 9.2 KiB

80
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: $('<div><input class="o_password" type="password"/></div>'),
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;
}
});
});

13
report_qweb_encrypt/templates/assets.xml

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<!--
(Copyright) 2020 Creu Blanca
License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
-->
<template id="assets_backend" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/report_qweb_encrypt/static/src/js/report/action_manager_report.js"/>
</xpath>
</template>
</odoo>

20
report_qweb_encrypt/views/ir_actions_report.xml

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2020 Creu Blanca
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<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="model">ir.actions.report</field>
<field name="inherit_id" ref="base.act_report_xml_view"/>
<field name="arch" type="xml">
<field name="paperformat_id" position="after">
<field name="encrypt"/>
</field>
</field>
</record>
</odoo>
Loading…
Cancel
Save