From cef7d64d97f65f87a7fa73d8b267eb08ef3f7893 Mon Sep 17 00:00:00 2001 From: mcabo Date: Wed, 21 Aug 2013 11:13:10 +0200 Subject: [PATCH] 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. --- gluon/dal.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/gluon/dal.py b/gluon/dal.py index d1edcd62..f00389b6 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -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)