From e8b3994f4efba2ddf5d7cd040bc3dbe2b0c480b3 Mon Sep 17 00:00:00 2001 From: Chen Houwu Date: Thu, 11 Apr 2013 19:54:38 +0800 Subject: [PATCH 1/5] feature: field lookup --- src/django_fields/fields.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/django_fields/fields.py b/src/django_fields/fields.py index c7c97b9..06098dd 100644 --- a/src/django_fields/fields.py +++ b/src/django_fields/fields.py @@ -79,6 +79,14 @@ def get_db_prep_value(self, value, connection=None, prepared=False): value = self.prefix + binascii.b2a_hex(self.cipher.encrypt(value)) return value + def get_db_prep_lookup(self, lookup_type, value, connection=None, prepared=False): + if lookup_type == 'exact': + return self.get_db_prep_value(value) + elif lookup_type == 'in': + return [self.get_db_prep_value(v) for v in value] + else: + raise TypeError('Lookup type %r not supported.' % lookup_type) + class EncryptedTextField(BaseEncryptedField): __metaclass__ = models.SubfieldBase From c13ef48ad1aa44b6d43c35c63b3bb0dedf28501f Mon Sep 17 00:00:00 2001 From: Chen Houwu Date: Thu, 11 Apr 2013 21:27:01 +0800 Subject: [PATCH 2/5] modify padding, so that it can be query for match --- src/django_fields/fields.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/django_fields/fields.py b/src/django_fields/fields.py index 06098dd..9db43ed 100644 --- a/src/django_fields/fields.py +++ b/src/django_fields/fields.py @@ -74,19 +74,12 @@ def get_db_prep_value(self, value, connection=None, prepared=False): if value is not None and not self._is_encrypted(value): padding = self._get_padding(value) if padding > 0: - value += "\0" + ''.join([random.choice(string.printable) + #value += "\0" + ''.join([random.choice(string.printable) + value += "\0" + ''.join(['\1' for index in range(padding-1)]) value = self.prefix + binascii.b2a_hex(self.cipher.encrypt(value)) return value - def get_db_prep_lookup(self, lookup_type, value, connection=None, prepared=False): - if lookup_type == 'exact': - return self.get_db_prep_value(value) - elif lookup_type == 'in': - return [self.get_db_prep_value(v) for v in value] - else: - raise TypeError('Lookup type %r not supported.' % lookup_type) - class EncryptedTextField(BaseEncryptedField): __metaclass__ = models.SubfieldBase From 4b36f3158f88f4dc6fba7d6226726c4dcd7bf343 Mon Sep 17 00:00:00 2001 From: Chen Houwu Date: Mon, 15 Apr 2013 19:16:55 +0800 Subject: [PATCH 3/5] fix: supporting null for number field --- src/django_fields/fields.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/django_fields/fields.py b/src/django_fields/fields.py index 9db43ed..2801bfd 100644 --- a/src/django_fields/fields.py +++ b/src/django_fields/fields.py @@ -58,7 +58,6 @@ def _get_padding(self, value): mod = (len(value) + 2) % self.cipher.block_size return self.cipher.block_size - mod + 2 - def to_python(self, value): if self._is_encrypted(value): return force_unicode( @@ -69,6 +68,9 @@ def to_python(self, value): return value def get_db_prep_value(self, value, connection=None, prepared=False): + if value is None: + return None + value = smart_str(value) if value is not None and not self._is_encrypted(value): @@ -200,8 +202,10 @@ def to_python(self, value): number = self.number_type(number_text) return number - # def get_prep_value(self, value): def get_db_prep_value(self, value, connection=None, prepared=False): + if value is None: + return None + number_text = self.format_string % value return super(BaseEncryptedNumberField, self).get_db_prep_value( number_text, From 8a16c35c4ba796d55b0405b9799de1198b445bab Mon Sep 17 00:00:00 2001 From: Chen Houwu Date: Mon, 15 Apr 2013 19:55:34 +0800 Subject: [PATCH 4/5] fix --- src/django_fields/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/django_fields/fields.py b/src/django_fields/fields.py index 2801bfd..635285a 100644 --- a/src/django_fields/fields.py +++ b/src/django_fields/fields.py @@ -195,7 +195,7 @@ def get_internal_type(self): def to_python(self, value): # value is either an int or a string of an integer - if isinstance(value, self.number_type): + if isinstance(value, self.number_type) or value is None: number = value else: number_text = super(BaseEncryptedNumberField, self).to_python(value) From a4051bcf5f80252231ab4cd043c39a0ea15c3233 Mon Sep 17 00:00:00 2001 From: Chen Houwu Date: Mon, 22 Apr 2013 08:51:33 +0800 Subject: [PATCH 5/5] fix: supporting null for number field --- src/django_fields/fields.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/django_fields/fields.py b/src/django_fields/fields.py index 635285a..834c580 100644 --- a/src/django_fields/fields.py +++ b/src/django_fields/fields.py @@ -76,8 +76,7 @@ def get_db_prep_value(self, value, connection=None, prepared=False): if value is not None and not self._is_encrypted(value): padding = self._get_padding(value) if padding > 0: - #value += "\0" + ''.join([random.choice(string.printable) - value += "\0" + ''.join(['\1' + value += "\0" + ''.join([random.choice(string.printable) for index in range(padding-1)]) value = self.prefix + binascii.b2a_hex(self.cipher.encrypt(value)) return value