From 5ac57dfd7954b8b8f3b07c83613b01363f851d2a Mon Sep 17 00:00:00 2001 From: Lorenzo Battistini Date: Tue, 4 Dec 2012 12:05:15 +0100 Subject: [PATCH] [FIX] The close should be done in the finally: clause of a try: except: because if an exception occurs, a postgresql transaction will be leaked. The except clause should properly rollback the cr --- base_report_to_printer/__openerp__.py | 30 +--------- base_report_to_printer/printing.py | 86 +++++++++++++++------------ 2 files changed, 50 insertions(+), 66 deletions(-) diff --git a/base_report_to_printer/__openerp__.py b/base_report_to_printer/__openerp__.py index 5bccd62..d1765f0 100644 --- a/base_report_to_printer/__openerp__.py +++ b/base_report_to_printer/__openerp__.py @@ -25,33 +25,9 @@ 'name': "Report to printer", 'version': '0.1', 'category': 'Generic Modules/Base', - 'description': """ -Extracted from printjob ( http://apps.openerp.com/addon/1727 ), this module allows to send reports to a printer attached to the server. Settings can be configured globaly, per user, per report and per user and report. - -Configuration -============= - -.. image:: http://planet.domsense.com/wp-content/uploads/2011/09/printing-menu.png - :width: 400 px - -First of all, you have to load CUPS printers in OpenERP. You can use a wizard that retrieves them automatically. You just have to click on Update Printers from CUPS and printers will appear within the available printers list. - -In the next step you will configure the reports to send to the printers. - -.. image:: http://planet.domsense.com/wp-content/uploads/2011/09/reports.png - :width: 400 px - -Through the report form you can define the system’s behaviour while producing the report. - -.. image:: http://planet.domsense.com/wp-content/uploads/2011/09/report-configuration.png - :width: 400 px - -You can set a global behaviour, or differentiate it according to the user who’s printing. In the example, the global behaviour defines to send the report to client directly (Send to Client), therefore without sending it to the printer. But if user elbati is printing, the report will be sent to the selected printer (Send to Printer). - -You can also define a default behaviour associated to the user, in order to establish whether a certain user, when not differently set, wants to send the reports always to a specific printer or not. - -After finishing the configuration, you will just have to click on printing button associated to the report (or launch the report by a wizard or whatever) and the system will automatically send the report to the previously set printer. -""", + 'description': """Extracted from printjob ( http://apps.openerp.com/addon/1727 ), this module allows to send reports to a printer attached to the server. Settings can be configured globaly, per user, per report and per user and report. + + For more info: http://planet.domsense.com/en/2011/10/linking-openerp-to-cups-printers-base_report_to_printer/""", 'author': 'Agile Business Group & Domsense, Pegueroles SCP, NaN', 'website': 'http://www.agilebg.com', 'license': 'AGPL-3', diff --git a/base_report_to_printer/printing.py b/base_report_to_printer/printing.py index 1d929c0..1e59684 100755 --- a/base_report_to_printer/printing.py +++ b/base_report_to_printer/printing.py @@ -90,30 +90,34 @@ class printing_printer(osv.osv): 5 : 'error' } + try: # Skip update to avoid the thread being created again - ctx = context.copy() - ctx['skip_update'] = True - ids = self.pool.get('printing.printer').search(cr, uid, [], context=ctx) - for printer in self.pool.get('printing.printer').browse(cr, uid, ids, context=ctx): - 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' + ctx = context.copy() + ctx['skip_update'] = True + ids = self.pool.get('printing.printer').search(cr, uid, [], context=ctx) + for printer in self.pool.get('printing.printer').browse(cr, uid, ids, context=ctx): + 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' - vals['status'] = status - self.pool.get('printing.printer').write(cr, uid, [printer.id], vals, context) - - cr.commit() - cr.close() + vals['status'] = status + self.pool.get('printing.printer').write(cr, uid, [printer.id], vals, context) + cr.commit() + except: + cr.rollback() + raise + finally: + cr.close() self.lock.acquire() self.updating = False self.last_update = time.time() @@ -337,23 +341,27 @@ class virtual_report_spool(base_calendar.virtual_report_spool): def exp_report_get(self, db, uid, report_id): cr = pooler.get_db(db).cursor() - pool = pooler.get_pool(cr.dbname) - - # 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'])]) - if report: - report = report_obj.browse(cr,uid,report[0]) - name = report.name - 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)): - report_obj.print_direct(cr, uid, base64.encodestring(self._reports[report_id]['result']), - self._reports[report_id]['format'], printer) - cr.close() + try: + pool = pooler.get_pool(cr.dbname) + # 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'])]) + if report: + report = report_obj.browse(cr,uid,report[0]) + name = report.name + 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)): + report_obj.print_direct(cr, uid, base64.encodestring(self._reports[report_id]['result']), + self._reports[report_id]['format'], printer) + except: + cr.rollback() + raise + finally: + cr.close() res = super(virtual_report_spool, self).exp_report_get(db, uid, report_id) return res