From 8f98d61a3879176aeff3b9a87243b63cd6fded4f Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 28 Dec 2016 16:42:09 -0600 Subject: [PATCH] Tweak default "numeric" grid filter, to ignore UPC-like values Those tend to cause SQL errors, so just "fail silently" for now. This situation needs to be improved on the front end at some point... --- tailbone/newgrids/filters.py | 48 +++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/tailbone/newgrids/filters.py b/tailbone/newgrids/filters.py index d6cf51cd..fa8616b7 100644 --- a/tailbone/newgrids/filters.py +++ b/tailbone/newgrids/filters.py @@ -366,12 +366,48 @@ class AlchemyNumericFilter(AlchemyGridFilter): """ value_renderer_factory = NumericValueRenderer - def default_verbs(self): - """ - Expose greater-than / less-than verbs in addition to core. - """ - return ['equal', 'not_equal', 'greater_than', 'greater_equal', - 'less_than', 'less_equal', 'is_null', 'is_not_null'] + # expose greater-than / less-than verbs in addition to core + default_verbs = ['equal', 'not_equal', 'greater_than', 'greater_equal', + 'less_than', 'less_equal', 'is_null', 'is_not_null'] + + # TODO: what follows "works" in that it prevents an error...but from the + # user's perspective it still fails silently...need to improve on front-end + + # try to detect (and ignore) common mistake where user enters UPC as search + # term for integer field... + + def value_invalid(self, value): + return bool(value and len(value) > 8) + + def filter_equal(self, query, value): + if self.value_invalid(value): + return query + return super(AlchemyNumericFilter, self).filter_equal(query, value) + + def filter_not_equal(self, query, value): + if self.value_invalid(value): + return query + return super(AlchemyNumericFilter, self).filter_not_equal(query, value) + + def filter_greater_than(self, query, value): + if self.value_invalid(value): + return query + return super(AlchemyNumericFilter, self).filter_greater_than(query, value) + + def filter_greater_equal(self, query, value): + if self.value_invalid(value): + return query + return super(AlchemyNumericFilter, self).filter_greater_equal(query, value) + + def filter_less_than(self, query, value): + if self.value_invalid(value): + return query + return super(AlchemyNumericFilter, self).filter_less_than(query, value) + + def filter_less_equal(self, query, value): + if self.value_invalid(value): + return query + return super(AlchemyNumericFilter, self).filter_less_equal(query, value) class AlchemyBooleanFilter(AlchemyGridFilter):