Skip to content

Commit 665a8b5

Browse files
committed
[models] Replaced JSONField with Django's built-in #600
- Replaced third-party jsonfield.JSONField with Django's built-in models.JSONField in all model definitions - Updated RadiusBatch.prefix_add method to directly assign user_credentials list instead of using json.dumps() - Added migration 0041 with data conversion function to handle existing double-encoded JSON data in user_credentials field - Removed jsonfield dependency from setup.py - Updated test migrations to use Django's JSONField
1 parent fc948b9 commit 665a8b5

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
@@ -18,12 +18,11 @@
1818
from django.core.exceptions import ObjectDoesNotExist, ValidationError
1919
from django.core.mail import send_mail
2020
from django.db import models
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
@@ -1021,7 +1020,7 @@ def prefix_add(self, prefix, n, password_length=BATCH_DEFAULT_PASSWORD_LENGTH):
10211020
for user in users_list:
10221021
user.full_clean()
10231022
self.save_user(user)
1024-
self.user_credentials = json.dumps(user_credentials)
1023+
self.user_credentials = user_credentials
10251024
self.full_clean()
10261025
self.save()
10271026

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
@@ -49,7 +49,6 @@
4949
"weasyprint>=65,<67",
5050
"dj-rest-auth>=6.0,<7.1",
5151
"django-sendsms~=0.5.0",
52-
"jsonfield~=3.1.0",
5352
"django-private-storage~=3.1.0",
5453
"django-ipware>=5.0,<7.1",
5554
"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)