Skip to content

Commit 2f408bd

Browse files
committed
[change] Replaced third-party JSONField with Django built-in #600
Replaced jsonfield package's JSONField with Django's built-in JSONField across all models to use the modern, maintained Django implementation. Updated RadiusBatch.prefix_add method to directly assign user_credentials list instead of using json.dumps(). Added migration 0043 to replace JSONField in model definitions and migration 0044 to convert existing double-encoded JSON data in user_credentials field with proper error handling. Used .iterator() in data migration for memory efficiency and removed jsonfield dependency from setup.py. Closes #600
1 parent b512f3a commit 2f408bd

6 files changed

Lines changed: 146 additions & 10 deletions

File tree

openwisp_radius/base/models.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import csv
22
import ipaddress
3-
import json
43
import logging
54
import os
65
import string
@@ -17,13 +16,13 @@
1716
from django.core.cache import cache
1817
from django.core.exceptions import ObjectDoesNotExist, ValidationError
1918
from django.core.mail import send_mail
19+
from django.core.serializers.json import DjangoJSONEncoder
2020
from django.db import models, transaction
21-
from django.db.models import ProtectedError, Q
21+
from django.db.models import JSONField, ProtectedError, Q
2222
from django.utils import timezone
2323
from django.utils.crypto import get_random_string
2424
from django.utils.timezone import now
2525
from django.utils.translation import gettext_lazy as _
26-
from jsonfield import JSONField
2726
from model_utils.fields import AutoLastModifiedField
2827
from openwisp_notifications.signals import notify
2928
from phonenumber_field.modelfields import PhoneNumberField
@@ -928,6 +927,7 @@ class AbstractRadiusBatch(OrgMixin, TimeStampedEditableModel):
928927
null=True,
929928
blank=True,
930929
verbose_name="PDF",
930+
encoder=DjangoJSONEncoder,
931931
)
932932
expiration_date = models.DateField(
933933
verbose_name=_("expiration date"),
@@ -1023,7 +1023,7 @@ def prefix_add(self, prefix, n, password_length=BATCH_DEFAULT_PASSWORD_LENGTH):
10231023
for user in users_list:
10241024
user.full_clean()
10251025
self.save_user(user)
1026-
self.user_credentials = json.dumps(user_credentials)
1026+
self.user_credentials = user_credentials
10271027
self.full_clean()
10281028
self.save()
10291029

@@ -1247,6 +1247,7 @@ class AbstractOrganizationRadiusSettings(UUIDModel):
12471247
" (optional, leave blank if unsure)"
12481248
),
12491249
verbose_name=_("SMS meta data"),
1250+
encoder=DjangoJSONEncoder,
12501251
)
12511252
freeradius_allowed_hosts = FallbackTextField(
12521253
help_text=_GET_IP_LIST_HELP_TEXT,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by Django 5.2.5 on 2025-10-22 18:34
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("openwisp_radius", "0042_set_existing_batches_completed"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="organizationradiussettings",
15+
name="sms_meta_data",
16+
field=models.JSONField(
17+
blank=True,
18+
help_text=(
19+
"Additional configuration for SMS backend in JSON format "
20+
"(optional, leave blank if unsure)"
21+
),
22+
null=True,
23+
verbose_name="SMS meta data",
24+
),
25+
),
26+
migrations.AlterField(
27+
model_name="radiusbatch",
28+
name="user_credentials",
29+
field=models.JSONField(blank=True, null=True, verbose_name="PDF"),
30+
),
31+
]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Generated by Django 5.2.9 on 2026-02-13 18:15
2+
3+
import json
4+
import logging
5+
6+
import django.core.serializers.json
7+
from django.db import migrations, models
8+
9+
logger = logging.getLogger(__name__)
10+
11+
12+
def convert_user_credentials_data(apps, schema_editor):
13+
"""
14+
Convert existing double-encoded JSON strings in user_credentials field
15+
to proper JSON objects for Django's built-in JSONField.
16+
"""
17+
db_alias = schema_editor.connection.alias
18+
RadiusBatch = apps.get_model("openwisp_radius", "RadiusBatch")
19+
for batch in (
20+
RadiusBatch.objects.using(db_alias)
21+
.exclude(user_credentials__isnull=True)
22+
.iterator()
23+
):
24+
if isinstance(batch.user_credentials, str):
25+
try:
26+
batch.user_credentials = json.loads(batch.user_credentials)
27+
batch.save(using=db_alias, update_fields=["user_credentials"])
28+
except Exception as e:
29+
logger.exception(f"Encountered error while processing {batch}: {e}")
30+
print(f"Encountered error while processing {batch}: {e}")
31+
32+
33+
class Migration(migrations.Migration):
34+
35+
dependencies = [
36+
(
37+
"openwisp_radius",
38+
"0043_alter_organizationradiussettings_sms_meta_data_and_more",
39+
),
40+
]
41+
42+
operations = [
43+
migrations.AlterField(
44+
model_name="organizationradiussettings",
45+
name="sms_meta_data",
46+
field=models.JSONField(
47+
blank=True,
48+
encoder=django.core.serializers.json.DjangoJSONEncoder,
49+
help_text=(
50+
"Additional configuration for SMS backend in JSON format "
51+
"(optional, leave blank if unsure)"
52+
),
53+
null=True,
54+
verbose_name="SMS meta data",
55+
),
56+
),
57+
migrations.AlterField(
58+
model_name="radiusbatch",
59+
name="user_credentials",
60+
field=models.JSONField(
61+
blank=True,
62+
encoder=django.core.serializers.json.DjangoJSONEncoder,
63+
null=True,
64+
verbose_name="PDF",
65+
),
66+
),
67+
migrations.RunPython(
68+
convert_user_credentials_data, reverse_code=migrations.RunPython.noop
69+
),
70+
]

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
"weasyprint>=65,<68",
5454
"dj-rest-auth>=6.0,<7.2",
5555
"django-sendsms~=0.5.0",
56-
"jsonfield~=3.1.0",
5756
"django-private-storage~=3.1.0",
5857
"django-ipware>=5.0,<7.1",
5958
"pyrad~=2.4",

