[ENH] Add option to auto encrypt password based on python syntax

This commit is contained in:
Kitti U
2020-12-26 15:06:58 +07:00
parent f3348c8d3d
commit f1af8c100e
10 changed files with 143 additions and 69 deletions

View File

@@ -1,42 +1,33 @@
# 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
from odoo.http import route, request
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)
# When report is downloaded from print action, this function is called,
# but this function cannot pass context (manually entered password) to
# report.render_qweb_pdf(), encrypton for manual password is done here.
requestcontent = json.loads(data)
url, type = requestcontent[0], requestcontent[1]
url, ttype = requestcontent[0], requestcontent[1]
if (
type 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:
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
url_data = dict(url_decode(url.split("?")[1]).items())
if "context" in url_data:
context = json.loads(url_data["context"])
if "encrypt_password" in context:
Report = request.env["ir.actions.report"]
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())
encrypted_data = Report._encrypt_pdf(
data, context["encrypt_password"])
result.set_data(encrypted_data)
return result