fixed problem with computed fields
This commit is contained in:
@@ -1 +1 @@
|
|||||||
Version 2.00.1 (2012-08-29 15:42:48) rc4
|
Version 2.00.1 (2012-08-29 16:05:38) rc4
|
||||||
|
|||||||
+8
-4
@@ -7673,21 +7673,25 @@ class Table(object):
|
|||||||
to_compute.append((name,ofield))
|
to_compute.append((name,ofield))
|
||||||
# if field is required, check its default value
|
# if field is required, check its default value
|
||||||
elif not update and not ofield.default is None:
|
elif not update and not ofield.default is None:
|
||||||
new_fields[name] = (ofield,ofield.default)
|
value = ofield.default
|
||||||
|
fields[name] = value
|
||||||
|
new_fields[name] = (ofield,value)
|
||||||
# if this is an update, user the update field instead
|
# if this is an update, user the update field instead
|
||||||
elif update and not ofield.update is None:
|
elif update and not ofield.update is None:
|
||||||
new_fields[name] = (ofield,ofield.update)
|
value = ofield.update
|
||||||
|
fields[name] = value
|
||||||
|
new_fields[name] = (ofield,value)
|
||||||
# if the field is still not there but it should, error
|
# if the field is still not there but it should, error
|
||||||
elif not update and ofield.required:
|
elif not update and ofield.required:
|
||||||
raise RuntimeError, \
|
raise RuntimeError, \
|
||||||
'Table: missing required field: %s' % name
|
'Table: missing required field: %s' % name
|
||||||
# now deal with fields that are supposed to be computed
|
# now deal with fields that are supposed to be computed
|
||||||
if to_compute:
|
if to_compute:
|
||||||
dummyrow = Row(new_fields)
|
row = Row(fields)
|
||||||
for name,ofield in to_compute:
|
for name,ofield in to_compute:
|
||||||
# try compute it
|
# try compute it
|
||||||
try:
|
try:
|
||||||
new_fields[name] = (ofield,ofield.compute(dummyrow))
|
new_fields[name] = (ofield,ofield.compute(row))
|
||||||
except (KeyError, AttributeError):
|
except (KeyError, AttributeError):
|
||||||
# error sinlently unless field is required!
|
# error sinlently unless field is required!
|
||||||
if ofield.required:
|
if ofield.required:
|
||||||
|
|||||||
@@ -515,6 +515,20 @@ class TestVirtualFields(unittest.TestCase):
|
|||||||
db.t.drop()
|
db.t.drop()
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
|
class TestComputedFields(unittest.TestCase):
|
||||||
|
|
||||||
|
def testRun(self):
|
||||||
|
db = DAL('sqlite:memory:')
|
||||||
|
db.define_table('t',
|
||||||
|
Field('a'),
|
||||||
|
Field('b',default='x'),
|
||||||
|
Field('c',compute=lambda r: r.a+r.b))
|
||||||
|
db.commit()
|
||||||
|
id = db.t.insert(a="z")
|
||||||
|
self.assertEqual(db.t[id].c,'zx')
|
||||||
|
db.t.drop()
|
||||||
|
db.commit()
|
||||||
|
|
||||||
class TestImportExportFields(unittest.TestCase):
|
class TestImportExportFields(unittest.TestCase):
|
||||||
|
|
||||||
def testRun(self):
|
def testRun(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user