mirror of
https://github.com/OCA/reporting-engine.git
synced 2025-02-16 16:30:38 +02:00
[IMP] report_fillpdf: black, isort, prettier
This commit is contained in:
@@ -1,32 +1,31 @@
|
|||||||
# Copyright 2017 Creu Blanca
|
# Copyright 2017 Creu Blanca
|
||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||||
{
|
{
|
||||||
'name': "Base report PDF Filler",
|
"name": "Base report PDF Filler",
|
||||||
|
"summary": """
|
||||||
'summary': """
|
|
||||||
Base module that fills PDFs""",
|
Base module that fills PDFs""",
|
||||||
'author': 'Creu Blanca,'
|
"author": "Creu Blanca," "Odoo Community Association (OCA)",
|
||||||
'Odoo Community Association (OCA)',
|
"website": "https://github.com/OCA/reporting-engine",
|
||||||
'website': "http://github.com/oca/reporting-engine",
|
"category": "Reporting",
|
||||||
'category': 'Reporting',
|
"version": "11.0.1.0.1",
|
||||||
'version': '11.0.1.0.1',
|
"license": "AGPL-3",
|
||||||
'license': 'AGPL-3',
|
"external_dependencies": {
|
||||||
'external_dependencies': {
|
"python": [
|
||||||
'python': [
|
"fdfgen",
|
||||||
'fdfgen',
|
],
|
||||||
|
"bin": [
|
||||||
|
"pdftk",
|
||||||
],
|
],
|
||||||
'bin': [
|
|
||||||
'pdftk',
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
'depends': [
|
"depends": [
|
||||||
'base', 'web',
|
"base",
|
||||||
|
"web",
|
||||||
],
|
],
|
||||||
'data': [
|
"data": [
|
||||||
'views/webclient_templates.xml',
|
"views/webclient_templates.xml",
|
||||||
],
|
],
|
||||||
'demo': [
|
"demo": [
|
||||||
'demo/report.xml',
|
"demo/report.xml",
|
||||||
],
|
],
|
||||||
'installable': True,
|
"installable": True,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,41 +1,39 @@
|
|||||||
# Copyright (C) 2017 Creu Blanca
|
# Copyright (C) 2017 Creu Blanca
|
||||||
# License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
|
# License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
|
||||||
|
|
||||||
from odoo.addons.web.controllers import main as report
|
|
||||||
from odoo.http import content_disposition, route, request
|
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from odoo.http import content_disposition, request, route
|
||||||
|
|
||||||
|
from odoo.addons.web.controllers import main as report
|
||||||
|
|
||||||
|
|
||||||
class ReportController(report.ReportController):
|
class ReportController(report.ReportController):
|
||||||
@route()
|
@route()
|
||||||
def report_routes(self, reportname, docids=None, converter=None, **data):
|
def report_routes(self, reportname, docids=None, converter=None, **data):
|
||||||
if converter == 'fillpdf':
|
if converter == "fillpdf":
|
||||||
report = request.env['ir.actions.report']._get_report_from_name(
|
report = request.env["ir.actions.report"]._get_report_from_name(reportname)
|
||||||
reportname)
|
|
||||||
context = dict(request.env.context)
|
context = dict(request.env.context)
|
||||||
if docids:
|
if docids:
|
||||||
docids = [int(i) for i in docids.split(',')]
|
docids = [int(i) for i in docids.split(",")]
|
||||||
if data.get('options'):
|
if data.get("options"):
|
||||||
data.update(json.loads(data.pop('options')))
|
data.update(json.loads(data.pop("options")))
|
||||||
if data.get('context'):
|
if data.get("context"):
|
||||||
# Ignore 'lang' here, because the context in data is the one
|
# Ignore 'lang' here, because the context in data is the one
|
||||||
# from the webclient *but* if the user explicitely wants to
|
# from the webclient *but* if the user explicitely wants to
|
||||||
# change the lang, this mechanism overwrites it.
|
# change the lang, this mechanism overwrites it.
|
||||||
data['context'] = json.loads(data['context'])
|
data["context"] = json.loads(data["context"])
|
||||||
if data['context'].get('lang'):
|
if data["context"].get("lang"):
|
||||||
del data['context']['lang']
|
del data["context"]["lang"]
|
||||||
context.update(data['context'])
|
context.update(data["context"])
|
||||||
pdf = report.with_context(context).render_fillpdf(
|
pdf = report.with_context(context).render_fillpdf(docids, data=data)[0]
|
||||||
docids, data=data
|
|
||||||
)[0]
|
|
||||||
pdfhttpheaders = [
|
pdfhttpheaders = [
|
||||||
('Content-Type', 'application/pdf'),
|
("Content-Type", "application/pdf"),
|
||||||
('Content-Length', len(pdf)),
|
("Content-Length", len(pdf)),
|
||||||
(
|
(
|
||||||
'Content-Disposition',
|
"Content-Disposition",
|
||||||
content_disposition(report.report_file + '.pdf')
|
content_disposition(report.report_file + ".pdf"),
|
||||||
)
|
),
|
||||||
]
|
]
|
||||||
return request.make_response(pdf, headers=pdfhttpheaders)
|
return request.make_response(pdf, headers=pdfhttpheaders)
|
||||||
return super(ReportController, self).report_routes(
|
return super(ReportController, self).report_routes(
|
||||||
|
|||||||
@@ -1,33 +1,35 @@
|
|||||||
# Copyright 2017 Creu Blanca
|
# Copyright 2017 Creu Blanca
|
||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||||
|
|
||||||
from odoo import api, fields, models, _
|
from odoo import _, api, fields, models
|
||||||
from odoo.exceptions import UserError
|
from odoo.exceptions import UserError
|
||||||
|
|
||||||
|
|
||||||
class ReportAction(models.Model):
|
class ReportAction(models.Model):
|
||||||
_inherit = 'ir.actions.report'
|
_inherit = "ir.actions.report"
|
||||||
|
|
||||||
report_type = fields.Selection(selection_add=[("fillpdf", "PDF Filler")])
|
report_type = fields.Selection(selection_add=[("fillpdf", "PDF Filler")])
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def render_fillpdf(self, docids, data):
|
def render_fillpdf(self, docids, data):
|
||||||
report_model_name = 'report.%s' % self.report_name
|
report_model_name = "report.%s" % self.report_name
|
||||||
report_model = self.env.get(report_model_name)
|
report_model = self.env.get(report_model_name)
|
||||||
if report_model is None:
|
if report_model is None:
|
||||||
raise UserError(_('%s model was not found' % report_model_name))
|
raise UserError(_("%s model was not found" % report_model_name))
|
||||||
return report_model.with_context({
|
return report_model.with_context({"active_model": self.model}).fill_report(
|
||||||
'active_model': self.model
|
docids, data
|
||||||
}).fill_report(docids, data)
|
)
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def _get_report_from_name(self, report_name):
|
def _get_report_from_name(self, report_name):
|
||||||
res = super(ReportAction, self)._get_report_from_name(report_name)
|
res = super(ReportAction, self)._get_report_from_name(report_name)
|
||||||
if res:
|
if res:
|
||||||
return res
|
return res
|
||||||
report_obj = self.env['ir.actions.report']
|
report_obj = self.env["ir.actions.report"]
|
||||||
qwebtypes = ['fillpdf']
|
qwebtypes = ["fillpdf"]
|
||||||
conditions = [('report_type', 'in', qwebtypes),
|
conditions = [
|
||||||
('report_name', '=', report_name)]
|
("report_type", "in", qwebtypes),
|
||||||
context = self.env['res.users'].context_get()
|
("report_name", "=", report_name),
|
||||||
|
]
|
||||||
|
context = self.env["res.users"].context_get()
|
||||||
return report_obj.with_context(context).search(conditions, limit=1)
|
return report_obj.with_context(context).search(conditions, limit=1)
|
||||||
|
|||||||
@@ -2,31 +2,36 @@
|
|||||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||||
|
|
||||||
|
|
||||||
from io import BytesIO
|
|
||||||
import os
|
|
||||||
from contextlib import closing
|
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
from subprocess import Popen, PIPE
|
from contextlib import closing
|
||||||
|
from io import BytesIO
|
||||||
|
from subprocess import PIPE, Popen
|
||||||
|
|
||||||
from odoo import api, models, tools
|
from odoo import api, models, tools
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
try:
|
try:
|
||||||
from fdfgen import forge_fdf
|
from fdfgen import forge_fdf
|
||||||
EXTERNAL_DEPENDENCY_BINARY_PDFTK = tools.find_in_path('pdftk')
|
|
||||||
|
EXTERNAL_DEPENDENCY_BINARY_PDFTK = tools.find_in_path("pdftk")
|
||||||
except (ImportError, IOError) as err:
|
except (ImportError, IOError) as err:
|
||||||
_logger.debug('Error while importing: %s.' % err)
|
_logger.debug("Error while importing: %s." % err)
|
||||||
EXTERNAL_DEPENDENCY_BINARY_PDFTK = ""
|
EXTERNAL_DEPENDENCY_BINARY_PDFTK = ""
|
||||||
|
|
||||||
|
|
||||||
class ReportFillPDFAbstract(models.AbstractModel):
|
class ReportFillPDFAbstract(models.AbstractModel):
|
||||||
_name = 'report.report_fillpdf.abstract'
|
_name = "report.report_fillpdf.abstract"
|
||||||
|
|
||||||
def fill_report(self, docids, data):
|
def fill_report(self, docids, data):
|
||||||
objs = self.env[self.env.context.get('active_model')].browse(docids)
|
objs = self.env[self.env.context.get("active_model")].browse(docids)
|
||||||
return self.fill_pdf_form(
|
return (
|
||||||
self.get_form(data, objs),
|
self.fill_pdf_form(
|
||||||
self.get_document_values(data, objs)), 'pdf'
|
self.get_form(data, objs), self.get_document_values(data, objs)
|
||||||
|
),
|
||||||
|
"pdf",
|
||||||
|
)
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def get_original_document_path(self, data, objs):
|
def get_original_document_path(self, data, objs):
|
||||||
@@ -34,7 +39,7 @@ class ReportFillPDFAbstract(models.AbstractModel):
|
|||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def get_form(self, data, objs):
|
def get_form(self, data, objs):
|
||||||
with open(self.get_original_document_path(data, objs), 'rb') as file:
|
with open(self.get_original_document_path(data, objs), "rb") as file:
|
||||||
result = file.read()
|
result = file.read()
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@@ -45,17 +50,18 @@ class ReportFillPDFAbstract(models.AbstractModel):
|
|||||||
@api.model
|
@api.model
|
||||||
def fill_pdf_form(self, form, vals):
|
def fill_pdf_form(self, form, vals):
|
||||||
fdf = forge_fdf("", vals.items(), [], [], [])
|
fdf = forge_fdf("", vals.items(), [], [], [])
|
||||||
document_fd, document_path = tempfile.mkstemp(
|
document_fd, document_path = tempfile.mkstemp(suffix=".pdf", prefix="")
|
||||||
suffix='.pdf', prefix='')
|
with closing(os.fdopen(document_fd, "wb")) as body_file:
|
||||||
with closing(os.fdopen(document_fd, 'wb')) as body_file:
|
|
||||||
body_file.write(form)
|
body_file.write(form)
|
||||||
args = [
|
args = [
|
||||||
EXTERNAL_DEPENDENCY_BINARY_PDFTK,
|
EXTERNAL_DEPENDENCY_BINARY_PDFTK,
|
||||||
document_path,
|
document_path,
|
||||||
"fill_form", "-",
|
"fill_form",
|
||||||
"output", "-",
|
"-",
|
||||||
|
"output",
|
||||||
|
"-",
|
||||||
"dont_ask",
|
"dont_ask",
|
||||||
"flatten"
|
"flatten",
|
||||||
]
|
]
|
||||||
p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
||||||
stdout, stderr = p.communicate(fdf)
|
stdout, stderr = p.communicate(fdf)
|
||||||
|
|||||||
@@ -6,15 +6,14 @@ from odoo.modules import get_resource_path
|
|||||||
|
|
||||||
|
|
||||||
class PartnerPDF(models.AbstractModel):
|
class PartnerPDF(models.AbstractModel):
|
||||||
_name = 'report.report_fillpdf.partner_fillpdf'
|
_name = "report.report_fillpdf.partner_fillpdf"
|
||||||
_inherit = 'report.report_fillpdf.abstract'
|
_inherit = "report.report_fillpdf.abstract"
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def get_original_document_path(self, data, objs):
|
def get_original_document_path(self, data, objs):
|
||||||
return get_resource_path(
|
return get_resource_path("report_fillpdf", "static/src/pdf", "partner_pdf.pdf")
|
||||||
'report_fillpdf', 'static/src/pdf', 'partner_pdf.pdf')
|
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def get_document_values(self, data, objs):
|
def get_document_values(self, data, objs):
|
||||||
objs.ensure_one()
|
objs.ensure_one()
|
||||||
return {'name': objs.name}
|
return {"name": objs.name}
|
||||||
|
|||||||
@@ -1,42 +1,49 @@
|
|||||||
// © 2017 Creu Blanca
|
// © 2017 Creu Blanca
|
||||||
// License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
|
// License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
|
||||||
odoo.define('report_fillpdf.report', function(require){
|
odoo.define("report_fillpdf.report", function (require) {
|
||||||
'use strict';
|
"use strict";
|
||||||
|
|
||||||
var ActionManager= require('web.ActionManager');
|
var ActionManager = require("web.ActionManager");
|
||||||
var crash_manager = require('web.crash_manager');
|
var crash_manager = require("web.crash_manager");
|
||||||
var framework = require('web.framework');
|
var framework = require("web.framework");
|
||||||
|
|
||||||
ActionManager.include({
|
ActionManager.include({
|
||||||
ir_actions_report: function (action, options){
|
ir_actions_report: function (action, options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var cloned_action = _.clone(action);
|
var cloned_action = _.clone(action);
|
||||||
if (cloned_action.report_type === 'fillpdf') {
|
if (cloned_action.report_type === "fillpdf") {
|
||||||
framework.blockUI();
|
framework.blockUI();
|
||||||
var report_fillpdf_url = 'report/fillpdf/' + cloned_action.report_name;
|
var report_fillpdf_url = "report/fillpdf/" + cloned_action.report_name;
|
||||||
if(cloned_action.context.active_ids){
|
if (cloned_action.context.active_ids) {
|
||||||
report_fillpdf_url += '/' + cloned_action.context.active_ids.join(',');
|
report_fillpdf_url +=
|
||||||
}else{
|
"/" + cloned_action.context.active_ids.join(",");
|
||||||
report_fillpdf_url += '?options=' + encodeURIComponent(JSON.stringify(cloned_action.data));
|
} else {
|
||||||
report_fillpdf_url += '&context=' + encodeURIComponent(JSON.stringify(cloned_action.context));
|
report_fillpdf_url +=
|
||||||
}
|
"?options=" +
|
||||||
self.getSession().get_file({
|
encodeURIComponent(JSON.stringify(cloned_action.data));
|
||||||
url: report_fillpdf_url,
|
report_fillpdf_url +=
|
||||||
data: {data: JSON.stringify([
|
"&context=" +
|
||||||
report_fillpdf_url,
|
encodeURIComponent(JSON.stringify(cloned_action.context));
|
||||||
cloned_action.report_type
|
|
||||||
])},
|
|
||||||
error: crash_manager.rpc_error.bind(crash_manager),
|
|
||||||
success: function (){
|
|
||||||
if(cloned_action && options && !cloned_action.dialog){
|
|
||||||
options.on_close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
self.getSession().get_file({
|
||||||
framework.unblockUI();
|
url: report_fillpdf_url,
|
||||||
return;
|
data: {
|
||||||
}
|
data: JSON.stringify([
|
||||||
return self._super(action, options);
|
report_fillpdf_url,
|
||||||
}
|
cloned_action.report_type,
|
||||||
});
|
]),
|
||||||
|
},
|
||||||
|
error: crash_manager.rpc_error.bind(crash_manager),
|
||||||
|
success: function () {
|
||||||
|
if (cloned_action && options && !cloned_action.dialog) {
|
||||||
|
options.on_close();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
framework.unblockUI();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return self._super(action, options);
|
||||||
|
},
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ from odoo.tests import common
|
|||||||
|
|
||||||
class TestReport(common.TransactionCase):
|
class TestReport(common.TransactionCase):
|
||||||
def test_report(self):
|
def test_report(self):
|
||||||
report_object = self.env['ir.actions.report']
|
report_object = self.env["ir.actions.report"]
|
||||||
report_name = 'report_fillpdf.partner_fillpdf'
|
report_name = "report_fillpdf.partner_fillpdf"
|
||||||
report = report_object._get_report_from_name(report_name)
|
report = report_object._get_report_from_name(report_name)
|
||||||
docs = self.env['res.company'].search([], limit=1).partner_id
|
docs = self.env["res.company"].search([], limit=1).partner_id
|
||||||
self.assertEqual(report.report_type, 'fillpdf')
|
self.assertEqual(report.report_type, "fillpdf")
|
||||||
report.render(docs.ids, {})
|
report.render(docs.ids, {})
|
||||||
|
|||||||
@@ -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_fillpdf/static/src/js/report/qwebactionmanager.js"/>
|
<script
|
||||||
|
type="text/javascript"
|
||||||
|
src="/report_fillpdf/static/src/js/report/qwebactionmanager.js"
|
||||||
|
/>
|
||||||
</xpath>
|
</xpath>
|
||||||
|
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
# generated from manifests external_dependencies
|
# generated from manifests external_dependencies
|
||||||
cryptography
|
cryptography
|
||||||
endesive
|
endesive
|
||||||
|
fdfgen
|
||||||
py3o.formats
|
py3o.formats
|
||||||
py3o.template
|
py3o.template
|
||||||
|
|||||||
1
setup/report_fillpdf/odoo/addons/report_fillpdf
Symbolic link
1
setup/report_fillpdf/odoo/addons/report_fillpdf
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
../../../../report_fillpdf
|
||||||
2
setup/report_fillpdf/setup.cfg
Normal file
2
setup/report_fillpdf/setup.cfg
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
[bdist_wheel]
|
||||||
|
universal=1
|
||||||
6
setup/report_fillpdf/setup.py
Normal file
6
setup/report_fillpdf/setup.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import setuptools
|
||||||
|
|
||||||
|
setuptools.setup(
|
||||||
|
setup_requires=['setuptools-odoo'],
|
||||||
|
odoo_addon=True,
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user