From bbb87882760da7bd7ae84df895fd6db9701af506 Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Sat, 27 Jan 2018 00:07:28 +0100 Subject: [PATCH] Meilleure gestion du AESField --- preferences/aes_field.py | 20 +++++++++++++++----- re2o/templatetags/self_adhesion.py | 2 ++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/preferences/aes_field.py b/preferences/aes_field.py index 76712a81..d48e9c4a 100644 --- a/preferences/aes_field.py +++ b/preferences/aes_field.py @@ -27,6 +27,7 @@ def encrypt(key, s): def decrypt(key, s): obj = AES.new(key) ss = obj.decrypt(s) + print(ss) return ss.split(bytes(EOD, 'utf-8'))[0] @@ -35,10 +36,6 @@ class AESEncryptedField(models.CharField): setattr(instance, self.name, binascii.b2a_base64(encrypt(settings.AES_KEY, data))) - def value_from_object(self, obj): - return decrypt(settings.AES_KEY, - binascii.a2b_base64(getattr(obj, self.attname))).decode('utf-8') - def to_python(self, value): if value is None: return None @@ -46,8 +43,21 @@ class AESEncryptedField(models.CharField): binascii.a2b_base64(value)).decode('utf-8') def from_db_value(self, value, expression, connection, *args): - + print('from db') + print(value) if value is None: return value return decrypt(settings.AES_KEY, binascii.a2b_base64(value)).decode('utf-8') + + def get_prep_value(self, value): + print('get prep value') + print(value) + print(binascii.b2a_base64(encrypt( + settings.AES_KEY, + value + ))) + return binascii.b2a_base64(encrypt( + settings.AES_KEY, + value + )) diff --git a/re2o/templatetags/self_adhesion.py b/re2o/templatetags/self_adhesion.py index e940a5a9..e1577a13 100644 --- a/re2o/templatetags/self_adhesion.py +++ b/re2o/templatetags/self_adhesion.py @@ -21,7 +21,9 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from django import template from preferences.models import OptionalUser, GeneralOption + register = template.Library() + @register.simple_tag def self_adhesion(): options, _created = OptionalUser.objects.get_or_create()