[ADD] base_report_to_printer: Print attachments wizard

This commit is contained in:
Akim Juillerat
2020-04-27 14:36:35 +02:00
committed by sonhd
parent 00635859b4
commit adfeb47a1b
7 changed files with 130 additions and 1 deletions

View File

@@ -154,6 +154,7 @@ Contributors
* Graeme Gellatly <graeme@o4sb.com>
* Rod Schouteden <rod@schout-it.be>
* Alexandre Fayolle <alexandre.fayolle@camptocamp.com>
* Akim Juillerat <akim.juillerat@camptocamp.com>
Maintainers
~~~~~~~~~~~

View File

@@ -25,6 +25,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",
],
"installable": True,

View File

@@ -11,3 +11,4 @@
* Graeme Gellatly <graeme@o4sb.com>
* Rod Schouteden <rod@schout-it.be>
* Alexandre Fayolle <alexandre.fayolle@camptocamp.com>
* Akim Juillerat <akim.juillerat@camptocamp.com>

View File

@@ -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 0.14: http://docutils.sourceforge.net/" />
<title>Report to printer</title>
<style type="text/css">
@@ -505,6 +505,7 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
<li>Graeme Gellatly &lt;<a class="reference external" href="mailto:graeme&#64;o4sb.com">graeme&#64;o4sb.com</a>&gt;</li>
<li>Rod Schouteden &lt;<a class="reference external" href="mailto:rod&#64;schout-it.be">rod&#64;schout-it.be</a>&gt;</li>
<li>Alexandre Fayolle &lt;<a class="reference external" href="mailto:alexandre.fayolle&#64;camptocamp.com">alexandre.fayolle&#64;camptocamp.com</a>&gt;</li>
<li>Akim Juillerat &lt;<a class="reference external" href="mailto:akim.juillerat&#64;camptocamp.com">akim.juillerat&#64;camptocamp.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">

View File

@@ -1 +1,2 @@
from . import print_attachment_report
from . import printing_printer_update_wizard

View File

@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
import base64
from openerp import _, models, api, fields
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',
)
@api.multi
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
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
)
if errors:
return {
'warning': _(
'Following attachments could not be printed:\n\n%s'
% '\n'.join(
[
_('%s (%s copies)') % (err.record_name, 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)
@api.multi
def get_format(self):
self.ensure_one()
mimetype = self.attachment_id.mimetype
if mimetype == "application/pdf":
return "pdf"
else:
return 'raw'

View File

@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<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" string="Print" type="object" class="oe_highlight"/>
or
<button string="Cancel" class="oe_link" 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_type">form</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" />
</data>
</openerp>