From 4334ae38ed2076e639246d800f732a9373d2b197 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Sun, 17 Feb 2013 01:00:01 -0600 Subject: [PATCH] fixed issue 1339, faster IS_NOT_IN_DB --- VERSION | 2 +- gluon/validators.py | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/VERSION b/VERSION index 5dec942e..5a07c73b 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.4.1-alpha.2+timestamp.2013.02.16.09.59.52 +Version 2.4.1-alpha.2+timestamp.2013.02.17.00.59.22 diff --git a/gluon/validators.py b/gluon/validators.py index 7cdd0649..1d3151b0 100644 --- a/gluon/validators.py +++ b/gluon/validators.py @@ -629,14 +629,18 @@ class IS_NOT_IN_DB(Validator): (tablename, fieldname) = str(self.field).split('.') table = self.dbset.db[tablename] field = table[fieldname] - rows = self.dbset(field == value, ignore_common_filters=self.ignore_common_filters).select(field, limitby=(0, 1)) - if len(rows) > 0: - if isinstance(self.record_id, dict): - for f in self.record_id: - if str(getattr(rows[0], f)) != str(self.record_id[f]): - return (value, translate(self.error_message)) - elif str(rows[0][table._id.name]) != str(self.record_id): - return (value, translate(self.error_message)) + subset = self.dbset(field == value, + ignore_common_filters=self.ignore_common_filters) + id = self.record_id + if isinstance(id, dict): + fields = [table[f] for f in id] + row = subset.select(*fields, **dict(limitby=(0, 1))).first() + if row and any(str(row[f]) != str(id[f]) for f in id): + return (value, translate(self.error_message)) + else: + row = subset.select(table._id, limitby=(0, 1)).first() + if row and str(row.id) != str(id): + return (value, translate(self.error_message)) return (value, None)