From 796d7bf67ff9e03738915af67a4e4de9d8c5670d Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Thu, 27 Jun 2019 17:10:15 -0400 Subject: [PATCH] Replace the use of the 3rd party jsonbfield library which was just a backport of Django's built-in JSONField. Also, bump the version of django-jsonfield. --- awx/main/fields.py | 20 ++----------- .../migrations/0002_squashed_v300_release.py | 3 +- awx/main/tests/functional/conftest.py | 4 +-- docs/licenses/django-jsonbfield.txt | 28 ------------------- requirements/requirements.in | 2 +- requirements/requirements.txt | 2 +- requirements/requirements_git.txt | 1 - 7 files changed, 8 insertions(+), 52 deletions(-) delete mode 100644 docs/licenses/django-jsonbfield.txt diff --git a/awx/main/fields.py b/awx/main/fields.py index 70ee365086..db1bc554d8 100644 --- a/awx/main/fields.py +++ b/awx/main/fields.py @@ -12,7 +12,9 @@ from jinja2.exceptions import UndefinedError, TemplateSyntaxError # Django import django +from django.contrib.postgres.fields import JSONField as upstream_JSONBField from django.core import exceptions as django_exceptions +from django.core.serializers.json import DjangoJSONEncoder from django.db.models.signals import ( post_save, post_delete, @@ -37,7 +39,6 @@ import jsonschema.exceptions # Django-JSONField from jsonfield import JSONField as upstream_JSONField -from jsonbfield.fields import JSONField as upstream_JSONBField # DRF from rest_framework import serializers @@ -91,7 +92,7 @@ class JSONBField(upstream_JSONBField): def get_db_prep_value(self, value, connection, prepared=False): if connection.vendor == 'sqlite': # sqlite (which we use for tests) does not support jsonb; - return json.dumps(value) + return json.dumps(value, cls=DjangoJSONEncoder) return super(JSONBField, self).get_db_prep_value( value, connection, prepared ) @@ -453,21 +454,6 @@ class JSONSchemaField(JSONBField): params={'value': value}, ) - def get_db_prep_value(self, value, connection, prepared=False): - if connection.vendor == 'sqlite': - # sqlite (which we use for tests) does not support jsonb; - return json.dumps(value) - return super(JSONSchemaField, self).get_db_prep_value( - value, connection, prepared - ) - - def from_db_value(self, value, expression, connection, context): - # Work around a bug in django-jsonfield - # https://bitbucket.org/schinckel/django-jsonfield/issues/57/cannot-use-in-the-same-project-as-djangos - if isinstance(value, str): - return json.loads(value) - return value - @JSONSchemaField.format_checker.checks('vault_id') def format_vault_id(value): diff --git a/awx/main/migrations/0002_squashed_v300_release.py b/awx/main/migrations/0002_squashed_v300_release.py index 11190a20da..9e84a889ab 100644 --- a/awx/main/migrations/0002_squashed_v300_release.py +++ b/awx/main/migrations/0002_squashed_v300_release.py @@ -13,7 +13,6 @@ from django.conf import settings from django.utils.timezone import now import jsonfield.fields -import jsonbfield.fields import taggit.managers @@ -239,7 +238,7 @@ class Migration(migrations.Migration): ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('timestamp', models.DateTimeField(default=None, help_text='Date and time of the corresponding fact scan gathering time.', editable=False)), ('module', models.CharField(max_length=128)), - ('facts', jsonbfield.fields.JSONField(default={}, help_text='Arbitrary JSON structure of module facts captured at timestamp for a single host.', blank=True)), + ('facts', awx.main.fields.JSONBField(default={}, help_text='Arbitrary JSON structure of module facts captured at timestamp for a single host.', blank=True)), ('host', models.ForeignKey(related_name='facts', to='main.Host', help_text='Host for the facts that the fact scan captured.')), ], ), diff --git a/awx/main/tests/functional/conftest.py b/awx/main/tests/functional/conftest.py index 5a0efa37ae..c924034bdd 100644 --- a/awx/main/tests/functional/conftest.py +++ b/awx/main/tests/functional/conftest.py @@ -11,9 +11,9 @@ from django.urls import resolve from django.contrib.auth.models import User from django.core.serializers.json import DjangoJSONEncoder from django.db.backends.sqlite3.base import SQLiteCursorWrapper -from jsonbfield.fields import JSONField # AWX +from awx.main.fields import JSONBField from awx.main.models.projects import Project from awx.main.models.ha import Instance @@ -737,7 +737,7 @@ def get_db_prep_save(self, value, connection, **kwargs): @pytest.fixture def monkeypatch_jsonbfield_get_db_prep_save(mocker): - JSONField.get_db_prep_save = get_db_prep_save + JSONBField.get_db_prep_save = get_db_prep_save @pytest.fixture diff --git a/docs/licenses/django-jsonbfield.txt b/docs/licenses/django-jsonbfield.txt deleted file mode 100644 index 028fb0f561..0000000000 --- a/docs/licenses/django-jsonbfield.txt +++ /dev/null @@ -1,28 +0,0 @@ - -Copyright (c) Django Software Foundation and individual contributors. -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - 3. Neither the name of Django nor the names of its contributors may be used - to endorse or promote products derived from this software without - specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/requirements/requirements.in b/requirements/requirements.in index 6864d5ee1d..534bc2dbcd 100644 --- a/requirements/requirements.in +++ b/requirements/requirements.in @@ -12,7 +12,7 @@ django-auth-ldap==1.7.0 django-cors-headers==2.4.0 django-crum==0.7.2 django-extensions==2.0.0 -django-jsonfield==1.0.1 +django-jsonfield==1.2.0 django-oauth-toolkit==1.1.3 django-polymorphic==2.0.2 django-pglocks==1.0.2 diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 6726525d5e..246ae863c0 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -27,7 +27,7 @@ django-auth-ldap==1.7.0 django-cors-headers==2.4.0 django-crum==0.7.2 django-extensions==2.0.0 -django-jsonfield==1.0.1 +django-jsonfield==1.2.0 django-oauth-toolkit==1.1.3 django-pglocks==1.0.2 django-polymorphic==2.0.2 diff --git a/requirements/requirements_git.txt b/requirements/requirements_git.txt index 46953643f7..9ce11ac140 100644 --- a/requirements/requirements_git.txt +++ b/requirements/requirements_git.txt @@ -1,3 +1,2 @@ git+https://github.com/ansible/ansiconv.git@tower_1.0.0#egg=ansiconv git+https://github.com/ansible/django-qsstats-magic.git@py3#egg=django-qsstats-magic -git+https://github.com/ansible/django-jsonbfield@fix-sqlite_serialization#egg=jsonbfield