Skip to content

Commit

Permalink
access model with model attr, default model_name to 'crypt'
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Aug 25, 2016
1 parent 6dbd1f9 commit 6beb500
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 11 deletions.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
include AUTHORS CHANGES README.md LICENCE
recursive-include docs *
recursive-exclude example/*
recursive-exclude etc/*
2 changes: 1 addition & 1 deletion django_crypto_fields/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# from django_crypto_fields.models import Crypt


Crypt = django_apps.get_model(*django_apps.get_app_config('django_crypto_fields').model)
Crypt = django_apps.get_app_config('django_crypto_fields').model


class CryptoFieldsAdminSite(AdminSite):
Expand Down
11 changes: 9 additions & 2 deletions django_crypto_fields/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys

from Crypto.Cipher import AES
from django.apps import apps as django_apps
from django.apps import AppConfig as DjangoAppConfig
from django.core.management.color import color_style
from django_crypto_fields.cryptor import Cryptor
Expand All @@ -16,7 +17,7 @@ class AppConfig(DjangoAppConfig):
name = 'django_crypto_fields'
verbose_name = "Data Encryption"
encryption_keys = None
model = ('django_crypto_fields', 'crypt')
app_label = 'django_crypto_fields'
crypt_model_using = 'default' # change if using more than one database and not 'default'.

def __init__(self, app_label, model_name):
Expand All @@ -38,8 +39,9 @@ def __init__(self, app_label, model_name):
keys.create_keys()
keys.load_keys()
self.encryption_keys = keys
sys.stdout.write(' * using model {}.\n'.format('.'.join(self.model)))
sys.stdout.write(' * using model {}.{}.\n'.format(self.app_label, 'crypt'))
sys.stdout.write(' Done loading {}.\n'.format(self.verbose_name))
sys.stdout.flush()

def ready(self):
cryptor = Cryptor()
Expand All @@ -48,3 +50,8 @@ def ready(self):
'Warning: Encryption mode MODE_CFB should not be used. \n'
' See django_crypto_fields.cryptor.py and comments \n'
' in pycrypto.blockalgo.py.\n'))
sys.stdout.flush()

@property
def model(self):
return django_apps.get_model(self.app_label, 'crypt')
2 changes: 1 addition & 1 deletion django_crypto_fields/field_cryptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __repr__(self):
def cipher_model(self):
"""Returns the cipher model and avoids issues with model loading and field classes."""
if not self._cipher_model:
self._cipher_model = django_apps.get_model(*django_apps.get_app_config('django_crypto_fields').model)
self._cipher_model = django_apps.get_app_config('django_crypto_fields').model
return self._cipher_model

def hash(self, plaintext):
Expand Down
2 changes: 1 addition & 1 deletion django_crypto_fields/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def key_path(self, key_path=None):
key_path = settings.BASE_DIR
sys.stdout.write(style.WARNING(
'Warning! Not ready for production. Setting KEY_PATH to {} '
'for testing purposes.'.format(key_path)))
'for testing purposes.\n'.format(key_path)))
self.using_test_keys = True
except (ImproperlyConfigured, AttributeError):
# your not in Django ...
Expand Down
7 changes: 3 additions & 4 deletions django_crypto_fields/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ def test_exact(self):
TestModel.objects.create(firstname='Erik1', identity='11111111', comment='')
self.assertEqual(1, TestModel.objects.filter(firstname__exact='Erik1').count())

def test_iexact(self):
TestModel.objects.create(firstname='Erik1', identity='11111111', comment='')
# self.assertEqual(1, TestModel.objects.filter(firstname__iexact='Erik1').count())
self.assertEqual(1, TestModel.objects.filter(firstname__iexact='Erik1').count())
# def test_iexact(self):
# TestModel.objects.create(firstname='Erik1', identity='11111111', comment='')
# # self.assertEqual(1, TestModel.objects.filter(firstname__iexact='Erik1').count())

# def test_contains(self):
# TestModel.objects.create(firstname='Erik1', identity='11111111', comment='')
Expand Down
2 changes: 1 addition & 1 deletion example/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ class AppConfig(DjangoAppConfig):


class DjangoCryptoFieldsAppConfig(DjangoCryptoFieldsAppConfigParent):
model = ('example', 'crypt')
app_label = 'example'
1 change: 1 addition & 0 deletions example/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class Crypt (CryptModelMixin, BaseUuidModel):

class Meta:
app_label = 'example'
verbose_name = 'Crypt'
unique_together = (('hash', 'algorithm', 'mode'),)

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
ignore = E226,E302,E41,F401
max-line-length = 120
max-complexity = 10
exclude = django_crypto_fields/tests/*,django_crypto_fields/migrations/*
exclude = django_crypto_fields/tests/*,django_crypto_fields/migrations/*,edc_example/*

0 comments on commit 6beb500

Please sign in to comment.