File: binary.py

package info (click to toggle)
tryton-server 2.2.4-1%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,500 kB
  • sloc: python: 27,407; xml: 4,424; sql: 510; sh: 127; makefile: 79
file content (54 lines) | stat: -rw-r--r-- 2,024 bytes parent folder | download
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
#This file is part of Tryton.  The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.

from trytond.model.fields.field import Field
from trytond.transaction import Transaction


class Binary(Field):
    '''
    Define a binary field (``str``).
    '''
    _type = 'binary'

    def __init__(self, string='', help='', required=False, readonly=False,
            domain=None, states=None, change_default=False, select=False,
            on_change=None, on_change_with=None, depends=None, filename=None,
            order_field=None, context=None, loading='lazy'):
        if filename is not None:
            self.filename = filename
            if depends is None:
                depends = [filename]
            else:
                depends.append(filename)
        super(Binary, self).__init__(string=string, help=help,
            required=required, readonly=readonly, domain=domain, states=states,
            change_default=change_default, select=select, on_change=on_change,
            on_change_with=on_change_with, depends=depends,
            order_field=order_field, context=context, loading=loading)

    @staticmethod
    def get(ids, model, name, values=None):
        '''
        Convert the binary value into ``str``

        :param ids: a list of ids
        :param model: a string with the name of the model
        :param name: a string with the name of the field
        :param values: a dictionary with the read values
        :return: a dictionary with ids as key and values as value
        '''
        if values is None:
            values = {}
        res = {}
        converter = buffer
        default = False
        format_ = Transaction().context.pop('%s.%s' % (model, name), '')
        if format_ == 'size':
            converter = len
            default = 0
        for i in values:
            res[i['id']] = converter(i[name]) if i[name] else default
        for i in ids:
            res.setdefault(i, default)
        return res