From 90af2e56980fc47cdafeda315094f191b309275c Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Wed, 5 Mar 2014 17:04:43 +0100 Subject: [PATCH] [gen] More compact code. --- fields/string.py | 15 ++++----------- shared/utils.py | 5 +++++ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/fields/string.py b/fields/string.py index fba7e92..f866cc8 100644 --- a/fields/string.py +++ b/fields/string.py @@ -586,21 +586,14 @@ class String(Field): break if error: return obj.translate('bad_select_value') - accents = {'é':'e','è':'e','ê':'e','ë':'e','à':'a','â':'a','ä':'a', - 'ù':'u','û':'u','ü':'u','î':'i','ï':'i','ô':'o','ö':'o', - 'ç':'c', 'Ç':'C', - 'Ù':'U','Û':'U','Ü':'U','Î':'I','Ï':'I','Ô':'O','Ö':'O', - 'É':'E','È':'E','Ê':'E','Ë':'E','À':'A','Â':'A','Ä':'A'} def applyTransform(self, value): '''Applies a transform as required by self.transform on single value p_value.''' if self.transform in ('uppercase', 'lowercase'): - # For those transforms, I will remove any accent, because - # (1) 'é'.upper() or 'Ê'.lower() has no effect; - # (2) most of the time, if the user wants to apply such effect, it - # is for ease of data manipulation, so I guess without accent. - for c, n in self.accents.iteritems(): - if c in value: value = value.replace(c, n) + # For those transforms, I will remove any accent, because, most of + # the time, if the user wants to apply such effect, it is for ease + # of data manipulation, so I guess without accent. + value = sutils.normalizeString(value, usage='noAccents') # Apply the transform if self.transform == 'lowercase': return value.lower() elif self.transform == 'uppercase': return value.upper() diff --git a/shared/utils.py b/shared/utils.py index 8a033f4..bd7affb 100644 --- a/shared/utils.py +++ b/shared/utils.py @@ -216,6 +216,7 @@ def normalizeString(s, usage='fileName'): * alphanum: it removes any non-alphanumeric char; * alpha: it removes any non-letter char. ''' + strNeeded = isinstance(s, str) # We work in unicode. Convert p_s to unicode if not unicode. if isinstance(s, str): s = s.decode('utf-8') elif not isinstance(s, unicode): s = unicode(s) @@ -236,8 +237,12 @@ def normalizeString(s, usage='fileName'): res = '' for char in s: if rex.match(char): res += char + elif usage == 'noAccents': + res = s else: res = s + # Re-code the result as a str if a str was given. + if strNeeded: res = res.encode('utf-8') return res def normalizeText(s):