Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Django 5.0 #622

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0d33475
update requirements_dev.txt
jdanielnd Feb 26, 2024
fce839f
updating coverage to fix vulnerability
jdanielnd Feb 26, 2024
e9fb8be
update decimal field behaviour to match Django 3.2
jdanielnd Feb 26, 2024
feb3ffc
fixing tests to work with Django 5 timezone default
jdanielnd Feb 26, 2024
c67a1bb
setup github actions and database setup
jdanielnd Feb 26, 2024
6406b34
adding docker-compose for test database
jdanielnd Feb 26, 2024
43e0c31
adding 3.10 and 3.11 python versions on test
jdanielnd Feb 26, 2024
ef75a63
adding support to JSONField
jdanielnd Feb 26, 2024
3281a18
Merge pull request #1 from jdanielnd/add-jsonb-field
jdanielnd Feb 26, 2024
5cfaae0
setting 3.2 as Django minimal version
jdanielnd Feb 26, 2024
8de97a9
Update actions/setup-python to v5
jdanielnd Feb 27, 2024
f1fffb0
adding coverage to test workflow and create lint job
jdanielnd Feb 27, 2024
63d11be
add list as requirement for test
jdanielnd Feb 27, 2024
2b498af
remove lint from make test
jdanielnd Feb 27, 2024
1f1ba69
adding django to test matrix and removing coveralls
jdanielnd Feb 27, 2024
3e32f70
fixing typo on gh action
jdanielnd Feb 27, 2024
69d5e8f
adding quotes to django version
jdanielnd Feb 27, 2024
bb61d14
Revert "adding quotes to django version"
jdanielnd Feb 27, 2024
7bbda57
Revert "fixing typo on gh action"
jdanielnd Feb 27, 2024
192a1db
Revert "adding django to test matrix and removing coveralls"
jdanielnd Feb 27, 2024
f956acc
Revert "remove lint from make test"
jdanielnd Feb 27, 2024
1359525
Revert "add list as requirement for test"
jdanielnd Feb 27, 2024
1af0cdd
Revert "adding coverage to test workflow and create lint job"
jdanielnd Feb 27, 2024
9710f11
adding python 3.9 to matrix
jdanielnd Feb 27, 2024
30383db
adding django version to matrix
jdanielnd Feb 27, 2024
9af5664
debugging import error
jdanielnd Feb 27, 2024
f1c7fae
debugging import error
jdanielnd Feb 27, 2024
1340897
debugging import error
jdanielnd Feb 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: On push

on: [push]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
# python-version: ['3.9', '3.10', '3.11', '3.12']
# django-version: ['django>=3.2,<4.0', 'django>=4.0,<5.0', 'django>=5.0']
python-version: ['3.12']
django-version: ['django>=5.0']
exclude:
- python-version: '3.9'
django-version: ['django>=5.0']
services:
postgres:
image: postgres
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e .
pip install -r requirements_dev.txt
pip install '${{ matrix.django-version }}'
- name: Run tests
run: |
make test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ coverage.xml
*.log
local_settings.py

# pyenv
.python-version

# virtualenv
.venv/
venv/
Expand Down
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'
services:
postgres:
image: postgres
ports:
- 5432:5432
restart: unless-stopped
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
14 changes: 14 additions & 0 deletions pgcrypto/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,13 @@ class BooleanPGPSymmetricKeyField(PGPPublicKeyFieldMixin, models.BooleanField):
class DecimalPGPPublicKeyField(DecimalPGPFieldMixin,
PGPPublicKeyFieldMixin, models.DecimalField):
"""Decimal PGP public key encrypted field for postgres."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL_WITH_NULLIF


class DecimalPGPSymmetricKeyField(DecimalPGPFieldMixin,
PGPSymmetricKeyFieldMixin, models.DecimalField):
"""Decimal PGP symmetric key encrypted field for postgres."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL_WITH_NULLIF


class FloatPGPPublicKeyField(PGPPublicKeyFieldMixin, models.FloatField):
Expand All @@ -154,3 +156,15 @@ class TimePGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.TimeField):
"""Float PGP symmetric key encrypted field for postgres."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'TIME'


class JSONPGPPublicKeyField(PGPPublicKeyFieldMixin, models.JSONField):
"""JSON PGP public key encrypted field for postgres."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'jsonb'


class JSONPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.JSONField):
"""Float PGP symmetric key encrypted field for postgres."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'jsonb'
2 changes: 1 addition & 1 deletion pgcrypto/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def get_decrypt_sql(self, connection):

class DecimalPGPFieldMixin:
"""Decimal PGP encrypted field mixin for postgres."""
cast_type = 'NUMERIC(%(max_digits)s, %(decimal_places)s)'
cast_type = 'DECIMAL(%(max_digits)s, %(decimal_places)s)'

def get_cast_sql(self):
"""Get cast sql."""
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
-e .
django>=1.11,<3.2
django>=3.2,<=5.0.2
25 changes: 12 additions & 13 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
colour-runner==0.1.1
coveralls==3.1.0
coverage==5.5
coveralls==1.8.0
coverage==7.4.3
dj-database-url==0.5.0
factory-boy==3.2.0
flake8-docstrings==1.6.0
flake8-import-order==0.18.1
flake8==3.9.2
factory-boy==3.3.0
flake8-docstrings==1.7.0
flake8-import-order==0.18.2
flake8==7.0.0
incuna-test-utils==8.0.0
pip==22.3
psycopg2-binary==2.8.6
pyflakes==2.3.1
pycodestyle==2.7.0
setuptools==65.5.0
twine==3.4.1
wheel==0.36.2
psycopg2-binary==2.9.9
pyflakes==3.2.0
pycodestyle==2.11.1
setuptools==69.1.1
twine==5.0.0
wheel==0.42.0
13 changes: 13 additions & 0 deletions tests/db_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import psycopg2


def create_dbs(db_config, databases):
"""Set up the databases for test."""
connection = psycopg2.connect(**db_config)
connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cursor = connection.cursor()
for database in databases:
cursor.execute(f"DROP DATABASE IF EXISTS {database}")
cursor.execute(f"CREATE DATABASE {database}")
cursor.close()
connection.close()
11 changes: 8 additions & 3 deletions tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from datetime import date, datetime
from datetime import date
from decimal import Decimal

import factory
from django.utils import timezone


from .models import EncryptedFKModel, EncryptedModel

Expand All @@ -28,7 +30,7 @@ class EncryptedModelFactory(factory.django.DjangoModelFactory):
char_pub_field = factory.Sequence('Text {}'.format)

date_pgp_pub_field = date.today()
datetime_pgp_pub_field = datetime.now()
datetime_pgp_pub_field = timezone.now()
decimal_pgp_pub_field = Decimal('123456.78')
boolean_pgp_pub_field = True

Expand All @@ -39,9 +41,12 @@ class EncryptedModelFactory(factory.django.DjangoModelFactory):
char_sym_field = factory.Sequence('Text {}'.format)

date_pgp_sym_field = date.today()
datetime_pgp_sym_field = datetime.now()
datetime_pgp_sym_field = timezone.now()
boolean_pgp_sym_field = False

json_pgp_pub_field = {"key": "value"}
json_pgp_sym_field = {"key": "value"}

fk_model = factory.SubFactory(EncryptedFKModelFactory)

class Meta:
Expand Down
3 changes: 3 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class EncryptedModel(models.Model):
float_pgp_sym_field = fields.FloatPGPSymmetricKeyField(blank=True, null=True)
boolean_pgp_sym_field = fields.BooleanPGPSymmetricKeyField(blank=True, null=True)

json_pgp_pub_field = fields.JSONPGPPublicKeyField(blank=True, null=True)
json_pgp_sym_field = fields.JSONPGPSymmetricKeyField(blank=True, null=True)

fk_model = models.ForeignKey(
EncryptedFKModel, blank=True, null=True, on_delete=models.CASCADE
)
Expand Down
23 changes: 21 additions & 2 deletions tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@
from django.conf import settings
from django.test.runner import DiscoverRunner

print("Current directory:", os.getcwd())
print("Contents of the directory:", os.listdir(os.getcwd()))
print("Contents of the directory of run.py:", os.listdir(os.chdir(os.path.dirname(os.path.abspath(__file__)))))

from db_setup import create_dbs

default_db_config = {
'dbname': 'postgres',
'user': 'postgres',
'password': 'postgres',
'host': 'localhost',
'port': 5432,
}

create_dbs(default_db_config,
databases=('pgcrypto_fields', 'pgcrypto_fields_diff'))

BASEDIR = os.path.dirname(os.path.dirname(__file__))
PUBLIC_PGP_KEY_PATH = os.path.abspath(
os.path.join(BASEDIR, 'tests/keys/public.key')
Expand All @@ -24,7 +41,7 @@
)

diff_keys = dj_database_url.config(
default='postgres://localhost/pgcrypto_fields_diff'
default='postgres://postgres:postgres@localhost/pgcrypto_fields_diff'
)

# Cannot chain onto the config() call due to error
Expand All @@ -37,7 +54,7 @@
settings.configure(
DATABASES={
'default': dj_database_url.config(
default='postgres://localhost/pgcrypto_fields'
default='postgres://postgres:postgres@localhost/pgcrypto_fields'
),
'diff_keys': diff_keys,
},
Expand All @@ -52,6 +69,8 @@
PRIVATE_PGP_KEY=open(PRIVATE_PGP_KEY_PATH, 'r').read(),
PGCRYPTO_KEY='ultrasecret',
DEBUG=True,
DEFAULT_AUTO_FIELD='django.db.models.BigAutoField',
USE_TZ=True,
)
django.setup()

Expand Down
Loading