~anybox/aeroo/openerp6

« back to all changes in this revision

Viewing changes to report_aeroo_direct_print/installer.py

  • Committer: root
  • Date: 2013-05-16 15:46:46 UTC
  • Revision ID: root@erp.kndati.lv-20130516154646-5lesr8tyzl1vdc0k
1.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2008-2013 Alistek Ltd (http://www.alistek.com) All Rights Reserved.
 
5
#                    General contacts <info@alistek.com>
 
6
#
 
7
# WARNING: This program as such is intended to be used by professional
 
8
# programmers who take the whole responsability of assessing all potential
 
9
# consequences resulting from its eventual inadequacies and bugs
 
10
# End users who are looking for a ready-to-use solution with commercial
 
11
# garantees and support are strongly adviced to contract a Free Software
 
12
# Service Company
 
13
#
 
14
# This program is Free Software; you can redistribute it and/or
 
15
# modify it under the terms of the GNU General Public License
 
16
# as published by the Free Software Foundation; either version 3
 
17
# of the License, or (at your option) any later version.
 
18
#
 
19
# This module is GPLv3 or newer and incompatible
 
20
# with OpenERP SA "AGPL + Private Use License"!
 
21
#
 
22
# This program is distributed in the hope that it will be useful,
 
23
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
24
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
25
# GNU General Public License for more details.
 
26
#
 
27
# You should have received a copy of the GNU General Public License
 
28
# along with this program; if not, write to the Free Software
 
29
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
30
#
 
31
##############################################################################
 
32
 
 
33
from osv import fields
 
34
from osv import osv
 
35
import tools
 
36
import os, base64
 
37
import cups
 
38
import urllib2
 
39
 
 
40
_url = 'http://www.alistek.com/aeroo_banner/v6_0_report_aeroo_direct_print.png'
 
41
 
 
42
class aeroo_printer_installer(osv.osv_memory):
 
43
    _name = 'aeroo_printer.installer'
 
44
    _inherit = 'res.config.installer'
 
45
    _logo_image = None
 
46
 
 
47
    def _get_image(self, cr, uid, context=None):
 
48
        if self._logo_image:
 
49
            return self._logo_image
 
50
        try:
 
51
            im = urllib2.urlopen(_url.encode("UTF-8"))
 
52
            if im.headers.maintype!='image':
 
53
                raise TypeError(im.headers.maintype)
 
54
        except Exception, e:
 
55
            path = os.path.join('report_aeroo','config_pixmaps','module_banner.png')
 
56
            image_file = file_data = tools.file_open(path,'rb')
 
57
            try:
 
58
                file_data = image_file.read()
 
59
                self._logo_image = base64.encodestring(file_data)
 
60
                return self._logo_image
 
61
            finally:
 
62
                image_file.close()
 
63
        else:
 
64
            self._logo_image = base64.encodestring(im.read())
 
65
            return self._logo_image
 
66
 
 
67
    def _get_image_fn(self, cr, uid, ids, name, args, context=None):
 
68
        image = self._get_image(cr, uid, context)
 
69
        return dict.fromkeys(ids, image) # ok to use .fromkeys() as the image is same for all 
 
70
 
 
71
    _columns = {
 
72
        'printer_ids':fields.one2many('aeroo.printers.temp', 'install_id', 'Printers'),
 
73
        'config_logo': fields.function(_get_image_fn, string='Image', type='binary', method=True),
 
74
        'state':fields.selection([
 
75
            ('init','Init'),
 
76
            ('done','Done'),
 
77
            
 
78
        ],'State', select=True, readonly=True),
 
79
    }
 
80
 
 
81
    def default_get(self, cr, uid, fields, context=None):
 
82
        printers_obj = self.pool.get('aeroo.printers')
 
83
        data = super(aeroo_printer_installer, self).default_get(cr, uid, fields, context=context)
 
84
        conn = cups.Connection()
 
85
        printers = conn.getPrinters()
 
86
        installed_ids = printers_obj.search(cr, 1, ['|',('active','=',False),('active','=',True)], context=context)
 
87
        printers_installed = printers_obj.read(cr, uid, installed_ids, context=context)
 
88
        new_printers = list(set(printers.keys()).difference(set(map(lambda p: p['code'], printers_installed))))
 
89
 
 
90
        data['printer_ids'] = []
 
91
        for p in printers_installed:
 
92
            p_temp = p.copy()
 
93
            del p_temp['id']
 
94
            del p_temp['group_ids']
 
95
            del p_temp['active']
 
96
            p_temp['state'] = 'connected'
 
97
            data['printer_ids'].append(p_temp)
 
98
 
 
99
        for new_p in new_printers:
 
100
            note = '\n'.join(map(lambda key: "%s: %s" % (key, printers[new_p][key]), printers[new_p]))
 
101
            p_temp = {'name':printers[new_p]['printer-info'],'code':new_p,'state':'new','note':note}
 
102
            data['printer_ids'].append(p_temp)
 
103
 
 
104
        data.update(data)
 
105
        return data
 
106
 
 
107
    def process(self, cr, uid, ids, context=None):
 
108
        printers_obj = self.pool.get('aeroo.printers')
 
109
        this = self.browse(cr, uid, ids[0], context=context)
 
110
        conn_printers = []
 
111
        for printer in this.printer_ids:
 
112
            if printer.state=='new' and not printers_obj.search(cr, uid, [('code','=',printer.code)], context=context):
 
113
                printers_obj.create(cr, uid, {'name':printer.name,
 
114
                                              'code':printer.code,
 
115
                                              'note':printer.note,
 
116
                                             }, context=context)
 
117
                conn_printers.append((1, printer.id, {'state':'connected'}))
 
118
 
 
119
        return this.write({'state':'done','printer_ids':conn_printers})
 
120
 
 
121
    _defaults = {
 
122
        'state':'init',
 
123
        'config_logo': _get_image,
 
124
    }
 
125
 
 
126
aeroo_printer_installer()
 
127
 
 
128
class aeroo_printers_temp(osv.osv_memory):
 
129
    _name = 'aeroo.printers.temp'
 
130
 
 
131
    _columns = {
 
132
        'name':fields.char('Description', size=256, required=True),
 
133
        'code':fields.char('Name', size=64, required=True),
 
134
        'note': fields.text('Details'),
 
135
        'install_id':fields.many2one('aeroo_printer.installer', 'Install Id'),
 
136
        'state':fields.selection([
 
137
            ('connected','Connected'),
 
138
            ('new','New'),
 
139
            
 
140
        ],'State', select=True, readonly=True),
 
141
    }
 
142
    
 
143
aeroo_printers_temp()
 
144