-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Django4 with CastOnAssignFieldMixin (#25)
* Modified settings, setup, requirements, run_tests to handle Django3 and github actions * Converted KField to use native Django 3.11+ db.models.JSONField instead of legacy jsonfield v0.9 * Removed jsonfield and six from setup requirements * Killed default_app_config * Restrict numpy version for testing * Version 2.0.3 --------- Co-authored-by: Wes Okes <[email protected]> Co-authored-by: Ben Huffine <[email protected]>
- Loading branch information
1 parent
e320e9e
commit 4452e04
Showing
12 changed files
with
174 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
name: django-kmatch tests | ||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master,develop ] | ||
|
||
jobs: | ||
tests: | ||
runs-on: ubuntu-20.04 | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
python: [ '3.7','3.8','3.9' ] | ||
# Time to switch to pytest or nose2? | ||
# nosetests is broken on 3.10 | ||
# AttributeError: module 'collections' has no attribute 'Callable' | ||
# https://github.com/nose-devs/nose/issues/1099 | ||
django: | ||
- 'Django~=3.2.0' | ||
- 'Django~=4.0.0' | ||
- 'Django~=4.1.0' | ||
- 'Django~=4.2.0' | ||
exclude: | ||
- python: '3.7' | ||
django: 'Django~=4.0.0' | ||
- python: '3.7' | ||
django: 'Django~=4.1.0' | ||
- python: '3.7' | ||
django: 'Django~=4.2.0' | ||
services: | ||
postgres: | ||
image: postgres:14.5 | ||
env: | ||
POSTGRES_DB: postgres | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_USER: postgres | ||
POSTGRES_HOST_AUTH_METHOD: trust | ||
ports: | ||
- 5432:5432 | ||
options: >- | ||
--health-cmd pg_isready | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python ${{ matrix.python }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python }} | ||
- name: Install requirements | ||
run: | | ||
python --version | ||
pip install --upgrade pip wheel setuptools | ||
pip install -r requirements/requirements.txt | ||
pip install -r requirements/requirements-testing.txt | ||
pip install "${{ matrix.django }}" | ||
pip freeze | ||
- name: Run tests | ||
env: | ||
DB_SETTINGS: >- | ||
{ | ||
"ENGINE":"django.db.backends.postgresql", | ||
"NAME":"django_kmatch", | ||
"USER":"postgres", | ||
"PASSWORD":"postgres", | ||
"HOST":"localhost", | ||
"PORT":"5432" | ||
} | ||
run: | | ||
python manage.py check | ||
coverage run manage.py test django_kmatch | ||
coverage report --fail-under=90 | ||
- name: Check style | ||
run: flake8 django_kmatch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
include README.rst | ||
include LICENSE | ||
include LICENSE | ||
recursive-include requirements * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# flake8: noqa | ||
from .version import __version__ | ||
from .fields import KField | ||
|
||
default_app_config = 'django_kmatch.apps.DjangoKmatchConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,50 @@ | ||
from jsonfield import JSONField | ||
import json | ||
|
||
from django.core.serializers.json import DjangoJSONEncoder | ||
from django.db.models import JSONField as DjangoJSONField | ||
from ambition_utils.fields import CastOnAssignFieldMixin | ||
from kmatch import K | ||
|
||
|
||
class KField(JSONField): | ||
class KField(CastOnAssignFieldMixin, DjangoJSONField): | ||
"""Stores a kmatch pattern and returns a compiled K object. | ||
The KField field stores a kmatch pattern in a JSONField. The pattern is compiled and returned as | ||
a K object when accessing the field. Invalid kmatch patterns cannot be stored. | ||
""" | ||
The KField field stores a kmatch pattern in a JSONField. The pattern is compiled and returned as | ||
a K object when accessing the field. Invalid kmatch patterns cannot be stored. | ||
""" | ||
description = 'A kmatch pattern' | ||
|
||
def pre_init(self, value, obj): | ||
""" | ||
Used to obtain a K object for a provided pattern. Normally this is done in the to_python method | ||
of a Django custom field. However, this field inherits JSONField, and JSONField had to do | ||
conversions in the pre_init method. | ||
""" | ||
value = super(KField, self).pre_init(value, obj) | ||
return K(value) if not isinstance(value, K) and value is not None else value | ||
def __init__(self, *args, **kwargs): | ||
self.dump_kwargs = kwargs.pop('dump_kwargs', { | ||
'cls': DjangoJSONEncoder, | ||
'separators': (',', ':') | ||
}) | ||
self.load_kwargs = kwargs.pop('load_kwargs', {}) | ||
|
||
super().__init__(*args, **kwargs) | ||
|
||
def get_db_prep_value(self, value, connection, prepared=False): | ||
""" | ||
Converts a K object to a pattern. This pattern will be serialized to JSON and saved as a | ||
TextField. | ||
Converts a K object to a pattern. | ||
""" | ||
if isinstance(value, K): | ||
value = value.pattern | ||
return super(KField, self).get_db_prep_value(value, connection, prepared=False) | ||
|
||
if not self.null and value is None: | ||
return json.dumps(value) | ||
|
||
return super().get_db_prep_value(value, connection, prepared=False) | ||
|
||
def to_python(self, value): | ||
""" | ||
Used to obtain a K object for a provided pattern. | ||
""" | ||
if isinstance(value, K) or value is None: | ||
return value | ||
|
||
if isinstance(value, str): | ||
# We really should not ever get here - the only way would be if the json | ||
# was invalid, in which case we'll end up with a value error anyway. | ||
return K(json.loads(value)) | ||
else: | ||
return K(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '1.2.0' | ||
__version__ = '2.0.3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
coverage | ||
django-nose | ||
flake8 | ||
mock | ||
psycopg2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Django>=3.2 | ||
ambition-utils>=3.1.6 | ||
kmatch>=0.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters