Update dal.py

I think It will be useful to update the table contents and to validate the data previously.

On the other hand, I think there is something weird in the update_or_insert definition (Class table) because it is not working properly on data updating.
This commit is contained in:
mcabo
2013-08-21 11:13:10 +02:00
parent 4f1d6e6f31
commit cef7d64d97

View File

@@ -8704,6 +8704,32 @@ class Table(object):
response.id = None
return response
def validate_and_update(self, _key=DEFAULT, **fields):
response = Row()
response.errors = Row()
new_fields = copy.copy(fields)
for key,value in fields.iteritems():
value,error = self[key].validate(value)
if error:
response.errors[key] = "%s" % error
else:
new_fields[key] = value
if _key is DEFAULT:
record = self(**values)
elif isinstance(_key,dict):
record = self(**_key)
else:
record = self(_key)
if not response.errors and record:
row = self._db(self._id==_key)
response.id = row.update(**fields)
else:
response.id = None
return response
def update_or_insert(self, _key=DEFAULT, **values):
if _key is DEFAULT:
record = self(**values)