Permalink
Browse files

Merge pull request #45 from ticosax/deconstruct

add support for deconstruct() to be compliant with django1.7
  • Loading branch information...
svetlyak40wt committed Sep 12, 2014
2 parents e046a8c + aaf47d8 commit fc66b526df384542b20057ddcb774b1d5a7a8475
Showing with 16 additions and 2 deletions.
  1. +3 −0 README.md
  2. +13 −2 src/django_fields/fields.py
View
@@ -11,6 +11,9 @@ This project uses Travis for continuous integration: [![Build Status](https://se
ChangeLog
---------
### 0.2.4
* Added: Support for Django migrations (>=1.7) thanks to Field.deconstruct()
### 0.2.3
* Added: Ability to specify `secret_key` as an argument to the field constructor (via [markkennedy](https://github.com/svetlyak40wt/django-fields/pull/40 "Issue #40")).
@@ -61,7 +61,7 @@ def __init__(self, *args, **kwargs):
self.cipher = self.cipher_object.new(self.secret_key)
self.prefix = '$%s$' % self.cipher_type
max_length = kwargs.get('max_length', 40)
self.original_max_length = max_length = kwargs.get('max_length', 40)
self.unencrypted_length = max_length
# always add at least 2 to the max_length:
# one for the null byte, one for padding
@@ -71,7 +71,7 @@ def __init__(self, *args, **kwargs):
max_length += self.cipher.block_size - mod
kwargs['max_length'] = max_length * 2 + len(self.prefix)
models.Field.__init__(self, *args, **kwargs)
super(BaseEncryptedField, self).__init__(*args, **kwargs)
def _is_encrypted(self, value):
return isinstance(value, basestring) and value.startswith(self.prefix)
@@ -121,6 +121,17 @@ def get_db_prep_value(self, value, connection=None, prepared=False):
value = self.prefix + binascii.b2a_hex(self.cipher.encrypt(value))
return value
def deconstruct(self):
original = super(BaseEncryptedField, self).deconstruct()
kwargs = original[-1]
if self.cipher_type != 'AES':
kwargs['cipher'] = self.cipher_type
if self.block_type is not None:
kwargs['block_type'] = self.block_type
if self.original_max_length != 40:
kwargs['max_length'] = self.original_max_length
return original[:-1] + (kwargs,)
class EncryptedTextField(BaseEncryptedField):
__metaclass__ = models.SubfieldBase

0 comments on commit fc66b52

Please sign in to comment.