[PEP8] - base_report_to_printer

This commit is contained in:
Laetitia Gangloff
2015-09-28 11:49:45 +02:00
parent ea0069f5b9
commit 5033da0104
7 changed files with 67 additions and 55 deletions

View File

@@ -78,7 +78,8 @@ Contributors
* Lionel Sausin <ls@numerigraphe.com> * Lionel Sausin <ls@numerigraphe.com>
""", """,
'author': "Agile Business Group & Domsense, Pegueroles SCP, NaN,Odoo Community Association (OCA)", 'author': "Agile Business Group & Domsense, Pegueroles SCP, NaN,"
"Odoo Community Association (OCA)",
'website': 'http://www.agilebg.com', 'website': 'http://www.agilebg.com',
'license': 'AGPL-3', 'license': 'AGPL-3',
"depends": ['base', 'base_calendar'], "depends": ['base', 'base_calendar'],
@@ -93,5 +94,5 @@ Contributors
'application': True, 'application': True,
'external_dependencies': { 'external_dependencies': {
'python': ['cups'] 'python': ['cups']
} }
} }

View File

@@ -32,6 +32,7 @@ from openerp.osv import orm, fields
class report_xml(orm.Model): class report_xml(orm.Model):
""" """
Reports Reports
""" """
@@ -45,7 +46,8 @@ class report_xml(orm.Model):
options['raw'] = True options['raw'] = True
return options return options
def print_direct(self, cr, uid, report_id, result, format, printer, context=None): def print_direct(self, cr, uid, report_id, result, format, printer,
context=None):
fd, file_name = mkstemp() fd, file_name = mkstemp()
try: try:
os.write(fd, base64.decodestring(result)) os.write(fd, base64.decodestring(result))
@@ -59,9 +61,11 @@ class report_xml(orm.Model):
printer_system_name = printer.system_name printer_system_name = printer.system_name
connection = cups.Connection() connection = cups.Connection()
options = self.set_print_options(cr, uid, report_id, format, context=context) options = self.set_print_options(cr, uid, report_id, format,
context=context)
connection.printFile(printer_system_name, file_name, file_name, options=options) connection.printFile(printer_system_name, file_name, file_name,
options=options)
logger = logging.getLogger('base_report_to_printer') logger = logging.getLogger('base_report_to_printer')
logger.info("Printing job : '%s'" % file_name) logger.info("Printing job : '%s'" % file_name)
return True return True
@@ -69,20 +73,20 @@ class report_xml(orm.Model):
_inherit = 'ir.actions.report.xml' _inherit = 'ir.actions.report.xml'
_columns = { _columns = {
'property_printing_action': fields.property( 'property_printing_action': fields.property(
#'ir.actions.report.xml', # 'ir.actions.report.xml',
'printing.action', 'printing.action',
type='many2one', type='many2one',
relation='printing.action', relation='printing.action',
string='Action', string='Action',
view_load=True, view_load=True,
method=True, method=True,
), ),
'printing_printer_id': fields.many2one('printing.printer', 'Printer'), 'printing_printer_id': fields.many2one('printing.printer', 'Printer'),
'printing_action_ids': fields.one2many( 'printing_action_ids': fields.one2many(
'printing.report.xml.action', 'report_id', 'Actions', 'printing.report.xml.action', 'report_id', 'Actions',
help='This field allows configuring action and printer on a per ' help='This field allows configuring action and printer on a per '
'user basis'), 'user basis'),
} }
def behaviour(self, cr, uid, ids, context=None): def behaviour(self, cr, uid, ids, context=None):
result = {} result = {}
@@ -93,7 +97,8 @@ class report_xml(orm.Model):
# Retrieve system wide printer # Retrieve system wide printer
default_printer = printer_obj.get_default(cr, uid, context=context) default_printer = printer_obj.get_default(cr, uid, context=context)
if default_printer: if default_printer:
default_printer = printer_obj.browse(cr, uid, default_printer, context=context) default_printer = printer_obj.browse(cr, uid, default_printer,
context=context)
# Retrieve user default values # Retrieve user default values
user = self.pool['res.users'].browse(cr, uid, uid, context=context) user = self.pool['res.users'].browse(cr, uid, uid, context=context)
@@ -107,8 +112,8 @@ class report_xml(orm.Model):
printer = default_printer printer = default_printer
# Retrieve report default values # Retrieve report default values
if (report.property_printing_action if (report.property_printing_action and
and report.property_printing_action.type != 'user_default'): report.property_printing_action.type != 'user_default'):
action = report.property_printing_action.type action = report.property_printing_action.type
if report.printing_printer_id: if report.printing_printer_id:
printer = report.printing_printer_id printer = report.printing_printer_id
@@ -120,7 +125,8 @@ class report_xml(orm.Model):
('user_id', '=', uid), ('user_id', '=', uid),
('action', '!=', 'user_default')], context=context) ('action', '!=', 'user_default')], context=context)
if act_ids: if act_ids:
user_action = printing_act_obj.behaviour(cr, uid, act_ids[0], context) user_action = printing_act_obj.behaviour(cr, uid, act_ids[0],
context)
action = user_action['action'] action = user_action['action']
if user_action['printer']: if user_action['printer']:
printer = user_action['printer'] printer = user_action['printer']
@@ -128,5 +134,5 @@ class report_xml(orm.Model):
result[report.id] = { result[report.id] = {
'action': action, 'action': action,
'printer': printer, 'printer': printer,
} }
return result return result