tests/openwisp2/sample_radius/migrations/0002_initial_openwisp_app.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import django.core.validators
88
import django.db.models.deletion
99
import django.utils.timezone
10-
import jsonfield.fields
1110
import model_utils.fields
1211
import private_storage.fields
1312
import private_storage.storage.files
@@ -543,7 +542,7 @@ class Migration(migrations.Migration):
543542
),
544543
(
545544
"sms_meta_data",
546-
jsonfield.fields.JSONField(
545+
models.JSONField(
547546
blank=True,
548547
help_text=(
549548
"Additional configuration for SMS backend in JSON format"
@@ -695,9 +694,7 @@ class Migration(migrations.Migration):
695694
),
696695
(
697696
"user_credentials",
698-
jsonfield.fields.JSONField(
699-
blank=True, null=True, verbose_name="PDF"
700-
),
697+
models.JSONField(blank=True, null=True, verbose_name="PDF"),
701698
),
702699
(
703700
"expiration_date",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Generated by Django 5.2.9 on 2026-02-13 18:15
2+
3+
import django.core.serializers.json
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
("sample_radius", "0031_radiusbatch_status"),
11+
]
12+
13+
operations = [
14+
migrations.AlterField(
15+
model_name="organizationradiussettings",
16+
name="sms_meta_data",
17+
field=models.JSONField(
18+
blank=True,
19+
encoder=django.core.serializers.json.DjangoJSONEncoder,
20+
help_text=(
21+
"Additional configuration for SMS backend in JSON format "
22+
"(optional, leave blank if unsure)"
23+
),
24+
null=True,
25+
verbose_name="SMS meta data",
26+
),
27+
),
28+
migrations.AlterField(
29+
model_name="radiusbatch",
30+
name="user_credentials",
31+
field=models.JSONField(
32+
blank=True,
33+
encoder=django.core.serializers.json.DjangoJSONEncoder,
34+
null=True,
35+
verbose_name="PDF",
36+
),
37+
),
38+
]

0 commit comments

Comments
 (0)