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

28 lines
859 B
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
import hashlib
from sql import Query, Expression
2009-02-03 18:58:25 +01:00
from ... import backend
2013-09-06 22:32:37 +02:00
from .field import SQLType
from .char import Char
2009-02-03 18:58:25 +01:00
2013-09-06 22:32:37 +02:00
class Sha(Char):
2009-03-01 13:37:31 +01:00
'''
Define a sha field (``unicode``) of len 40.
'''
2009-02-03 18:58:25 +01:00
_type = 'sha'
2013-09-06 22:32:37 +02:00
def sql_format(self, value):
if value is not None and not isinstance(value, (Query, Expression)):
if isinstance(value, unicode):
value = value.encode('utf-8')
value = hashlib.sha1(value).hexdigest()
return super(Sha, self).sql_format(value)
def sql_type(self):
db_type = backend.name()
2013-09-06 22:32:37 +02:00
if db_type == 'mysql':
return SQLType('CHAR', 'VARCHAR(40)')
return SQLType('VARCHAR', 'VARCHAR(40)')