From f9eb016f66b75628a2e40b2262bc43e370d56a79 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 23 Nov 2022 15:27:27 +0100 Subject: [PATCH 2/3] django.contrib.postgres's JSONField is deprecated (#71619) --- .../apps/journal/migrations/0001_initial.py | 9 +++++++-- src/authentic2/apps/journal/models.py | 7 ++++++- .../custom_user/migrations/0020_deleteduser.py | 11 +++++++---- .../migrations/0029_profile_profiletype.py | 11 +++++++---- src/authentic2/custom_user/models.py | 7 ++++++- src/authentic2/migrations/0026_token.py | 9 +++++++-- src/authentic2/models.py | 8 ++++++-- .../migrations/0001_initial.py | 9 +++++++-- src/authentic2_auth_oidc/models.py | 7 ++++++- .../migrations/0001_initial.py | 15 ++++++++++----- src/authentic2_auth_saml/models.py | 7 ++++++- 11 files changed, 75 insertions(+), 25 deletions(-) diff --git a/src/authentic2/apps/journal/migrations/0001_initial.py b/src/authentic2/apps/journal/migrations/0001_initial.py index 814fa38b..d9fa8244 100644 --- a/src/authentic2/apps/journal/migrations/0001_initial.py +++ b/src/authentic2/apps/journal/migrations/0001_initial.py @@ -1,12 +1,17 @@ # Generated by Django 2.2.15 on 2020-08-23 16:56 +import django import django.contrib.postgres.fields -import django.contrib.postgres.fields.jsonb import django.db.models.deletion from django.conf import settings from django.db import migrations, models from django.utils import timezone +if django.VERSION < (3, 1): + from django.contrib.postgres.fields.jsonb import JSONField # noqa pylint: disable=ungrouped-imports +else: + from django.db.models import JSONField + class Migration(migrations.Migration): @@ -65,7 +70,7 @@ class Migration(migrations.Migration): verbose_name='reference ct ids', ), ), - ('data', django.contrib.postgres.fields.jsonb.JSONField(null=True, verbose_name='data')), + ('data', JSONField(null=True, verbose_name='data')), ( 'session', models.ForeignKey( diff --git a/src/authentic2/apps/journal/models.py b/src/authentic2/apps/journal/models.py index 1ab7fd93..d1202fe1 100644 --- a/src/authentic2/apps/journal/models.py +++ b/src/authentic2/apps/journal/models.py @@ -26,7 +26,7 @@ import django from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.contenttypes.models import ContentType -from django.contrib.postgres.fields import ArrayField, JSONField +from django.contrib.postgres.fields import ArrayField from django.contrib.postgres.fields.jsonb import KeyTextTransform from django.core.exceptions import ObjectDoesNotExist from django.db import models @@ -39,6 +39,11 @@ from authentic2.utils.cache import GlobalCache from . import sql +if django.VERSION < (3, 1): + from django.contrib.postgres.fields.jsonb import JSONField # noqa pylint: disable=ungrouped-imports +else: + from django.db.models import JSONField + logger = logging.getLogger(__name__) User = get_user_model() diff --git a/src/authentic2/custom_user/migrations/0020_deleteduser.py b/src/authentic2/custom_user/migrations/0020_deleteduser.py index 68dd9e1c..5fd92d91 100644 --- a/src/authentic2/custom_user/migrations/0020_deleteduser.py +++ b/src/authentic2/custom_user/migrations/0020_deleteduser.py @@ -1,8 +1,13 @@ # Generated by Django 2.2.12 on 2020-05-05 14:16 -import django.contrib.postgres.fields.jsonb +import django from django.db import migrations, models +if django.VERSION < (3, 1): + from django.contrib.postgres.fields.jsonb import JSONField # noqa pylint: disable=ungrouped-imports +else: + from django.db.models import JSONField + class Migration(migrations.Migration): @@ -30,9 +35,7 @@ class Migration(migrations.Migration): ), ( 'old_data', - django.contrib.postgres.fields.jsonb.JSONField( - blank=True, null=True, verbose_name='Old data' - ), + JSONField(blank=True, null=True, verbose_name='Old data'), ), ], options={ diff --git a/src/authentic2/custom_user/migrations/0029_profile_profiletype.py b/src/authentic2/custom_user/migrations/0029_profile_profiletype.py index ab5341d0..b58a46d1 100644 --- a/src/authentic2/custom_user/migrations/0029_profile_profiletype.py +++ b/src/authentic2/custom_user/migrations/0029_profile_profiletype.py @@ -2,11 +2,16 @@ import uuid -import django.contrib.postgres.fields.jsonb +import django import django.db.models.deletion from django.conf import settings from django.db import migrations, models +if django.VERSION < (3, 1): + from django.contrib.postgres.fields.jsonb import JSONField # noqa pylint: disable=ungrouped-imports +else: + from django.db.models import JSONField + class Migration(migrations.Migration): @@ -48,9 +53,7 @@ class Migration(migrations.Migration): ), ( 'data', - django.contrib.postgres.fields.jsonb.JSONField( - blank=True, null=True, verbose_name='data' - ), + JSONField(blank=True, null=True, verbose_name='data'), ), ( 'profile_type', diff --git a/src/authentic2/custom_user/models.py b/src/authentic2/custom_user/models.py index b15ef1b2..0a68338b 100644 --- a/src/authentic2/custom_user/models.py +++ b/src/authentic2/custom_user/models.py @@ -22,12 +22,12 @@ import operator import os import uuid +import django from django.contrib import auth from django.contrib.auth.models import AbstractBaseUser, Group from django.contrib.auth.models import Permission as AuthPermission from django.contrib.auth.models import _user_has_module_perms, _user_has_perm from django.contrib.contenttypes.fields import GenericRelation -from django.contrib.postgres.fields import JSONField from django.core.exceptions import MultipleObjectsReturned, ValidationError from django.core.mail import send_mail from django.db import models, transaction @@ -57,6 +57,11 @@ from authentic2.validators import PhoneNumberValidator, email_validator from .backends import DjangoRBACBackend from .managers import UserManager, UserQuerySet +if django.VERSION < (3, 1): + from django.contrib.postgres.fields.jsonb import JSONField # noqa pylint: disable=ungrouped-imports +else: + from django.db.models import JSONField + @RequestCache def get_attributes_map(): diff --git a/src/authentic2/migrations/0026_token.py b/src/authentic2/migrations/0026_token.py index 0be9eea6..bfa190eb 100644 --- a/src/authentic2/migrations/0026_token.py +++ b/src/authentic2/migrations/0026_token.py @@ -2,9 +2,14 @@ import uuid -import django.contrib.postgres.fields.jsonb +import django from django.db import migrations, models +if django.VERSION < (3, 1): + from django.contrib.postgres.fields.jsonb import JSONField # noqa pylint: disable=ungrouped-imports +else: + from django.db.models import JSONField + class Migration(migrations.Migration): @@ -29,7 +34,7 @@ class Migration(migrations.Migration): ('kind', models.CharField(max_length=32, verbose_name='Kind')), ( 'content', - django.contrib.postgres.fields.jsonb.JSONField(blank=True, verbose_name='Content'), + JSONField(blank=True, verbose_name='Content'), ), ('created', models.DateTimeField(verbose_name='Creation date', auto_now_add=True)), ('expires', models.DateTimeField(verbose_name='Expires')), diff --git a/src/authentic2/models.py b/src/authentic2/models.py index a25a71bc..2db2d0e8 100644 --- a/src/authentic2/models.py +++ b/src/authentic2/models.py @@ -27,7 +27,6 @@ from django.conf import settings from django.contrib import auth from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType -from django.contrib.postgres.fields import jsonb from django.contrib.postgres.indexes import GinIndex from django.contrib.postgres.search import SearchVectorField from django.core.exceptions import PermissionDenied, ValidationError @@ -50,6 +49,11 @@ from . import managers from . import natural_key as unused_natural_key # pylint: disable=unused-import from .utils.misc import ServiceAccessDenied +if django.VERSION < (3, 1): + from django.contrib.postgres.fields.jsonb import JSONField # noqa pylint: disable=ungrouped-imports +else: + from django.db.models import JSONField + class UserExternalId(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'), on_delete=models.CASCADE) @@ -538,7 +542,7 @@ class Token(models.Model): verbose_name=_('Identifier'), primary_key=True, default=uuid.uuid4, editable=False ) kind = models.CharField(verbose_name=_('Kind'), max_length=32) - content = jsonb.JSONField(verbose_name=_('Content'), blank=True) + content = JSONField(verbose_name=_('Content'), blank=True) created = models.DateTimeField(verbose_name=_('Creation date'), auto_now_add=True) expires = models.DateTimeField(verbose_name=_('Expires')) diff --git a/src/authentic2_auth_oidc/migrations/0001_initial.py b/src/authentic2_auth_oidc/migrations/0001_initial.py index 57b6d3a0..78de81ae 100644 --- a/src/authentic2_auth_oidc/migrations/0001_initial.py +++ b/src/authentic2_auth_oidc/migrations/0001_initial.py @@ -1,9 +1,14 @@ -import django.contrib.postgres.fields.jsonb +import django from django.conf import settings from django.db import migrations, models import authentic2_auth_oidc.models +if django.VERSION < (3, 1): + from django.contrib.postgres.fields.jsonb import JSONField # noqa pylint: disable=ungrouped-imports +else: + from django.db.models import JSONField + class Migration(migrations.Migration): @@ -88,7 +93,7 @@ class Migration(migrations.Migration): ('scopes', models.CharField(max_length=128, verbose_name='scopes', blank=True)), ( 'jwkset_json', - django.contrib.postgres.fields.jsonb.JSONField( + JSONField( blank=True, null=True, verbose_name='JSON WebKey set', diff --git a/src/authentic2_auth_oidc/models.py b/src/authentic2_auth_oidc/models.py index 06ac0521..2b4fe02e 100644 --- a/src/authentic2_auth_oidc/models.py +++ b/src/authentic2_auth_oidc/models.py @@ -16,8 +16,8 @@ import json +import django from django.conf import settings -from django.contrib.postgres.fields import JSONField from django.core.exceptions import ValidationError from django.db import models from django.shortcuts import render @@ -36,6 +36,11 @@ from authentic2.utils.template import validate_template from . import managers +if django.VERSION < (3, 1): + from django.contrib.postgres.fields.jsonb import JSONField # noqa pylint: disable=ungrouped-imports +else: + from django.db.models import JSONField + def validate_jwkset(data): data = json.dumps(data) diff --git a/src/authentic2_auth_saml/migrations/0001_initial.py b/src/authentic2_auth_saml/migrations/0001_initial.py index 66ddeed6..9bc14724 100644 --- a/src/authentic2_auth_saml/migrations/0001_initial.py +++ b/src/authentic2_auth_saml/migrations/0001_initial.py @@ -1,12 +1,17 @@ # Generated by Django 2.2.26 on 2022-06-15 15:00 -import django.contrib.postgres.fields.jsonb +import django import django.db.models.deletion from django.db import migrations, models import authentic2_auth_saml.models from authentic2_auth_saml.models import NAME_ID_FORMAT_CHOICES +if django.VERSION < (3, 1): + from django.contrib.postgres.fields.jsonb import JSONField # noqa pylint: disable=ungrouped-imports +else: + from django.db.models import JSONField + class Migration(migrations.Migration): @@ -203,7 +208,7 @@ class Migration(migrations.Migration): ), ( 'lookup_by_attributes', - django.contrib.postgres.fields.jsonb.JSONField( + JSONField( blank=True, default=list, help_text=( @@ -218,7 +223,7 @@ class Migration(migrations.Migration): ), ( 'a2_attribute_mapping', - django.contrib.postgres.fields.jsonb.JSONField( + JSONField( blank=True, default=list, help_text=( @@ -230,7 +235,7 @@ class Migration(migrations.Migration): ), ( 'attribute_mapping', - django.contrib.postgres.fields.jsonb.JSONField( + JSONField( blank=True, default=dict, help_text=( @@ -242,7 +247,7 @@ class Migration(migrations.Migration): ), ( 'superuser_mapping', - django.contrib.postgres.fields.jsonb.JSONField( + JSONField( blank=True, default=dict, editable=False, diff --git a/src/authentic2_auth_saml/models.py b/src/authentic2_auth_saml/models.py index ce811387..675a4fbb 100644 --- a/src/authentic2_auth_saml/models.py +++ b/src/authentic2_auth_saml/models.py @@ -16,9 +16,9 @@ import xml.etree.ElementTree as ET +import django import lasso from django.conf import settings -from django.contrib.postgres.fields import JSONField from django.core.exceptions import ValidationError from django.db import models from django.urls import reverse @@ -32,6 +32,11 @@ from authentic2.apps.authenticators.models import ( ) from authentic2.utils.misc import redirect_to_login +if django.VERSION < (3, 1): + from django.contrib.postgres.fields.jsonb import JSONField # noqa pylint: disable=ungrouped-imports +else: + from django.db.models import JSONField + NAME_ID_FORMAT_CHOICES = ( ('', _('None')), ( -- 2.37.2