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

75 lines
2.5 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.
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 ...config import CONFIG
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,
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
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
2013-08-16 11:08:44 +02:00
default = None
format_ = Transaction().context.pop('%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:
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
2013-09-06 22:32:37 +02:00
@staticmethod
def sql_format(value):
if isinstance(value, (Query, Expression)):
return value
db_type = CONFIG['db_type']
if db_type == 'postgresql' and value is not None:
import psycopg2
return psycopg2.Binary(value)
return value
def sql_type(self):
db_type = CONFIG['db_type']
if db_type == 'postgresql':
return SQLType('BYTEA', 'BYTEA')
elif db_type == 'mysql':
return SQLType('LONGBLOB', 'LONGBLOB')
return SQLType('BLOB', 'BLOB')