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

83 lines
2.7 KiB
Python
Raw Normal View History

2014-12-02 12:54:01 +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.
2013-09-06 22:32:37 +02:00
from sql import Query, Expression
2009-02-03 18:58:25 +01:00
2013-09-06 22:32:37 +02:00
from .field import Field, SQLType
from ...transaction import Transaction
from ... import backend
2009-02-03 18:58:25 +01:00
class Binary(Field):
2009-03-01 13:37:31 +01:00
'''
2015-03-16 19:28:34 +01:00
Define a binary field (``bytes``).
2009-03-01 13:37:31 +01:00
'''
2009-02-03 18:58:25 +01:00
_type = 'binary'
2015-03-16 19:28:34 +01:00
cast = bytearray if bytes == str else bytes
2009-02-03 18:58:25 +01:00
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,
2013-09-06 22:32:37 +02:00
on_change_with=None, depends=None, filename=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,
2013-09-06 22:32:37 +02:00
depends=depends, context=context, loading=loading)
2011-01-07 10:58:43 +01:00
2015-03-16 19:28:34 +01:00
@classmethod
def get(cls, ids, model, name, values=None):
2009-03-01 13:37:31 +01:00
'''
2015-03-16 19:28:34 +01:00
Convert the binary value into ``bytes``
2009-03-01 13:37:31 +01:00
: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 = {}
2015-03-16 19:28:34 +01:00
converter = cls.cast
2013-08-16 11:08:44 +02:00
default = None
format_ = Transaction().context.get(
'%s.%s' % (model.__name__, name), '')
2011-09-17 14:43:05 +02:00
if format_ == 'size':
converter = len
default = 0
2009-02-03 18:58:25 +01:00
for i in values:
2015-03-16 20:13:03 +01:00
value = i[name]
if value:
if isinstance(value, unicode):
value = value.encode('utf-8')
value = converter(value)
else:
value = default
res[i['id']] = value
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
2013-09-06 22:32:37 +02:00
@staticmethod
def sql_format(value):
if isinstance(value, (Query, Expression)):
return value
db_type = backend.name()
2013-09-06 22:32:37 +02:00
if db_type == 'postgresql' and value is not None:
import psycopg2
return psycopg2.Binary(value)
return value
def sql_type(self):
db_type = backend.name()
2013-09-06 22:32:37 +02:00
if db_type == 'postgresql':
return SQLType('BYTEA', 'BYTEA')
elif db_type == 'mysql':
return SQLType('LONGBLOB', 'LONGBLOB')
return SQLType('BLOB', 'BLOB')