Files
tryton/trytond/model/fields/binary.py
T

55 lines
1.9 KiB
Python
Raw Normal View History

2009-02-03 18:58:25 +01:00
#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
2011-09-17 14:43:05 +02:00
from trytond.transaction import Transaction
2009-02-03 18:58:25 +01:00
class Binary(Field):
2009-03-01 13:37:31 +01:00
'''
2009-09-13 15:37:11 +02:00
Define a binary field (``str``).
2009-03-01 13:37:31 +01:00
'''
2009-02-03 18:58:25 +01:00
_type = 'binary'
2011-01-07 10:58:43 +01:00
def __init__(self, string='', help='', required=False, readonly=False,
2011-12-22 22:03:15 +01:00
domain=None, states=None, select=False, on_change=None,
on_change_with=None, depends=None, filename=None, order_field=None,
context=None, loading='lazy'):
2011-02-16 17:50:09 +01:00
if filename is not None:
self.filename = filename
if depends is None:
depends = [filename]
else:
depends.append(filename)
2011-01-07 10:58:43 +01:00
super(Binary, self).__init__(string=string, help=help,
2011-02-16 17:50:09 +01:00
required=required, readonly=readonly, domain=domain, states=states,
2011-12-22 22:03:15 +01:00
select=select, on_change=on_change, on_change_with=on_change_with,
depends=depends, order_field=order_field, context=context,
loading=loading)
2011-01-07 10:58:43 +01:00
2010-02-18 18:50:35 +01:00
@staticmethod
2010-08-19 22:01:12 +02:00
def get(ids, model, name, values=None):
2009-03-01 13:37:31 +01:00
'''
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
2009-09-13 15:37:11 +02:00
:param values: a dictionary with the read values
2009-03-01 13:37:31 +01:00
:return: a dictionary with ids as key and values as value
'''
2009-02-03 18:58:25 +01:00
if values is None:
values = {}
res = {}
2011-09-17 14:43:05 +02:00
converter = buffer
default = False
format_ = Transaction().context.pop('%s.%s' % (model, name), '')
if format_ == 'size':
converter = len
default = 0
2009-02-03 18:58:25 +01:00
for i in values:
2011-09-17 14:43:05 +02:00
res[i['id']] = converter(i[name]) if i[name] else default
2009-02-03 18:58:25 +01:00
for i in ids:
2011-09-17 14:43:05 +02:00
res.setdefault(i, default)
2009-02-03 18:58:25 +01:00
return res