mirror of
https://github.com/OCA/report-print-send.git
synced 2025-02-16 07:11:31 +02:00
Migrate printing.py to new API
This commit is contained in:
committed by
Sylvain GARANCHER
parent
f46ad66960
commit
ce0876ef9f
@@ -5,8 +5,7 @@
|
|||||||
# Copyright (c) 2009 Albert Cervera i Areny <albert@nan-tic.com>
|
# Copyright (c) 2009 Albert Cervera i Areny <albert@nan-tic.com>
|
||||||
# Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>)
|
# Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>)
|
||||||
# Copyright (C) 2011 Domsense srl (<http://www.domsense.com>)
|
# Copyright (C) 2011 Domsense srl (<http://www.domsense.com>)
|
||||||
# Copyright (C) 2013 Camptocamp (<http://www.camptocamp.com>)
|
# Copyright (C) 2013-2014 Camptocamp (<http://www.camptocamp.com>)
|
||||||
# All Rights Reserved
|
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU Affero General Public License as published
|
# it under the terms of the GNU Affero General Public License as published
|
||||||
@@ -25,145 +24,118 @@
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
import cups
|
import cups
|
||||||
|
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
|
||||||
from openerp import pooler
|
from openerp import models, fields, api, sql_db
|
||||||
from openerp.osv import orm, fields
|
|
||||||
from openerp.tools.translate import _
|
|
||||||
|
|
||||||
|
|
||||||
class printing_printer(orm.Model):
|
class PrintingPrinter(models.Model):
|
||||||
"""
|
"""
|
||||||
Printers
|
Printers
|
||||||
"""
|
"""
|
||||||
_name = "printing.printer"
|
|
||||||
_description = "Printer"
|
|
||||||
|
|
||||||
_columns = {
|
_name = 'printing.printer'
|
||||||
'name': fields.char(
|
_description = 'Printer'
|
||||||
'Name',
|
_order = 'name'
|
||||||
size=64,
|
|
||||||
required=True,
|
|
||||||
select="1"),
|
|
||||||
'system_name': fields.char(
|
|
||||||
'System Name',
|
|
||||||
size=64,
|
|
||||||
required=True,
|
|
||||||
select="1"),
|
|
||||||
'default': fields.boolean(
|
|
||||||
'Default Printer',
|
|
||||||
readonly=True),
|
|
||||||
'status': fields.selection(
|
|
||||||
[('unavailable', 'Unavailable'),
|
|
||||||
('printing', 'Printing'),
|
|
||||||
('unknown', 'Unknown'),
|
|
||||||
('available', 'Available'),
|
|
||||||
('error', 'Error'),
|
|
||||||
('server-error', 'Server Error')],
|
|
||||||
'Status', required=True, readonly=True),
|
|
||||||
'status_message': fields.char(
|
|
||||||
'Status Message',
|
|
||||||
size=500,
|
|
||||||
readonly=True),
|
|
||||||
'model': fields.char(
|
|
||||||
'Model',
|
|
||||||
size=500,
|
|
||||||
readonly=True),
|
|
||||||
'location': fields.char(
|
|
||||||
'Location',
|
|
||||||
size=500,
|
|
||||||
readonly=True),
|
|
||||||
'uri': fields.char(
|
|
||||||
'URI',
|
|
||||||
size=500,
|
|
||||||
readonly=True),
|
|
||||||
}
|
|
||||||
|
|
||||||
_order = "name"
|
name = fields.Char(required=True, select=True)
|
||||||
|
system_name = fields.Char(required=True, select=True)
|
||||||
_defaults = {
|
default = fields.Boolean(readonly=True)
|
||||||
'default': lambda *a: False,
|
status = fields.Selection([('unavailable', 'Unavailable'),
|
||||||
'status': lambda *a: 'unknown',
|
('printing', 'Printing'),
|
||||||
}
|
('unknown', 'Unknown'),
|
||||||
|
('available', 'Available'),
|
||||||
|
('error', 'Error'),
|
||||||
|
('server-error', 'Server Error')],
|
||||||
|
required=True,
|
||||||
|
readonly=True,
|
||||||
|
default='unknown')
|
||||||
|
status_message = fields.Char(readonly=True)
|
||||||
|
model = fields.Char(readonly=True)
|
||||||
|
location = fields.Char(readonly=True)
|
||||||
|
uri = fields.Char(readonly=True)
|
||||||
|
|
||||||
def __init__(self, pool, cr):
|
def __init__(self, pool, cr):
|
||||||
super(printing_printer, self).__init__(pool, cr)
|
super(PrintingPrinter, self).__init__(pool, cr)
|
||||||
self.lock = Lock()
|
self.lock = Lock()
|
||||||
self.last_update = None
|
self.last_update = None
|
||||||
self.updating = False
|
self.updating = False
|
||||||
|
|
||||||
def update_printers_status(self, db_name, uid, context=None):
|
@api.model
|
||||||
db, pool = pooler.get_db_and_pool(db_name)
|
def update_printers_status(self):
|
||||||
cr = db.cursor()
|
cr = sql_db.db_connect(self.env.cr.dbname).cursor()
|
||||||
|
uid, context = self.env.uid, self.env.context
|
||||||
|
with api.Environment.manage():
|
||||||
|
self.env = api.Environment(cr, uid, context)
|
||||||
|
try:
|
||||||
|
connection = cups.Connection()
|
||||||
|
printers = connection.getPrinters()
|
||||||
|
server_error = False
|
||||||
|
except:
|
||||||
|
server_error = True
|
||||||
|
|
||||||
try:
|
mapping = {
|
||||||
connection = cups.Connection()
|
3: 'available',
|
||||||
printers = connection.getPrinters()
|
4: 'printing',
|
||||||
server_error = False
|
5: 'error'
|
||||||
except:
|
}
|
||||||
server_error = True
|
|
||||||
|
|
||||||
mapping = {
|
try:
|
||||||
3: 'available',
|
# Skip update to avoid the thread being created again
|
||||||
4: 'printing',
|
env = self.env.with_context(skip_update=True)
|
||||||
5: 'error'
|
printer_recs = env.search([])
|
||||||
}
|
for printer in printer_recs:
|
||||||
|
vals = {}
|
||||||
|
if server_error:
|
||||||
|
status = 'server-error'
|
||||||
|
elif printer.system_name in printers:
|
||||||
|
info = printers[printer.system_name]
|
||||||
|
status = mapping.get(info['printer-state'], 'unknown')
|
||||||
|
vals = {
|
||||||
|
'model': info.get('printer-make-and-model', False),
|
||||||
|
'location': info.get('printer-location', False),
|
||||||
|
'uri': info.get('device-uri', False),
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
status = 'unavailable'
|
||||||
|
|
||||||
if context is None:
|
vals['status'] = status
|
||||||
context = {}
|
printer.write(vals)
|
||||||
try:
|
self.env.cr.commit()
|
||||||
# Skip update to avoid the thread being created again
|
except:
|
||||||
ctx = context.copy()
|
self.env.cr.rollback()
|
||||||
ctx['skip_update'] = True
|
raise
|
||||||
ids = self.search(cr, uid, [], context=ctx)
|
finally:
|
||||||
for printer in self.browse(cr, uid, ids, context=ctx):
|
self.env.cr.close()
|
||||||
vals = {}
|
with self.lock:
|
||||||
if server_error:
|
self.updating = False
|
||||||
status = 'server-error'
|
self.last_update = time.time()
|
||||||
elif printer.system_name in printers:
|
|
||||||
info = printers[printer.system_name]
|
|
||||||
status = mapping.get(info['printer-state'], 'unknown')
|
|
||||||
vals = {
|
|
||||||
'model': info.get('printer-make-and-model', False),
|
|
||||||
'location': info.get('printer-location', False),
|
|
||||||
'uri': info.get('device-uri', False),
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
status = 'unavailable'
|
|
||||||
|
|
||||||
vals['status'] = status
|
@api.model
|
||||||
self.write(cr, uid, [printer.id], vals, context=context)
|
def start_printer_update(self):
|
||||||
cr.commit()
|
|
||||||
except:
|
|
||||||
cr.rollback()
|
|
||||||
raise
|
|
||||||
finally:
|
|
||||||
cr.close()
|
|
||||||
with self.lock:
|
|
||||||
self.updating = False
|
|
||||||
self.last_update = time.time()
|
|
||||||
|
|
||||||
def start_printer_update(self, cr, uid, context):
|
|
||||||
self.lock.acquire()
|
self.lock.acquire()
|
||||||
if self.updating:
|
if self.updating:
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return
|
return
|
||||||
self.updating = True
|
self.updating = True
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
thread = Thread(target=self.update_printers_status, args=(cr.dbname, uid, context.copy()))
|
thread = Thread(target=self.update_printers_status, args=())
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
def update(self, cr, uid, context=None):
|
@api.model
|
||||||
|
def update(self):
|
||||||
"""Update printer status if current status is more than 10s old."""
|
"""Update printer status if current status is more than 10s old."""
|
||||||
# We won't acquire locks - we're only assigning from immutable data
|
# We won't acquire locks - we're only assigning from immutable data
|
||||||
if not context or 'skip_update' in context:
|
if not self.env.context or 'skip_update' in self.env.context:
|
||||||
return True
|
return True
|
||||||
last_update = self.last_update
|
last_update = self.last_update
|
||||||
now = time.time()
|
now = time.time()
|
||||||
# Only update printer status if current status is more than 10 seconds old.
|
# Only update printer status if current status is more than 10
|
||||||
|
# seconds old.
|
||||||
if not last_update or now - last_update > 10:
|
if not last_update or now - last_update > 10:
|
||||||
self.start_printer_update(cr, uid, context)
|
self.start_printer_update()
|
||||||
# Wait up to five seconds for printer status update
|
# Wait up to five seconds for printer status update
|
||||||
for _dummy in range(0, 5):
|
for _dummy in range(0, 5):
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
@@ -171,54 +143,53 @@ class printing_printer(orm.Model):
|
|||||||
break
|
break
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def search(self, cr, uid, args, offset=0, limit=None, order=None,
|
@api.returns('self')
|
||||||
|
def search(self, cr, user, args, offset=0, limit=None, order=None,
|
||||||
context=None, count=False):
|
context=None, count=False):
|
||||||
self.update(cr, uid, context)
|
self.update()
|
||||||
return super(printing_printer, self
|
_super = super(PrintingPrinter, self)
|
||||||
).search(cr, uid, args, offset,
|
return _super.search(cr, user, args, offset=offset, limit=limit,
|
||||||
limit, order, context, count)
|
order=order, context=context, count=count)
|
||||||
|
|
||||||
def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
|
@api.v8
|
||||||
self.update(cr, uid, context)
|
def read(self, fields=None, load='_classic_read'):
|
||||||
return super(printing_printer, self
|
self.update()
|
||||||
).read(cr, uid, ids, fields, context, load)
|
return super(PrintingPrinter, self).read(fields=fields, load=load)
|
||||||
|
|
||||||
def browse(self, cr, uid, ids, context=None):
|
@api.v8
|
||||||
self.update(cr, uid, context)
|
def browse(self, arg=None):
|
||||||
return super(printing_printer, self).browse(cr, uid, ids, context)
|
self.update()
|
||||||
|
return super(PrintingPrinter, self).browse(arg=arg)
|
||||||
|
|
||||||
def set_default(self, cr, uid, ids, context):
|
@api.multi
|
||||||
if not ids:
|
def set_default(self):
|
||||||
|
if not self:
|
||||||
return
|
return
|
||||||
default_ids = self.search(cr, uid, [('default', '=', True)])
|
self.ensure_one()
|
||||||
self.write(cr, uid, default_ids, {'default': False}, context)
|
default_printers = self.search([('default', '=', True)])
|
||||||
self.write(cr, uid, ids[0], {'default': True}, context)
|
default_printers.write({'default': False})
|
||||||
|
self.write({'default': True})
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def get_default(self, cr, uid, context):
|
@api.multi
|
||||||
printer_ids = self.search(cr, uid, [('default', '=', True)])
|
def get_default(self):
|
||||||
if printer_ids:
|
return self.search([('default', '=', True)], limit=1)
|
||||||
return printer_ids[0]
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Actions
|
# Actions
|
||||||
#
|
#
|
||||||
|
|
||||||
def _available_action_types(self, cr, uid, context=None):
|
|
||||||
return [
|
def _available_action_types(self):
|
||||||
('server', _('Send to Printer')),
|
return [('server', 'Send to Printer'),
|
||||||
('client', _('Send to Client')),
|
('client', 'Send to Client'),
|
||||||
('user_default', _("Use user's defaults")),
|
('user_default', "Use user's defaults"),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class printing_action(orm.Model):
|
class PrintingAction(models.Model):
|
||||||
_name = 'printing.action'
|
_name = 'printing.action'
|
||||||
_description = 'Print Job Action'
|
_description = 'Print Job Action'
|
||||||
|
|
||||||
_columns = {
|
name = fields.Char(required=True)
|
||||||
'name': fields.char('Name', size=256, required=True),
|
type = fields.Selection(_available_action_types, required=True)
|
||||||
'type': fields.selection(_available_action_types, 'Type', required=True),
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user