Skip to content

Commit fe56cd9

Browse files
committed
[change:radius] Replace third-party JSONField with Django built-in JSONField
- Replaced jsonfield.JSONField with Django's built-in JSONField in models - Updated RadiusBatch.user_credentials field - Updated OrganizationRadiusSettings.sms_meta_data field - Removed third-party jsonfield dependency from setup.py - Added migration to handle field type transition - Updated test migrations to use Django's JSONField - Ensured backward compatibility and data preservation - All existing tests pass with the new implementation Fixes #600
1 parent f93534d commit fe56cd9

4 files changed

Lines changed: 55 additions & 9 deletions

File tree

openwisp_radius/base/models.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
from django.core.exceptions import ObjectDoesNotExist, ValidationError
1717
from django.core.mail import send_mail
1818
from django.db import models
19-
from django.db.models import ProtectedError, Q
19+
from django.db.models import JSONField, ProtectedError, Q
2020
from django.utils import timezone
2121
from django.utils.crypto import get_random_string
2222
from django.utils.timezone import now
2323
from django.utils.translation import gettext_lazy as _
24-
from jsonfield import JSONField
2524
from model_utils.fields import AutoLastModifiedField
2625
from phonenumber_field.modelfields import PhoneNumberField
2726
from private_storage.fields import PrivateFileField
@@ -974,7 +973,7 @@ def prefix_add(self, prefix, n, password_length=BATCH_DEFAULT_PASSWORD_LENGTH):
974973
for user in users_list:
975974
user.full_clean()
976975
self.save_user(user)
977-
self.user_credentials = json.dumps(user_credentials)
976+
self.user_credentials = user_credentials
978977
self.full_clean()
979978
self.save()
980979

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Generated by Django 5.2.5 on 2025-08-10 05:19
2+
3+
import json
4+
5+
from django.db import migrations, models
6+
7+
8+
def convert_user_credentials_data(apps, schema_editor):
9+
RadiusBatch = apps.get_model("openwisp_radius", "RadiusBatch")
10+
for batch in RadiusBatch.objects.exclude(user_credentials__isnull=True):
11+
# Decode the double-encoded JSON
12+
try:
13+
decoded = json.loads(batch.user_credentials)
14+
batch.user_credentials = decoded
15+
batch.save(update_fields=["user_credentials"])
16+
except (json.JSONDecodeError, TypeError):
17+
# Skip if data is not valid JSON
18+
pass
19+
20+
21+
class Migration(migrations.Migration):
22+
23+
dependencies = [
24+
("openwisp_radius", "0040_rename_phonetoken_index"),
25+
]
26+
27+
operations = [
28+
# Replace third-party JSONField with Django's built-in JSONField
29+
migrations.AlterField(
30+
model_name="radiusbatch",
31+
name="user_credentials",
32+
field=models.JSONField(
33+
blank=True,
34+
null=True,
35+
verbose_name="PDF",
36+
),
37+
),
38+
migrations.AlterField(
39+
model_name="organizationradiussettings",
40+
name="sms_meta_data",
41+
field=models.JSONField(
42+
blank=True,
43+
help_text="Additional configuration for SMS backend in JSON "
44+
"format (optional, leave blank if unsure)",
45+
null=True,
46+
verbose_name="SMS meta data",
47+
),
48+
),
49+
# Convert existing user_credentials data from double-encoded JSON
50+
migrations.RunPython(convert_user_credentials_data, migrations.RunPython.noop),
51+
]

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"weasyprint>=65,<67",
4646
"dj-rest-auth>=6.0,<7.1",
4747
"django-sendsms~=0.5.0",
48-
"jsonfield~=3.1.0",
4948
"django-private-storage~=3.1.0",
5049
"django-ipware>=5.0,<7.1",
5150
"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",

0 commit comments

Comments
 (0)