[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>
""",
'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',
'license': 'AGPL-3',
"depends": ['base', 'base_calendar'],
@@ -93,5 +94,5 @@ Contributors
'application': True,
'external_dependencies': {
'python': ['cups']
}
}
}

View File

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

View File

@@ -34,6 +34,7 @@ from openerp.tools.translate import _
class printing_printer(orm.Model):
"""
Printers
"""
@@ -78,14 +79,14 @@ class printing_printer(orm.Model):
'URI',
size=500,
readonly=True),
}
}
_order = "name"
_defaults = {
'default': lambda *a: False,
'status': lambda *a: 'unknown',
}
}
def __init__(self, pool, cr):
super(printing_printer, self).__init__(pool, cr)
@@ -113,7 +114,7 @@ class printing_printer(orm.Model):
if context is None:
context = {}
try:
# Skip update to avoid the thread being created again
# Skip update to avoid the thread being created again
ctx = context.copy()
ctx['skip_update'] = True
ids = self.search(cr, uid, [], context=ctx)
@@ -151,7 +152,8 @@ class printing_printer(orm.Model):
return
self.updating = True
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()
def update(self, cr, uid, context=None):
@@ -161,7 +163,8 @@ class printing_printer(orm.Model):
return True
last_update = self.last_update
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:
self.start_printer_update(cr, uid, context)
# Wait up to five seconds for printer status update
@@ -178,7 +181,8 @@ class printing_printer(orm.Model):
).search(cr, uid, args, offset,
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)
return super(printing_printer, self
).read(cr, uid, ids, fields, context, load)
@@ -211,7 +215,7 @@ def _available_action_types(self, cr, uid, context=None):
('server', _('Send to Printer')),
('client', _('Send to Client')),
('user_default', _("Use user's defaults")),
]
]
class printing_action(orm.Model):
@@ -220,5 +224,6 @@ class printing_action(orm.Model):
_columns = {
'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):
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
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
report_obj = pool.get('ir.actions.report.xml')
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:
report = report_obj.browse(cr, uid, report[0])
data = report.behaviour()[report.id]
action = data['action']
printer = data['printer']
if action != 'client':
if (self._reports and self._reports.get(report_id, False)
and self._reports[report_id].get('result', False)
and self._reports[report_id].get('format', False)):
if (self._reports and
self._reports.get(report_id, False) and
self._reports[report_id].get('result', False) and
self._reports[report_id].get('format', False)):
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)
# 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
# in this case it must be with a by pass parameter to allow massive impression
# it would be interesting to have a dialog box to
# 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(
# _('Printing...'),
# _('Document sent to printer %s') % (printer,))
@@ -69,9 +75,8 @@ class virtual_report_spool(base_calendar.virtual_report_spool):
finally:
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
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
class report_xml_action(orm.Model):
_name = 'printing.report.xml.action'
_description = 'Report Printing Actions'
_columns = {
'report_id': fields.many2one('ir.actions.report.xml', 'Report', required=True, ondelete='cascade'),
'user_id': fields.many2one('res.users', 'User', required=True, ondelete='cascade'),
'action': fields.selection(_available_action_types, 'Action', required=True),
'report_id': fields.many2one('ir.actions.report.xml', 'Report',
required=True, ondelete='cascade'),
'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'),
}
}
def behaviour(self, cr, uid, act_id, context=None):
result = {}
if not act_id:
return False
action = self.browse(cr, uid, act_id, context=context)
return {
'action': action.action,
'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):
"""
Users
"""
@@ -41,8 +42,8 @@ class res_users(orm.Model):
if x[0] != 'user_default']
_columns = {
'printing_action': fields.selection(_user_available_action_types, 'Printing Action'),
'printing_printer_id': fields.many2one('printing.printer', 'Default Printer'),
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
'printing_action': fields.selection(_user_available_action_types,
'Printing Action'),
'printing_printer_id': fields.many2one('printing.printer',
'Default Printer'),
}

View File

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