Skip to content

Commit b206eb7

Browse files
committed
Add SQLite support.
1 parent 457e72c commit b206eb7

File tree

9 files changed

+47
-19
lines changed

9 files changed

+47
-19
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Django model fields whose value is transparently encrypted using the `Fernet
1919
recipe`_ from the `cryptography`_ library.
2020

2121
``django-fernet-fields`` supports `Django`_ 1.8.2 and later on Python 2.7, 3.3,
22-
3.4, pypy, and pypy3. Currently PostgreSQL is the only supported database, but
23-
other backends could easily be supported as well.
22+
3.4, pypy, and pypy3. Currently PostgreSQL and SQLite are the only supported
23+
databases, but support for other backends could easily be added.
2424

2525
.. _Django: http://www.djangoproject.com/
2626
.. _Fernet recipe: https://cryptography.io/en/latest/fernet/

doc/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Prerequisites
1414
``django-fernet-fields`` supports `Django`_ 1.8.2 and later on Python 2.7, 3.3,
1515
3.4, pypy, and pypy3.
1616

17-
PostgreSQL is currently the only database with built-in support; support for
18-
other database backends should be easy to add.
17+
PostgreSQL and SQLite are currently the only databases with built-in support;
18+
support for other database backends should be easy to add.
1919

2020
.. _Django: http://www.djangoproject.com/
2121

fernet_fields/fields.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,17 @@ def fernet(self):
6464
return MultiFernet([Fernet(k) for k in self.fernet_keys])
6565

6666
def db_type(self, connection):
67+
# PostgreSQL and SQLite both support the BYTEA type.
6768
return 'bytea'
6869

69-
def get_prep_value(self, value):
70+
def get_internal_type(self):
71+
"""Prevent Django from doing type conversions on encrypted data."""
72+
return None
73+
74+
def get_db_prep_value(self, *args, **kwargs):
75+
value = super(
76+
EncryptedFieldMixin, self
77+
).get_db_prep_value(*args, **kwargs)
7078
if value is not None:
7179
return self.fernet.encrypt(force_bytes(value))
7280

fernet_fields/test/settings/__init__.py

Whitespace-only changes.

fernet_fields/test/settings.py renamed to fernet_fields/test/settings/base.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@
44
from psycopg2cffi import compat
55
compat.register()
66

7-
DATABASES = {
8-
'default': {
9-
'ENGINE': 'django.db.backends.postgresql_psycopg2',
10-
'NAME': 'djftest',
11-
'TEST': {
12-
'NAME': 'djftest',
13-
},
14-
},
15-
}
16-
177
INSTALLED_APPS = [
188
'fernet_fields.test'
199
]

fernet_fields/test/settings/pg.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from .base import *
2+
3+
DATABASES = {
4+
'default': {
5+
'ENGINE': 'django.db.backends.postgresql_psycopg2',
6+
'NAME': 'djftest',
7+
'TEST': {
8+
'NAME': 'djftest',
9+
},
10+
},
11+
}

fernet_fields/test/settings/sqlite.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from .base import *
2+
3+
import os
4+
5+
HERE = os.path.dirname(os.path.abspath(__file__))
6+
DB = os.path.join(HERE, 'testdb.sqlite')
7+
8+
DATABASES = {
9+
'default': {
10+
'ENGINE': 'django.db.backends.sqlite3',
11+
'NAME': DB,
12+
'TEST': {
13+
'NAME': DB,
14+
},
15+
},
16+
}

runtests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
import sys
66

77
os.environ.setdefault(
8-
'DJANGO_SETTINGS_MODULE', 'fernet_fields.test.settings')
8+
'DJANGO_SETTINGS_MODULE', 'fernet_fields.test.settings.sqlite')
99

1010
sys.exit(pytest.main())

tox.ini

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py{27,33,34,py,py3}-django{18,trunk},flake8,docs
2+
envlist = py{27,33,34,py,py3}-django{18,trunk}-{pg,sqlite},flake8,docs
33

44
[testenv]
55
deps =
@@ -9,8 +9,11 @@ deps =
99
coverage==3.7.1
1010
django18: Django>=1.8,<1.9
1111
djangotrunk: https://github.com/django/django/tarball/master
12-
py{26,27,33,34}: psycopg2==2.6
13-
{pypy,pypy3}: psycopg2cffi==2.6.1
12+
py{26,27,33,34}-pg: psycopg2==2.6
13+
{pypy,pypy3}-pg: psycopg2cffi==2.6.1
14+
setenv =
15+
sqlite: DJANGO_SETTINGS_MODULE = fernet_fields.test.settings.sqlite
16+
pg: DJANGO_SETTINGS_MODULE = fernet_fields.test.settings.pg
1417
commands =
1518
coverage run -a runtests.py fernet_fields/test --tb short
1619

0 commit comments

Comments
 (0)