mirror of
https://github.com/OCA/report-print-send.git
synced 2025-02-16 07:11:31 +02:00
@@ -82,7 +82,8 @@ The jobs will be sent to the printer with a name matching the print_report_name
|
||||
of the report (truncated at 80 characters). By default this will not be
|
||||
displayed by CUPS web interface or in Odoo. To see this information, you need
|
||||
to change the configuration of your CUPS server and set the JobPrivateValue
|
||||
directive to "job-name", and reload the server. See `cupsd.conf(5)
|
||||
directive to "none" (or some other list of values which does not include
|
||||
"job-name") , and reload the server. See `cupsd.conf(5)
|
||||
<https://www.cups.org/doc/man-cupsd.conf.html>` for details.
|
||||
|
||||
Usage
|
||||
@@ -155,6 +156,7 @@ Contributors
|
||||
* Rod Schouteden <rod@schout-it.be>
|
||||
* Alexandre Fayolle <alexandre.fayolle@camptocamp.com>
|
||||
* Matias Peralta <mnp@adhoc.com.ar>
|
||||
* Akim Juillerat <akim.juillerat@camptocamp.com>
|
||||
|
||||
Maintainers
|
||||
~~~~~~~~~~~
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
"views/printing_report.xml",
|
||||
"views/res_users.xml",
|
||||
"views/ir_actions_report.xml",
|
||||
"wizards/print_attachment_report.xml",
|
||||
"wizards/printing_printer_update_wizard_view.xml",
|
||||
],
|
||||
"assets": {
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
from odoo import _, api, exceptions, fields, models
|
||||
from odoo.tools.safe_eval import safe_eval, time
|
||||
|
||||
REPORT_TYPES = {"qweb-pdf": "pdf", "qweb-text": "text"}
|
||||
|
||||
|
||||
class IrActionsReport(models.Model):
|
||||
_inherit = "ir.actions.report"
|
||||
@@ -99,9 +101,16 @@ class IrActionsReport(models.Model):
|
||||
|
||||
def print_document(self, record_ids, data=None):
|
||||
"""Print a document, do not return the document file"""
|
||||
document, doc_format = self.with_context(
|
||||
must_skip_send_to_printer=True
|
||||
)._render_qweb_pdf(record_ids, data=data)
|
||||
report_type = REPORT_TYPES.get(self.report_type)
|
||||
if not report_type:
|
||||
raise exceptions.UserError(
|
||||
_("This report type (%s) is not supported by direct printing!")
|
||||
% str(self.report_type)
|
||||
)
|
||||
method_name = "_render_qweb_%s" % (report_type)
|
||||
document, doc_format = getattr(
|
||||
self.with_context(must_skip_send_to_printer=True), method_name
|
||||
)(record_ids, data=data)
|
||||
behaviour = self.behaviour()
|
||||
printer = behaviour.pop("printer", None)
|
||||
|
||||
@@ -159,3 +168,22 @@ class IrActionsReport(models.Model):
|
||||
)
|
||||
|
||||
return document, doc_format
|
||||
|
||||
def _render_qweb_text(self, docids, data=None):
|
||||
"""Generate a TEXT file and returns it.
|
||||
|
||||
If the action configured on the report is server, it prints the
|
||||
generated document as well.
|
||||
"""
|
||||
document, doc_format = super()._render_qweb_text(docids=docids, data=data)
|
||||
|
||||
behaviour = self.behaviour()
|
||||
printer = behaviour.pop("printer", None)
|
||||
can_print_report = self._can_print_report(behaviour, printer, document)
|
||||
|
||||
if can_print_report:
|
||||
printer.print_document(
|
||||
self, document, doc_format=self.report_type, **behaviour
|
||||
)
|
||||
|
||||
return document, doc_format
|
||||
|
||||
@@ -8,5 +8,6 @@ The jobs will be sent to the printer with a name matching the print_report_name
|
||||
of the report (truncated at 80 characters). By default this will not be
|
||||
displayed by CUPS web interface or in Odoo. To see this information, you need
|
||||
to change the configuration of your CUPS server and set the JobPrivateValue
|
||||
directive to "job-name", and reload the server. See `cupsd.conf(5)
|
||||
directive to "none" (or some other list of values which does not include
|
||||
"job-name") , and reload the server. See `cupsd.conf(5)
|
||||
<https://www.cups.org/doc/man-cupsd.conf.html>` for details.
|
||||
|
||||
@@ -12,3 +12,4 @@
|
||||
* Rod Schouteden <rod@schout-it.be>
|
||||
* Alexandre Fayolle <alexandre.fayolle@camptocamp.com>
|
||||
* Matias Peralta <mnp@adhoc.com.ar>
|
||||
* Akim Juillerat <akim.juillerat@camptocamp.com>
|
||||
|
||||
@@ -128,6 +128,23 @@
|
||||
<field eval="1" name="perm_read" />
|
||||
<field eval="1" name="perm_unlink" />
|
||||
<field eval="1" name="perm_write" />
|
||||
</record>
|
||||
<record id="access_wizard_print_attachment_user" model="ir.model.access">
|
||||
<field name="name">Print Attachment User</field>
|
||||
<field name="model_id" ref="model_wizard_print_attachment" />
|
||||
<field name="group_id" ref="printing_group_user" />
|
||||
<field eval="1" name="perm_read" />
|
||||
<field eval="1" name="perm_unlink" />
|
||||
<field eval="1" name="perm_write" />
|
||||
<field eval="1" name="perm_create" />
|
||||
</record>
|
||||
<record id="access_wizard_print_attachment_line_user" model="ir.model.access">
|
||||
<field name="name">Print Attachment Line User</field>
|
||||
<field name="model_id" ref="model_wizard_print_attachment_line" />
|
||||
<field name="group_id" ref="printing_group_user" />
|
||||
<field eval="1" name="perm_read" />
|
||||
<field eval="1" name="perm_unlink" />
|
||||
<field eval="1" name="perm_write" />
|
||||
<field eval="1" name="perm_create" />
|
||||
</record>
|
||||
</odoo>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
|
||||
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
|
||||
<title>Report to printer</title>
|
||||
<style type="text/css">
|
||||
|
||||
@@ -432,7 +432,8 @@ rights to give users the ability to view the print menu.</li>
|
||||
of the report (truncated at 80 characters). By default this will not be
|
||||
displayed by CUPS web interface or in Odoo. To see this information, you need
|
||||
to change the configuration of your CUPS server and set the JobPrivateValue
|
||||
directive to “job-name”, and reload the server. See <cite>cupsd.conf(5)
|
||||
directive to “none” (or some other list of values which does not include
|
||||
“job-name”) , and reload the server. See <cite>cupsd.conf(5)
|
||||
<https://www.cups.org/doc/man-cupsd.conf.html></cite> for details.</p>
|
||||
</div>
|
||||
<div class="section" id="usage">
|
||||
@@ -506,6 +507,7 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
|
||||
<li>Rod Schouteden <<a class="reference external" href="mailto:rod@schout-it.be">rod@schout-it.be</a>></li>
|
||||
<li>Alexandre Fayolle <<a class="reference external" href="mailto:alexandre.fayolle@camptocamp.com">alexandre.fayolle@camptocamp.com</a>></li>
|
||||
<li>Matias Peralta <<a class="reference external" href="mailto:mnp@adhoc.com.ar">mnp@adhoc.com.ar</a>></li>
|
||||
<li>Akim Juillerat <<a class="reference external" href="mailto:akim.juillerat@camptocamp.com">akim.juillerat@camptocamp.com</a>></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section" id="maintainers">
|
||||
|
||||
@@ -47,6 +47,14 @@ class TestReport(common.HttpCase):
|
||||
"report_name": "base_report_to_printer.test",
|
||||
}
|
||||
)
|
||||
self.report_text = self.Model.create(
|
||||
{
|
||||
"name": "Test",
|
||||
"report_type": "qweb-text",
|
||||
"model": "res.partner",
|
||||
"report_name": "base_report_to_printer.test",
|
||||
}
|
||||
)
|
||||
self.partners = self.env["res.partner"]
|
||||
for n in range(5):
|
||||
self.partners += self.env["res.partner"].create({"name": "Test %d" % n})
|
||||
@@ -113,6 +121,24 @@ class TestReport(common.HttpCase):
|
||||
tray=False,
|
||||
)
|
||||
|
||||
def test_render_qweb_text_printable(self):
|
||||
"""It should print the report, only if it is printable"""
|
||||
with mock.patch(
|
||||
"odoo.addons.base_report_to_printer.models."
|
||||
"printing_printer.PrintingPrinter."
|
||||
"print_document"
|
||||
) as print_document:
|
||||
self.report_text.property_printing_action_id.action_type = "server"
|
||||
self.report_text.printing_printer_id = self.new_printer()
|
||||
document = self.report_text._render_qweb_text(self.partners.ids)
|
||||
print_document.assert_called_once_with(
|
||||
self.report_text,
|
||||
document[0],
|
||||
action="server",
|
||||
doc_format="qweb-text",
|
||||
tray=False,
|
||||
)
|
||||
|
||||
def test_print_document_not_printable(self):
|
||||
"""It should print the report, regardless of the defined behaviour"""
|
||||
self.report.printing_printer_id = self.new_printer()
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
from . import print_attachment_report
|
||||
from . import printing_printer_update_wizard
|
||||
|
||||
79
base_report_to_printer/wizards/print_attachment_report.py
Normal file
79
base_report_to_printer/wizards/print_attachment_report.py
Normal file
@@ -0,0 +1,79 @@
|
||||
# Copyright 2020 Camptocamp SA
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
|
||||
import base64
|
||||
|
||||
from odoo import _, fields, models
|
||||
|
||||
|
||||
class PrintAttachment(models.TransientModel):
|
||||
_name = "wizard.print.attachment"
|
||||
_description = "Print Attachment"
|
||||
|
||||
printer_id = fields.Many2one(
|
||||
comodel_name="printing.printer",
|
||||
string="Printer",
|
||||
required=True,
|
||||
help="Printer used to print the attachments.",
|
||||
)
|
||||
attachment_line_ids = fields.One2many(
|
||||
"wizard.print.attachment.line",
|
||||
"wizard_id",
|
||||
string="Attachments to print",
|
||||
)
|
||||
|
||||
def print_attachments(self):
|
||||
"""Prints a label per selected record"""
|
||||
self.ensure_one()
|
||||
errors = []
|
||||
for att_line in self.attachment_line_ids:
|
||||
data = att_line.attachment_id.datas
|
||||
title = att_line.attachment_id.name
|
||||
if not data:
|
||||
errors.append(att_line)
|
||||
continue
|
||||
content = base64.b64decode(data)
|
||||
content_format = att_line.get_format()
|
||||
self.printer_id.print_document(
|
||||
None,
|
||||
content=content,
|
||||
format=content_format,
|
||||
copies=att_line.copies,
|
||||
title=title,
|
||||
)
|
||||
if errors:
|
||||
return {
|
||||
"warning": _("Following attachments could not be printed:\n\n%s")
|
||||
% "\n".join(
|
||||
[
|
||||
_("{name} ({copies} copies)").format(
|
||||
name=err.record_name, copies=err.copies
|
||||
)
|
||||
for err in errors
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
class PrintAttachmentLine(models.TransientModel):
|
||||
_name = "wizard.print.attachment.line"
|
||||
_description = "Print Attachment line"
|
||||
|
||||
wizard_id = fields.Many2one("wizard.print.attachment")
|
||||
attachment_id = fields.Many2one(
|
||||
"ir.attachment",
|
||||
required=True,
|
||||
domain=(
|
||||
"['|', ('mimetype', '=', 'application/pdf'), "
|
||||
"('mimetype', '=', 'application/octet-stream')]"
|
||||
),
|
||||
)
|
||||
record_name = fields.Char(related="attachment_id.res_name", readonly=True)
|
||||
copies = fields.Integer(default=1)
|
||||
|
||||
def get_format(self):
|
||||
self.ensure_one()
|
||||
mimetype = self.attachment_id.mimetype
|
||||
if mimetype == "application/pdf":
|
||||
return "pdf"
|
||||
else:
|
||||
return "raw"
|
||||
56
base_report_to_printer/wizards/print_attachment_report.xml
Normal file
56
base_report_to_printer/wizards/print_attachment_report.xml
Normal file
@@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<odoo>
|
||||
<record id="wizard_print_attachment_form" model="ir.ui.view">
|
||||
<field name="name">wizard.print.attachment</field>
|
||||
<field name="model">wizard.print.attachment</field>
|
||||
<field name="arch" type="xml">
|
||||
<form string="Print attachments">
|
||||
<group>
|
||||
<field name="printer_id" />
|
||||
<field name="attachment_line_ids">
|
||||
<tree editable="top">
|
||||
<field name="attachment_id" create="0" />
|
||||
<field name="record_name" />
|
||||
<field name="copies" />
|
||||
</tree>
|
||||
</field>
|
||||
</group>
|
||||
<footer>
|
||||
<button
|
||||
name="print_attachments"
|
||||
type="object"
|
||||
string="Print"
|
||||
class="btn-primary"
|
||||
/>
|
||||
<button string="Cancel" class="btn-secondary" special="cancel" />
|
||||
</footer>
|
||||
</form>
|
||||
</field>
|
||||
</record>
|
||||
<record id="wizard_print_attachment_line_form" model="ir.ui.view">
|
||||
<field name="name">wizard.print.attachment.line.form</field>
|
||||
<field name="model">wizard.print.attachment.line</field>
|
||||
<field name="arch" type="xml">
|
||||
<form>
|
||||
<group>
|
||||
<field name="attachment_id" create="0" />
|
||||
<field name="record_name" />
|
||||
<field name="copies" />
|
||||
</group>
|
||||
</form>
|
||||
</field>
|
||||
</record>
|
||||
<record id="action_wizard_print_attachment" model="ir.actions.act_window">
|
||||
<field name="name">Print Attachments</field>
|
||||
<field name="type">ir.actions.act_window</field>
|
||||
<field name="res_model">wizard.print.attachment</field>
|
||||
<field name="view_mode">form</field>
|
||||
<field name="target">new</field>
|
||||
</record>
|
||||
<menuitem
|
||||
id="menu_action_wizard_print_attachment"
|
||||
action="action_wizard_print_attachment"
|
||||
sequence="50"
|
||||
parent="printing_menu"
|
||||
/>
|
||||
</odoo>
|
||||
Reference in New Issue
Block a user