Skip to content

Commit

Permalink
[WIP] Commit before rebase on latest OCA#109 for user and view updates
Browse files Browse the repository at this point in the history
Migration of printer_tray to v11 and integration with base_report_to_printer
  • Loading branch information
gdgellatly authored and schout-it committed Oct 14, 2019
1 parent 877e5cb commit 52a8e0e
Show file tree
Hide file tree
Showing 18 changed files with 788 additions and 136 deletions.
28 changes: 20 additions & 8 deletions base_report_to_printer/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@ Report To Printer

This module allows users to send reports to a printer attached to the server.


It adds an optional behaviour on reports to send it directly to a printer.

* `Send to Client` is the default behaviour providing you a downloadable PDF
* `Send to Printer` prints the report on selected printer

It detects trays on printers installation plus permits to select the
paper source on which you want to print directly.

Report behaviour is defined by settings.

You will find this option on default user config, on default report
config and on specific config per user per report.

This allows you to dedicate a specific paper source for example for
preprinted paper such as payment slip.

Settings can be configured:

* globaly
* globally
* per user
* per report
* per user and report
Expand Down Expand Up @@ -47,12 +54,17 @@ rights to give users the ability to view the print menu.
Usage
=====

To show all available printers for your server, use the
`Settings/Configuration/Printing/Update Printers from CUPS` wizard.

* To update the CUPS printers in *Settings > Printing > Update Printers
from CUPS*
* If you want to print a report on a specific printer or tray, you can change
these in *Settings > Printing > Reports* to define default behaviour.
* If you want to print a report on a specific printer and/or tray for a user, you can
change these in *Settings > Printing > Reports* in
*Specific actions per user*
* Users may also select a default action, printer or tray in their preferences

Then go to the user profile and set the users printing action and default
printer.
When no tray is configured for a report and a user, the
default tray setup on the CUPS server is used.

Caveat
------
Expand All @@ -62,7 +74,7 @@ displayed for the deprecated report types (RML, Webkit, ...).

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/144/9.0
:target: https://runbot.odoo-community.org/runbot/144/11.0


Known issues / Roadmap
Expand Down
4 changes: 2 additions & 2 deletions base_report_to_printer/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

{
'name': "Report to printer",
'version': '11.0.1.0.1',
'version': '11.0.2.0.0',
'category': 'Generic Modules/Base',
'author': "Agile Business Group & Domsense, Pegueroles SCP, NaN,"
" LasLabs, Odoo Community Association (OCA)",
" LasLabs, Camptocamp, Odoo Community Association (OCA)",
'website': 'http://www.agilebg.com',
'license': 'AGPL-3',
"depends": ['web'],
Expand Down
1 change: 1 addition & 0 deletions base_report_to_printer/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
from . import printing_printer
from . import printing_server
from . import printing_report_xml_action
from . import printing_tray
from . import res_users
10 changes: 10 additions & 0 deletions base_report_to_printer/models/ir_actions_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class IrActionsReport(models.Model):
comodel_name='printing.printer',
string='Default Printer'
)
printer_tray_id = fields.Many2one(
comodel_name='printing.tray',
string='Paper Source',
domain="[('printer_id', '=', printing_printer_id)]",
)
printing_action_ids = fields.One2many(
comodel_name='printing.report.xml.action',
inverse_name='report_id',
Expand All @@ -29,6 +34,11 @@ class IrActionsReport(models.Model):
'user basis'
)

@api.onchange('printing_printer_id')
def onchange_printing_printer_id(self):
""" Reset the tray when the printer is changed """
self.printer_tray_id = False

@api.model
def print_action_for_report_name(self, report_name):
""" Returns if the action is a direct print or pdf
Expand Down
75 changes: 74 additions & 1 deletion base_report_to_printer/models/printing_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
# Copyright (C) 2016 SYLEAM (<http://www.syleam.fr>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import errno
import logging

import os
from tempfile import mkstemp

Expand All @@ -17,6 +17,11 @@

_logger = logging.getLogger(__name__)

try:
import cups
except ImportError:
_logger.debug('Cannot `import cups`.')


class PrintingPrinter(models.Model):
"""
Expand Down Expand Up @@ -52,6 +57,55 @@ class PrintingPrinter(models.Model):
model = fields.Char(readonly=True)
location = fields.Char(readonly=True)
uri = fields.Char(string='URI', readonly=True)
tray_ids = fields.One2many(comodel_name='printing.tray',
inverse_name='printer_id',
string='Paper Sources')

