Author: phd
Date: 2005-01-05 14:45:15 +0000 (Wed, 05 Jan 2005)
New Revision: 517
Modified:
home/phd/SQLObject/inheritance/sqlobject/col.py
home/phd/SQLObject/inheritance/sqlobject/include/validators.py
home/phd/SQLObject/inheritance/tests/test_sqlobject.py
Log:
Merged patches from revisions 509:516
Modified: home/phd/SQLObject/inheritance/sqlobject/col.py
===================================================================
--- home/phd/SQLObject/inheritance/sqlobject/col.py 2005-01-05 14:40:38 UTC (rev 516)
+++ home/phd/SQLObject/inheritance/sqlobject/col.py 2005-01-05 14:45:15 UTC (rev 517)
@@ -346,31 +346,67 @@
def fromPython(self, value, state):
if value is None:
return None
+ if isinstance(value, str):
+ return value
return value.encode(self.db_encoding)
def toPython(self, value, state):
if value is None:
return None
+ if isinstance(value, unicode):
+ return value
return unicode(value, self.db_encoding)
class SOUnicodeCol(SOStringCol):
+ validatorClass = UnicodeStringValidator # can be overriden in descendants
def __init__(self, **kw):
self.dbEncoding = popKey(kw, 'dbEncoding', 'UTF-8')
SOStringCol.__init__(self, **kw)
- self.validator = validators.All.join(
- UnicodeStringValidator(self.dbEncoding), self.validator)
+ self.validator = validators.All.join(self.createValidator(), self.validator)
+ def createValidator(self):
+ """Create a validator for the column. Can be overriden in descendants."""
+ return self.validatorClass(self.dbEncoding)
+
class UnicodeCol(Col):
baseClass = SOUnicodeCol
+
+class IntValidator(validators.Validator):
+
+ def fromPython(self, value, state):
+ if value is None:
+ return None
+ if not isinstance(value, (int, long, sqlbuilder.SQLExpression)):
+ raise validators.InvalidField("expected an int in the IntCol '%s', got %s instead" % \
+ (self.name, type(value)), value, state)
+ return value
+
+ def toPython(self, value, state):
+ if value is None:
+ return None
+ if not isinstance(value, (int, long, sqlbuilder.SQLExpression)):
+ raise validators.InvalidField("expected an int in the IntCol '%s', got %s instead" % \
+ (self.name, type(value)), value, state)
+ return value
+
class SOIntCol(SOCol):
+ validatorClass = IntValidator # can be overriden in descendants
# 3-03 @@: support precision, maybe max and min directly
+ def __init__(self, **kw):
+ SOCol.__init__(self, **kw)
+ self.validator = validators.All.join(self.createValidator(), self.validator)
+
def autoConstraints(self):
return [consts.isInt]
+ def createValidator(self):
+ """Create a validator for the column. Can be overriden in descendants."""
+ return self.validatorClass(name=self.name)
+
def _sqlType(self):
return 'INT'
@@ -392,14 +428,19 @@
return sqlbuilder.TRUE
class SOBoolCol(SOCol):
+ validatorClass = BoolValidator # can be overriden in descendants
def __init__(self, **kw):
SOCol.__init__(self, **kw)
- self.validator = validators.All.join(BoolValidator(), self.validator)
+ self.validator = validators.All.join(self.createValidator(), self.validator)
def autoConstraints(self):
return [consts.isBool]
+ def createValidator(self):
+ """Create a validator for the column. Can be overriden in descendants."""
+ return self.validatorClass()
+
def _postgresType(self):
return 'BOOL'
Modified: home/phd/SQLObject/inheritance/sqlobject/include/validators.py
===================================================================
--- home/phd/SQLObject/inheritance/sqlobject/include/validators.py 2005-01-05 14:40:38 UTC (rev 516)
+++ home/phd/SQLObject/inheritance/sqlobject/include/validators.py 2005-01-05 14:45:15 UTC (rev 517)
@@ -224,13 +224,11 @@
self.validators = validators
FancyValidator.__init__(self, ifInvalid=ifInvalid, **kw)
- def _toPython(self, value, state=None):
- return self.attemptConvert(self, value, state,
- toPython)
+ def toPython(self, value, state=None):
+ return self.attemptConvert(value, state, toPython)
- def _fromPython(self, value, state):
- return self.attemptConvert(self, value, state,
- fromPython)
+ def fromPython(self, value, state):
+ return self.attemptConvert(value, state, fromPython)
def matchesProtocol(self, validator, state):
target = validator.protocols
Modified: home/phd/SQLObject/inheritance/tests/test_sqlobject.py
===================================================================
--- home/phd/SQLObject/inheritance/tests/test_sqlobject.py 2005-01-05 14:40:38 UTC (rev 516)
+++ home/phd/SQLObject/inheritance/tests/test_sqlobject.py 2005-01-05 14:45:15 UTC (rev 517)
@@ -908,8 +908,6 @@
'name3', 'x')
t.name3 = 1L
self.assertEqual(t.name3, 1)
- t.name3 = '1'
- self.assertEqual(t.name3, 1)
t.name3 = 0
self.assertEqual(t.name3, 0)
|