~vauxoo/addons-vauxoo/6.0-trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#########################################################################
# Copyright (C) 2009  Sharoon Thomas, Open Labs Business solutions      #
#                                                                       #
# This program is free software: you can redistribute it and/or modify   #
# it under the terms of the GNU General Public License as published by   #
# the Free Software Foundation, either version 3 of the License, or      #
#(at your option) any later version.                                    #
#                                                                       #
# This program is distributed in the hope that it will be useful,        #
# but WITHOUT ANY WARRANTY; without even the implied warranty of         #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          #
# GNU General Public License for more details.                           #
#                                                                       #
# You should have received a copy of the GNU General Public License      #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.  #
#########################################################################
from openerp.osv import osv, fields
import base64
import urllib


class product_images(osv.Model):

    "Products Image gallery"
    _name = "product.images"
    _description = __doc__
    _table = "product_images"

    def get_image(self, cr, uid, id):
        each = self.read(cr, uid, id, ['link', 'filename', 'image'])
        if each['link']:
            try:
                (filename, header) = urllib.urlretrieve(each['filename'])
                f = open(filename, 'rb')
                img = base64.encodestring(f.read())
                f.close()
            except:
                img = ''
        else:
            img = each['image']
        return img

    def _get_image(self, cr, uid, ids, field_name, arg, context={}):
        res = {}
        for each in ids:
            res[each] = self.get_image(cr, uid, each)
        return res

    _columns = {
        'name': fields.char('Image Title', size=100, required=True),
        'link': fields.boolean('Link?',
                               help="""Images can be linked from files on your
                                       file system or remote (Preferred)"""),
        'image': fields.binary('Image', filters='*.png,*.jpg,*.gif'),
        'filename': fields.char('File Location', size=250),
        'preview': fields.function(_get_image, type="binary", method=True),
        'comments': fields.text('Comments'),
        'product_id': fields.many2one('product.product', 'Product')
    }

    _defaults = {
        'link': lambda *a: True,
    }