View File

@@ -34,6 +34,7 @@ from openerp.tools.translate import _
class printing_printer(orm.Model): class printing_printer(orm.Model):
""" """
Printers Printers
""" """
@@ -78,14 +79,14 @@ class printing_printer(orm.Model):
'URI', 'URI',
size=500, size=500,
readonly=True), readonly=True),
} }
_order = "name" _order = "name"
_defaults = { _defaults = {
'default': lambda *a: False, 'default': lambda *a: False,
'status': lambda *a: 'unknown', 'status': lambda *a: 'unknown',
} }
def __init__(self, pool, cr): def __init__(self, pool, cr):
super(printing_printer, self).__init__(pool, cr) super(printing_printer, self).__init__(pool, cr)
@@ -113,7 +114,7 @@ class printing_printer(orm.Model):
if context is None: if context is None:
context = {} context = {}
try: try:
# Skip update to avoid the thread being created again # Skip update to avoid the thread being created again
ctx = context.copy() ctx = context.copy()
ctx['skip_update'] = True ctx['skip_update'] = True
ids = self.search(cr, uid, [], context=ctx) ids = self.search(cr, uid, [], context=ctx)
@@ -151,7 +152,8 @@ class printing_printer(orm.Model):
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=(cr.dbname, uid, context.copy()))
thread.start() thread.start()
def update(self, cr, uid, context=None): def update(self, cr, uid, context=None):
@@ -161,7 +163,8 @@ class printing_printer(orm.Model):
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(cr, uid, context)
# Wait up to five seconds for printer status update # Wait up to five seconds for printer status update
@@ -178,7 +181,8 @@ class printing_printer(orm.Model):
).search(cr, uid, args, offset, ).search(cr, uid, args, offset,
limit, order, context, count) limit, order, context, count)
def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'): def read(self, cr, uid, ids, fields=None, context=None,
load='_classic_read'):
self.update(cr, uid, context) self.update(cr, uid, context)
return super(printing_printer, self return super(printing_printer, self
).read(cr, uid, ids, fields, context, load) ).read(cr, uid, ids, fields, context, load)
@@ -211,7 +215,7 @@ def _available_action_types(self, cr, uid, context=None):
('server', _('Send to Printer')), ('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 printing_action(orm.Model):
@@ -220,5 +224,6 @@ class printing_action(orm.Model):
_columns = { _columns = {
'name': fields.char('Name', size=256, required=True), 'name': fields.char('Name', size=256, required=True),
'type': fields.selection(_available_action_types, 'Type', required=True), 'type': fields.selection(_available_action_types, 'Type',
} required=True),
}

View File

@@ -31,7 +31,8 @@ from openerp.addons.base_calendar import base_calendar
class virtual_report_spool(base_calendar.virtual_report_spool): class virtual_report_spool(base_calendar.virtual_report_spool):
def exp_report(self, db, uid, object, ids, datas=None, context=None): def exp_report(self, db, uid, object, ids, datas=None, context=None):
res = super(virtual_report_spool, self).exp_report(db, uid, object, ids, datas, context) res = super(virtual_report_spool, self).exp_report(
db, uid, object, ids, datas, context)
self._reports[res]['report_name'] = object self._reports[res]['report_name'] = object
return res return res
@@ -43,22 +44,27 @@ class virtual_report_spool(base_calendar.virtual_report_spool):
# First of all load report defaults: name, action and printer # First of all load report defaults: name, action and printer
report_obj = pool.get('ir.actions.report.xml') report_obj = pool.get('ir.actions.report.xml')
report = report_obj.search( report = report_obj.search(
cr, uid, [('report_name', '=', self._reports[report_id]['report_name'])]) cr, uid, [('report_name', '=',
self._reports[report_id]['report_name'])])
if report: if report:
report = report_obj.browse(cr, uid, report[0]) report = report_obj.browse(cr, uid, report[0])
data = report.behaviour()[report.id] data = report.behaviour()[report.id]
action = data['action'] action = data['action']
printer = data['printer'] printer = data['printer']
if action != 'client': if action != 'client':
if (self._reports and self._reports.get(report_id, False) if (self._reports and
and self._reports[report_id].get('result', False) self._reports.get(report_id, False) and
and self._reports[report_id].get('format', False)): self._reports[report_id].get('result', False) and
self._reports[report_id].get('format', False)):
report_obj.print_direct( report_obj.print_direct(
cr, uid, report.id, base64.encodestring(self._reports[report_id]['result']), cr, uid, report.id, base64.encodestring(
self._reports[report_id]['result']),
self._reports[report_id]['format'], printer) self._reports[report_id]['format'], printer)
# FIXME "Warning" removed as it breaks the workflow # FIXME "Warning" removed as it breaks the workflow
# it would be interesting to have a dialog box to confirm if we really want to print # it would be interesting to have a dialog box to
# in this case it must be with a by pass parameter to allow massive impression # confirm if we really want to print
# in this case it must be with a by pass parameter to
# allow massive impression
# raise osv.except_osv( # raise osv.except_osv(
# _('Printing...'), # _('Printing...'),
# _('Document sent to printer %s') % (printer,)) # _('Document sent to printer %s') % (printer,))
@@ -69,9 +75,8 @@ class virtual_report_spool(base_calendar.virtual_report_spool):
finally: finally:
cr.close() cr.close()
res = super(virtual_report_spool, self).exp_report_get(db, uid, report_id) res = super(virtual_report_spool, self).exp_report_get(db, uid,
report_id)
return res return res
virtual_report_spool() virtual_report_spool()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@@ -26,25 +26,25 @@ from openerp.osv import orm, fields
from printing import _available_action_types from printing import _available_action_types
class report_xml_action(orm.Model): class report_xml_action(orm.Model):
_name = 'printing.report.xml.action' _name = 'printing.report.xml.action'
_description = 'Report Printing Actions' _description = 'Report Printing Actions'
_columns = { _columns = {
'report_id': fields.many2one('ir.actions.report.xml', 'Report', required=True, ondelete='cascade'), 'report_id': fields.many2one('ir.actions.report.xml', 'Report',
'user_id': fields.many2one('res.users', 'User', required=True, ondelete='cascade'), required=True, ondelete='cascade'),
'action': fields.selection(_available_action_types, 'Action', required=True), 'user_id': fields.many2one('res.users', 'User', required=True,
ondelete='cascade'),
'action': fields.selection(_available_action_types, 'Action',
required=True),
'printer_id': fields.many2one('printing.printer', 'Printer'), 'printer_id': fields.many2one('printing.printer', 'Printer'),
} }
def behaviour(self, cr, uid, act_id, context=None): def behaviour(self, cr, uid, act_id, context=None):
result = {}
if not act_id: if not act_id:
return False return False
action = self.browse(cr, uid, act_id, context=context) action = self.browse(cr, uid, act_id, context=context)
return { return {
'action': action.action, 'action': action.action,
'printer': action.printer_id, 'printer': action.printer_id,
} }
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@@ -28,6 +28,7 @@ from printing import _available_action_types
class res_users(orm.Model): class res_users(orm.Model):
""" """
Users Users
""" """
@@ -41,8 +42,8 @@ class res_users(orm.Model):
if x[0] != 'user_default'] if x[0] != 'user_default']
_columns = { _columns = {
'printing_action': fields.selection(_user_available_action_types, 'Printing Action'), 'printing_action': fields.selection(_user_available_action_types,
'printing_printer_id': fields.many2one('printing.printer', 'Default Printer'), 'Printing Action'),
} 'printing_printer_id': fields.many2one('printing.printer',
'Default Printer'),
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: }

View File

@@ -29,9 +29,6 @@ from openerp.osv import orm
class printing_printer_update_wizard(orm.TransientModel): class printing_printer_update_wizard(orm.TransientModel):
_name = "printing.printer.update.wizard" _name = "printing.printer.update.wizard"
_columns = {
}
def action_cancel(self, cr, uid, ids, context=None): def action_cancel(self, cr, uid, ids, context=None):
return {} return {}
@@ -57,7 +54,7 @@ class printing_printer_update_wizard(orm.TransientModel):
'model': printer.get('printer-make-and-model', False), 'model': printer.get('printer-make-and-model', False),
'location': printer.get('printer-location', False), 'location': printer.get('printer-location', False),
'uri': printer.get('device-uri', False), 'uri': printer.get('device-uri', False),
}, context) }, context)
return { return {
'name': 'Printers', 'name': 'Printers',
@@ -66,7 +63,4 @@ class printing_printer_update_wizard(orm.TransientModel):
'res_model': 'printing.printer', 'res_model': 'printing.printer',
'type': 'ir.actions.act_window', 'type': 'ir.actions.act_window',
'target': 'current', 'target': 'current',
} }
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: