Skip to content

Commit

Permalink
Merge a431778 into bbd177e
Browse files Browse the repository at this point in the history
  • Loading branch information
yvaucher committed Jul 4, 2014
2 parents bbd177e + a431778 commit 11bad50
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 117 deletions.
2 changes: 1 addition & 1 deletion base_report_to_printer/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
'author': 'Agile Business Group & Domsense, Pegueroles SCP, NaN',
'website': 'http://www.agilebg.com',
'license': 'AGPL-3',
"depends" : ['base', 'base_calendar'],
"depends": ['base', 'base_calendar'],
'data': [
'security/security.xml',
'printing_data.xml',
Expand Down
24 changes: 14 additions & 10 deletions base_report_to_printer/ir_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@

from openerp.osv import orm, fields

#
# Reports
#

class report_xml(orm.Model):

"""
Reports
"""

def set_print_options(self, cr, uid, report_id, format, context=None):
"""
Expand Down Expand Up @@ -79,7 +78,10 @@ def print_direct(self, cr, uid, report_id, result, format, printer, context=None
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'),
'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):
Expand All @@ -105,16 +107,18 @@ def behaviour(self, cr, uid, ids, context=None):
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

# Retrieve report-user specific values
act_ids = printing_act_obj.search(cr, uid,
[('report_id', '=', report.id),
('user_id', '=', uid),
('action', '!=', 'user_default')], context=context)
act_ids = printing_act_obj.search(
cr, uid,
[('report_id', '=', report.id),
('user_id', '=', uid),
('action', '!=', 'user_default')], context=context)
if act_ids:
user_action = printing_act_obj.behaviour(cr, uid, act_ids[0], context)
action = user_action['action']
Expand Down
95 changes: 62 additions & 33 deletions base_report_to_printer/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#
##############################################################################
import time
import base64

import cups
from threading import Thread
Expand All @@ -32,25 +31,53 @@
from openerp import pooler
from openerp.osv import orm, fields
from openerp.tools.translate import _
from openerp.addons.base_calendar import base_calendar


#
# Printers
#
class printing_printer(orm.Model):
"""
Printers
"""
_name = "printing.printer"
_description = "Printer"

_columns = {
'name' : fields.char('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),
'name': fields.char(
'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"
Expand Down Expand Up @@ -78,9 +105,9 @@ def update_printers_status(self, db_name, uid, context=None):
server_error = True

mapping = {
3 : 'available',
4 : 'printing',
5 : 'error'
3: 'available',
4: 'printing',
5: 'error'
}

if context is None:
Expand All @@ -96,7 +123,7 @@ def update_printers_status(self, db_name, uid, context=None):
status = 'server-error'
elif printer.system_name in printers:
info = printers[printer.system_name]
status = mapping.get( info['printer-state'], 'unknown' )
status = mapping.get(info['printer-state'], 'unknown')
vals = {
'model': info.get('printer-make-and-model', False),
'location': info.get('printer-location', False),
Expand All @@ -117,7 +144,6 @@ def update_printers_status(self, db_name, uid, context=None):
self.updating = False
self.last_update = time.time()


def start_printer_update(self, cr, uid, context):
self.lock.acquire()
if self.updating:
Expand All @@ -140,7 +166,7 @@ def update(self, cr, uid, context=None):
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
for x in range(0,5):
for x in range(0, 5):
time.sleep(1)
self.lock.acquire()
updating = self.updating
Expand All @@ -149,28 +175,32 @@ def update(self, cr, uid, context=None):
break
return True

def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
def search(self, cr, uid, args, offset=0, limit=None, order=None,
context=None, count=False):
self.update(cr, uid, context)
return super(printing_printer,self).search(cr, uid, args, offset, limit, order, context, count)
return super(printing_printer, self
).search(cr, uid, args, offset,
limit, order, context, count)

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)
return super(printing_printer, self
).read(cr, uid, ids, fields, context, load)

def browse(self, cr, uid, ids, context=None):
self.update(cr, uid, context)
return super(printing_printer,self).browse(cr, uid, ids, context)
return super(printing_printer, self).browse(cr, uid, ids, context)

def set_default(self, cr, uid, ids, context):
if not ids:
return
default_ids= self.search(cr, uid,[('default','=',True)])
self.write(cr, uid, default_ids, {'default':False}, context)
self.write(cr, uid, ids[0], {'default':True}, context)
default_ids = self.search(cr, uid, [('default', '=', True)])
self.write(cr, uid, default_ids, {'default': False}, context)
self.write(cr, uid, ids[0], {'default': True}, context)
return True

def get_default(self,cr,uid,context):
printer_ids = self.search(cr, uid,[('default','=',True)])
def get_default(self, cr, uid, context):
printer_ids = self.search(cr, uid, [('default', '=', True)])
if printer_ids:
return printer_ids[0]
return False
Expand All @@ -182,11 +212,12 @@ def get_default(self,cr,uid,context):

def _available_action_types(self, cr, uid, context=None):
return [
('server',_('Send to Printer')),
('client',_('Send to Client')),
('user_default',_("Use user's defaults")),
('server', _('Send to Printer')),
('client', _('Send to Client')),
('user_default', _("Use user's defaults")),
]


class printing_action(orm.Model):
_name = 'printing.action'
_description = 'Print Job Action'
Expand All @@ -195,5 +226,3 @@ class printing_action(orm.Model):
'name': fields.char('Name', size=256, required=True),
'type': fields.selection(_available_action_types, 'Type', required=True),
}

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
19 changes: 12 additions & 7 deletions base_report_to_printer/report_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from openerp import pooler
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):
Expand All @@ -41,22 +42,26 @@ def exp_report_get(self, db, uid, report_id):
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'])])
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
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)):
report_obj.print_direct(cr, uid, report.id, base64.encodestring(self._reports[report_id]['result']),
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']),
self._reports[report_id]['format'], printer)
# XXX "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
#raise osv.except_osv(_('Printing...'), _('Document sent to printer %s') % (printer,))
# raise osv.except_osv(
# _('Printing...'),
# _('Document sent to printer %s') % (printer,))

except:
cr.rollback()
Expand Down
12 changes: 7 additions & 5 deletions base_report_to_printer/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@

from printing import _available_action_types

#
# Users
#

class res_users(orm.Model):
"""
Users
"""
_name = "res.users"
_inherit = "res.users"

def _user_available_action_types(self, cr, uid, context=None):
if context is None:
context={}
return [x for x in _available_action_types(self, cr, uid, context) if x[0] != 'user_default']
context = {}
return [x for x in _available_action_types(self, cr, uid, context)
if x[0] != 'user_default']

_columns = {
'printing_action': fields.selection(_user_available_action_types, 'Printing Action'),
Expand Down
2 changes: 1 addition & 1 deletion base_report_to_printer/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
##############################################################################
#
#
# 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 Domsense srl (<http://www.domsense.com>)
Expand Down
8 changes: 5 additions & 3 deletions base_report_to_printer/wizard/update_printers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
##############################################################################
#
#
# 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 Domsense srl (<http://www.domsense.com>)
Expand Down Expand Up @@ -37,14 +37,16 @@ def action_cancel(self, cr, uid, ids, context=None):

def action_ok(self, cr, uid, ids, context=None):
# Update Printers
printer_obj = self.pool['printing.printer']
try:
connection = cups.Connection()
printers = connection.getPrinters()
except:
return {}

ids = self.pool.get('printing.printer').search(cr, uid, [('system_name','in',printers.keys())], context=context)
for printer in self.pool.get('printing.printer').browse(cr, uid, ids, context=context):
ids = printer_obj.search(
cr, uid, [('system_name', 'in', printers.keys())], context=context)
for printer in printer_obj.browse(cr, uid, ids, context=context):
del printers[printer.system_name]

for name in printers:
Expand Down
1 change: 0 additions & 1 deletion pingen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@
import pingen
import pingen_document
import res_company

0 comments on commit 11bad50

Please sign in to comment.