Skip to content

Commit fa44638

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 0043 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 - Removed unused json import Closes #600
1 parent fc948b9 commit fa44638

4 files changed

Lines changed: 56 additions & 10 deletions

File tree

openwisp_radius/base/models.py

Lines changed: 2 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
@@ -18,12 +17,11 @@
1817
from django.core.exceptions import ObjectDoesNotExist, ValidationError
1918
from django.core.mail import send_mail
2019
from django.db import models
21-
from django.db.models import ProtectedError, Q
20+
from django.db.models import JSONField, ProtectedError, Q
2221
from django.utils import timezone
2322
from django.utils.crypto import get_random_string
2423
from django.utils.timezone import now
2524
from django.utils.translation import gettext_lazy as _
26-
from jsonfield import JSONField
2725
from model_utils.fields import AutoLastModifiedField
2826
from openwisp_notifications.signals import notify
2927
from phonenumber_field.modelfields import PhoneNumberField
@@ -1021,7 +1019,7 @@ def prefix_add(self, prefix, n, password_length=BATCH_DEFAULT_PASSWORD_LENGTH):
10211019
for user in users_list:
10221020
user.full_clean()
10231021
self.save_user(user)
1024-
self.user_credentials = json.dumps(user_credentials)
1022+
self.user_credentials = user_credentials
10251023
self.full_clean()
10261024
self.save()
10271025

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

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)