Migrate printer_tray to v9.0

This commit is contained in:
Sylvain GARANCHER
2016-11-08 13:13:30 +01:00
committed by Graeme Gellatly
parent 9bbe10bfc7
commit c59f9d6dd0
20 changed files with 173 additions and 266 deletions

View File

@@ -0,0 +1,9 @@
# -*- 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 . import ir_actions_report_xml
from . import printing_tray
from . import printing_printer
from . import res_users
from . import printing_report_xml_action

View File

@@ -0,0 +1,20 @@
# -*- 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 openerp import models, fields, api
class IrActionsReportXml(models.Model):
_inherit = 'ir.actions.report.xml'
printer_tray_id = fields.Many2one(
comodel_name='printing.tray',
string='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

View File

@@ -0,0 +1,93 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import errno
import logging
import os
from openerp import models, fields, api
_logger = logging.getLogger(__name__)
try:
import cups
except ImportError:
_logger.debug('Cannot `import cups`.')
class PrintingPrinter(models.Model):
_inherit = 'printing.printer'
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:
if err.errno == errno.ENOENT:
pass
raise
if not option:
return vals
vals_trays = []
tray_names = set(tray.system_name for tray in self.tray_ids)
for tray_option in option.choices:
if tray_option['choice'] not in tray_names:
tray_vals = {
'name': tray_option['text'],
'system_name': tray_option['choice'],
}
vals_trays.append((0, 0, tray_vals))
cups_trays = set(tray_option['choice'] for tray_option
in option.choices)
for tray in self.tray_ids:
if tray.system_name not in cups_trays:
vals_trays.append((2, tray.id))
vals['tray_ids'] = vals_trays
return vals
@api.multi
def print_options(self, report, format, copies=1):
""" Hook to define Tray """
printing_act_obj = self.env['printing.report.xml.action']
options = super(PrintingPrinter, self).print_options(report, format)
# Retrieve user default values
user = self.env.user
tray = user.printer_tray_id
# Retrieve report default values
if report.printer_tray_id:
tray = report.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

View File

@@ -0,0 +1,27 @@
# -*- 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 openerp import models, fields, api
class PrintingReportXMLAction(models.Model):
_inherit = 'printing.report.xml.action'
printer_tray_id = fields.Many2one(
comodel_name='printing.tray',
string='Paper Source',
domain="[('printer_id', '=', printer_id)]",
)
@api.multi
def behaviour(self):
self.ensure_one()
res = super(PrintingReportXMLAction, self).behaviour()
res['tray'] = self.printer_tray_id.system_name
return res
@api.onchange('printer_id')
def onchange_printer_id(self):
""" Reset the tray when the printer is changed """
self.printer_tray_id = False

View File

@@ -0,0 +1,20 @@
# -*- 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 openerp import models, fields
class PrinterTray(models.Model):
_name = 'printing.tray'
_description = 'Printer Tray'
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',
)

View File

@@ -0,0 +1,20 @@
# -*- 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 openerp import models, fields, api
class ResUsers(models.Model):
_inherit = "res.users"
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