[MIG] base_report_to_printer: Print attachments wizard

This commit is contained in:
sebalix
2020-08-06 12:30:44 +02:00
committed by Sébastien Alix
parent 0414d9c453
commit d1822016a8
3 changed files with 80 additions and 74 deletions

View File

@@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.14: http://docutils.sourceforge.net/" /> <meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
<title>Report to printer</title> <title>Report to printer</title>
<style type="text/css"> <style type="text/css">

View File

@@ -1,25 +1,26 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Camptocamp SA # Copyright 2020 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
import base64 import base64
from openerp import _, models, api, fields from odoo import _, fields, models
class PrintAttachment(models.TransientModel): class PrintAttachment(models.TransientModel):
_name = 'wizard.print.attachment' _name = "wizard.print.attachment"
_description = 'Print Attachment' _description = "Print Attachment"
printer_id = fields.Many2one( printer_id = fields.Many2one(
comodel_name='printing.printer', string='Printer', required=True, comodel_name="printing.printer",
help='Printer used to print the attachments.' string="Printer",
required=True,
help="Printer used to print the attachments.",
) )
attachment_line_ids = fields.One2many( attachment_line_ids = fields.One2many(
'wizard.print.attachment.line', 'wizard_id', "wizard.print.attachment.line",
string='Attachments to print', "wizard_id",
string="Attachments to print",
) )
@api.multi
def print_attachments(self): def print_attachments(self):
""" Prints a label per selected record """ """ Prints a label per selected record """
self.ensure_one() self.ensure_one()
@@ -32,18 +33,15 @@ class PrintAttachment(models.TransientModel):
content = base64.b64decode(data) content = base64.b64decode(data)
content_format = att_line.get_format() content_format = att_line.get_format()
self.printer_id.print_document( self.printer_id.print_document(
None, None, content=content, format=content_format, copies=att_line.copies
content=content,
format=content_format,
copies=att_line.copies
) )
if errors: if errors:
return { return {
'warning': _( "warning": _(
'Following attachments could not be printed:\n\n%s' "Following attachments could not be printed:\n\n%s"
% '\n'.join( % "\n".join(
[ [
_('%s (%s copies)') % (err.record_name, err.copies) _("%s (%s copies)") % (err.record_name, err.copies)
for err in errors for err in errors
] ]
) )
@@ -52,23 +50,25 @@ class PrintAttachment(models.TransientModel):
class PrintAttachmentLine(models.TransientModel): class PrintAttachmentLine(models.TransientModel):
_name = 'wizard.print.attachment.line' _name = "wizard.print.attachment.line"
_description = 'Print Attachment line' _description = "Print Attachment line"
wizard_id = fields.Many2one("wizard.print.attachment") wizard_id = fields.Many2one("wizard.print.attachment")
attachment_id = fields.Many2one( attachment_id = fields.Many2one(
'ir.attachment', "ir.attachment",
required=True, required=True,
domain="['|', ('mimetype', '=', 'application/pdf'), ('mimetype', '=', 'application/octet-stream')]" domain=(
"['|', ('mimetype', '=', 'application/pdf'), "
"('mimetype', '=', 'application/octet-stream')]"
),
) )
record_name = fields.Char(related="attachment_id.res_name", readonly=True) record_name = fields.Char(related="attachment_id.res_name", readonly=True)
copies = fields.Integer(default=1) copies = fields.Integer(default=1)
@api.multi
def get_format(self): def get_format(self):
self.ensure_one() self.ensure_one()
mimetype = self.attachment_id.mimetype mimetype = self.attachment_id.mimetype
if mimetype == "application/pdf": if mimetype == "application/pdf":
return "pdf" return "pdf"
else: else:
return 'raw' return "raw"

View File

@@ -1,50 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8" ?>
<openerp> <odoo>
<data> <record id="wizard_print_attachment_form" model="ir.ui.view">
<record id="wizard_print_attachment_form" model="ir.ui.view"> <field name="name">wizard.print.attachment</field>
<field name="name">wizard.print.attachment</field> <field name="model">wizard.print.attachment</field>
<field name="model">wizard.print.attachment</field> <field name="arch" type="xml">
<field name="arch" type="xml"> <form string="Print attachments">
<form string="Print attachments"> <group>
<group> <field name="printer_id" />
<field name="printer_id" /> <field name="attachment_line_ids">
<field name="attachment_line_ids"> <tree editable="top">
<tree editable="top"> <field name="attachment_id" create="0" />
<field name="attachment_id" create="0" /> <field name="record_name" />
<field name="record_name" /> <field name="copies" />
<field name="copies" /> </tree>
</tree> </field>
</field> </group>
</group> <footer>
<footer> <button
<button name="print_attachments" string="Print" type="object" class="oe_highlight"/> name="print_attachments"
or type="object"
<button string="Cancel" class="oe_link" special="cancel"/> string="Print"
</footer> class="btn-primary"
</form> />
</field> <button string="Cancel" class="btn-secondary" special="cancel" />
</record> </footer>
<record id="wizard_print_attachment_line_form" model="ir.ui.view"> </form>
<field name="name">wizard.print.attachment.line.form</field> </field>
<field name="model">wizard.print.attachment.line</field> </record>
<field name="arch" type="xml"> <record id="wizard_print_attachment_line_form" model="ir.ui.view">
<form> <field name="name">wizard.print.attachment.line.form</field>
<group> <field name="model">wizard.print.attachment.line</field>
<field name="attachment_id" create="0" /> <field name="arch" type="xml">
<field name="record_name" /> <form>
<field name="copies" /> <group>
</group> <field name="attachment_id" create="0" />
</form> <field name="record_name" />
</field> <field name="copies" />
</record> </group>
<record id="action_wizard_print_attachment" model="ir.actions.act_window" > </form>
<field name="name">Print Attachments</field> </field>
<field name="type">ir.actions.act_window</field> </record>
<field name="res_model">wizard.print.attachment</field> <record id="action_wizard_print_attachment" model="ir.actions.act_window">
<field name="view_type">form</field> <field name="name">Print Attachments</field>
<field name="view_mode">form</field> <field name="type">ir.actions.act_window</field>
<field name="target">new</field> <field name="res_model">wizard.print.attachment</field>
</record> <field name="view_mode">form</field>
<menuitem id="menu_action_wizard_print_attachment" action="action_wizard_print_attachment" sequence="50" parent="printing_menu" /> <field name="target">new</field>
</data> </record>
</openerp> <menuitem
id="menu_action_wizard_print_attachment"
action="action_wizard_print_attachment"
sequence="50"
parent="printing_menu"
/>
</odoo>