@api.multi
def _prepare_update_from_cups(self, cups_connection, cups_printer):
vals = super(PrintingPrinter, self)._prepare_update_from_cups(
cups_connection, cups_printer)

printer_uri = cups_printer['printer-uri-supported']
printer_system_name = printer_uri[printer_uri.rfind('/') + 1:]
ppd_info = cups_connection.getPPD3(printer_system_name)
ppd_path = ppd_info[2]
if not ppd_path:
return vals

ppd = cups.PPD(ppd_path)
option = ppd.findOption('InputSlot')
try:
os.unlink(ppd_path)
except OSError as err:
# ENOENT means No such file or directory
# The file has already been deleted, we can continue the update
if err.errno != errno.ENOENT:
raise
if not option:
return vals

vals['tray_ids'] = []
cups_trays = {
tray_option['choice']: tray_option['text']
for tray_option in option.choices
}

# Add new trays
vals['tray_ids'].extend([
(0, 0, {'name': text, 'system_name': choice})
for choice, text in cups_trays.items()
if choice not in self.tray_ids.mapped('system_name')
])

# Remove deleted trays
vals['tray_ids'].extend([
(2, tray.id)
for tray in self.tray_ids.filtered(
lambda record: record.system_name not in cups_trays.keys())
])

return vals

@api.multi
def _prepare_update_from_cups(self, cups_connection, cups_printer):
Expand Down Expand Up @@ -79,6 +133,25 @@ def print_options(self, report=None, format=None, copies=1):
options['raw'] = 'True'
if copies > 1:
options['copies'] = str(copies)
if report is not None:
printing_act_obj = self.env['printing.report.xml.action']
if report.printer_tray_id:
tray = report.printer_tray_id
else:
# Retrieve user default values
tray = self.env.user.printer_tray_id

# Retrieve report-user specific values
action = printing_act_obj.search([
('report_id', '=', report.id),
('user_id', '=', self.env.uid),
('action', '!=', 'user_default'),
], limit=1)
if action.printer_tray_id:
tray = action.printer_tray_id

if tray:
options['InputSlot'] = str(tray.system_name)
return options

@api.multi
Expand Down
12 changes: 12 additions & 0 deletions base_report_to_printer/models/printing_report_xml_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,23 @@ class PrintingReportXmlAction(models.Model):
printer_id = fields.Many2one(comodel_name='printing.printer',
string='Printer')

printer_tray_id = fields.Many2one(
comodel_name='printing.tray',
string='Paper Source',
domain="[('printer_id', '=', printer_id)]",
)

@api.onchange('printer_id')
def onchange_printer_id(self):
""" Reset the tray when the printer is changed """
self.printer_tray_id = False

@api.multi
def behaviour(self):
if not self:
return {}
return {
'action': self.action,
'printer': self.printer_id,
'tray': self.printer_tray_id.system_name
}
22 changes: 22 additions & 0 deletions base_report_to_printer/models/printing_tray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class PrinterTray(models.Model):
_name = 'printing.tray'
_description = 'Printer Tray'

_order = 'name asc'

name = fields.Char(required=True)
system_name = fields.Char(required=True, readonly=True)
printer_id = fields.Many2one(
comodel_name='printing.printer',
string='Printer',
required=True,
readonly=True,
ondelete='cascade',
)
12 changes: 12 additions & 0 deletions base_report_to_printer/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ def _register_hook(self):
'printing_action',
'printing_printer_id',
])

printer_tray_id = fields.Many2one(
comodel_name='printing.tray',
string='Default Printer Paper Source',
domain="[('printer_id', '=', printing_printer_id)]",
)

@api.onchange('printing_printer_id')
def onchange_printing_printer_id(self):
""" Reset the tray when the printer is changed """
self.printer_tray_id = False

0 comments on commit 52a8e0e

Please sign in to comment.