Refactor backend fields

This commit is contained in:
Cédric Krier committed 2009-10-31 19:33:45 +01:00
1 parent 1fd6dc8a1f
commit 74eee1cd20
3 files changed
+166 -176

No files matched your search

+125
View File
@@ -0,0 +1,125 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
try:
import hashlib
except ImportError:
hashlib = None
import sha
class Field(object):
@staticmethod
def sql_format(value):
if value is None or value == False:
return None
elif isinstance(value, str):
return unicode(value, 'utf-8')
elif isinstance(value, unicode):
return value
return unicode(value)
@staticmethod
def sql_type(field):
return None
class Boolean(Field):
@staticmethod
def sql_format(value):
return value and 'True' or 'False'
class Integer(Field):
@staticmethod
def sql_format(value):
return int(value or 0)
class BigInteger(Integer):
pass
class Char(Field):
pass
class Sha(Field):
@staticmethod
def sql_format(value):
if isinstance(value, basestring):
if isinstance(value, unicode):
value = value.encode('utf-8')
if hashlib:
value = hashlib.sha1(value).hexdigest()
else:
value = sha.new(value).hexdigest()
return Field.sql_format(value)
class Text(Field):
pass
class Float(Field):
@staticmethod
def sql_format(value):
return float(value or 0.0)
class Numeric(Float):
pass
class Date(Field):
pass
class DateTime(Field):
pass
class Time(Field):
pass
class Binary(Field):
@staticmethod
def sql_format(value):
return value or None
class Selection(Char):
pass
class Reference(Field):
pass
class Many2One(Field):
@staticmethod
def sql_format(value):
return value and int(value) or None
class One2Many(Field):
pass
class Many2Many(Field):
pass
class Function(Field):
pass
class Property(Function):
pass
+20 -86
View File
@@ -1,61 +1,31 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
try:
import hashlib
except ImportError:
hashlib = None
import sha
import psycopg2
from trytond.backend import fields
class Field(object):
@staticmethod
def sql_format(value):
if value is None or value == False:
return None
elif isinstance(value, str):
return unicode(value, 'utf-8')
elif isinstance(value, unicode):
return value
return unicode(value)
@staticmethod
def sql_type(field):
return None
class Boolean(Field):
@staticmethod
def sql_format(value):
return value and 'True' or 'False'
class Boolean(fields.Boolean):
@staticmethod
def sql_type(field):
return ('bool', 'bool')
class Integer(Field):
@staticmethod
def sql_format(value):
return int(value or 0)
class Integer(fields.Integer):
@staticmethod
def sql_type(field):
return ('int4', 'int4')
class BigInteger(Integer):
class BigInteger(fields.BigInteger):
@staticmethod
def sql_type(field):
return ('int8', 'int8')
class Char(Field):
class Char(fields.Char):
@staticmethod
def sql_type(field):
@@ -64,71 +34,56 @@ class Char(Field):
return ('varchar', 'varchar')
class Sha(Field):
@staticmethod
def sql_format(value):
if isinstance(value, basestring):
if isinstance(value, unicode):
value = value.encode('utf-8')
if hashlib:
value = hashlib.sha1(value).hexdigest()
else:
value = sha.new(value).hexdigest()
return Field.sql_format(value)
class Sha(fields.Sha):
@staticmethod
def sql_type(field):
return ('varchar', 'varchar(40)')
class Text(Field):
class Text(fields.Text):
@staticmethod
def sql_type(field):
return ('text', 'text')
class Float(Field):
@staticmethod
def sql_format(value):
return float(value or 0.0)
class Float(fields.Float):
@staticmethod
def sql_type(field):
return ('float8', 'float8')
class Numeric(Float):
class Numeric(fields.Numeric):
@staticmethod
def sql_type(field):
return ('numeric', 'numeric')
class Date(Field):
class Date(fields.Date):
@staticmethod
def sql_type(field):
return ('date', 'date')
class DateTime(Field):
class DateTime(fields.DateTime):
@staticmethod
def sql_type(field):
return ('timestamp', 'timestamp(0)')
class Time(Field):
class Time(fields.Time):
@staticmethod
def sql_type(field):
return ('time', 'time')
class Binary(Field):
class Binary(fields.Binary):
@staticmethod
def sql_format(value):
@@ -139,47 +94,26 @@ class Binary(Field):
return ('bytea', 'bytea')
class Selection(Char):
class Selection(fields.Selection):
@staticmethod
def sql_type(field):
return ('varchar', 'varchar')
class Reference(Field):
class Reference(fields.Reference):
@staticmethod
def sql_type(field):
return ('varchar', 'varchar')
class Many2One(Field):
@staticmethod
def sql_format(value):
return value and int(value) or None
class Many2One(fields.Many2One):
@staticmethod
def sql_type(field):
return ('int4', 'int4')
class One2Many(Field):
pass
class Many2Many(Field):
pass
class Function(Field):
pass
class Property(Function):
pass
FIELDS = {
'boolean': Boolean,
'integer': Integer,
@@ -196,8 +130,8 @@ FIELDS = {
'selection': Selection,
'reference': Reference,
'many2one': Many2One,
'one2many': One2Many,
'many2many': Many2Many,
'function': Function,
'property': Property,
'one2many': fields.One2Many,
'many2many': fields.Many2Many,
'function': fields.Function,
'property': fields.Property,
}
+21 -90
View File
@@ -1,30 +1,9 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
try:
import hashlib
except ImportError:
hashlib = None
import sha
from trytond.backend import fields
class Field(object):
@staticmethod
def sql_format(value):
if value is None or value == False:
return None
elif isinstance(value, str):
return unicode(value, 'utf-8')
elif isinstance(value, unicode):
return value
return unicode(value)
@staticmethod
def sql_type(field):
return None
class Boolean(Field):
class Boolean(fields.Boolean):
@staticmethod
def sql_format(value):
@@ -35,148 +14,100 @@ class Boolean(Field):
return ('BOOLEAN', 'BOOLEAN')
class Integer(Field):
@staticmethod
def sql_format(value):
return int(value or 0)
class Integer(fields.Integer):
@staticmethod
def sql_type(field):
return ('INTEGER', 'INTEGER')
class BigInteger(Integer):
@staticmethod
def sql_type(field):
return ('INTEGER', 'INTEGER')
class BigInteger(fields.BigInteger, Integer):
pass
class Char(Field):
class Char(fields.Char):
@staticmethod
def sql_type(field):
return ('VARCHAR', 'VARCHAR')
class Sha(Field):
@staticmethod
def sql_format(value):
if isinstance(value, basestring):
if isinstance(value, unicode):
value = value.encode('utf-8')
if hashlib:
value = hashlib.sha1(value).hexdigest()
else:
value = sha.new(value).hexdigest()
return Field.sql_format(value)
class Sha(fields.Sha):
@staticmethod
def sql_type(field):
return ('VARCHAR', 'VARCHAR(40)')
class Text(Field):
class Text(fields.Text):
@staticmethod
def sql_type(field):
return ('TEXT', 'TEXT')
class Float(Field):
@staticmethod
def sql_format(value):
return float(value or 0.0)
class Float(fields.Float):
@staticmethod
def sql_type(field):
return ('FLOAT', 'FLOAT')
class Numeric(Float):
class Numeric(fields.Numeric):
@staticmethod
def sql_type(field):
return ('NUMERIC', 'NUMERIC')
class Date(Field):
class Date(fields.Date):
@staticmethod
def sql_type(field):
return ('DATE', 'DATE')
class DateTime(Field):
class DateTime(fields.DateTime):
@staticmethod
def sql_type(field):
return ('TIMESTAMP', 'TIMESTAMP')
class Time(Field):
class Time(fields.Time):
@staticmethod
def sql_type(field):
return ('TIME', 'TIME')
class Binary(Field):
@staticmethod
def sql_format(value):
return value or None
class Binary(fields.Binary):
@staticmethod
def sql_type(field):
return ('BLOB', 'BLOB')
class Selection(Char):
class Selection(fields.Selection):
@staticmethod
def sql_type(field):
return ('VARCHAR', 'VARCHAR')
class Reference(Field):
class Reference(fields.Reference):
@staticmethod
def sql_type(field):
return ('VARCHAR', 'VARCHAR')
class Many2One(Field):
@staticmethod
def sql_format(value):
return value and int(value) or None
class Many2One(fields.Many2One):
@staticmethod
def sql_type(field):
return ('INTEGER', 'INTEGER')
class One2Many(Field):
pass
class Many2Many(Field):
pass
class Function(Field):
pass
class Property(Function):
pass
FIELDS = {
'boolean': Boolean,
'integer': Integer,
@@ -193,8 +124,8 @@ FIELDS = {
'selection': Selection,
'reference': Reference,
'many2one': Many2One,
'one2many': One2Many,
'many2many': Many2Many,
'function': Function,
'property': Property,
'one2many': fields.One2Many,
'many2many': fields.Many2Many,
'function': fields.Function,
'property': fields.Property,
}