-
-
Notifications
You must be signed in to change notification settings - Fork 222
[change:radius] Replace third-party JSONField with Django built-in JSONField #619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Generated by Django 5.2.5 on 2025-10-22 18:34 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("openwisp_radius", "0042_set_existing_batches_completed"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name="organizationradiussettings", | ||
| name="sms_meta_data", | ||
| field=models.JSONField( | ||
| blank=True, | ||
| help_text=( | ||
| "Additional configuration for SMS backend in JSON format " | ||
| "(optional, leave blank if unsure)" | ||
| ), | ||
| null=True, | ||
| verbose_name="SMS meta data", | ||
| ), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="radiusbatch", | ||
| name="user_credentials", | ||
| field=models.JSONField(blank=True, null=True, verbose_name="PDF"), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||||||||||||||
| # Generated by Django 5.2.9 on 2026-02-13 18:15 | ||||||||||||||||||
|
|
||||||||||||||||||
| import json | ||||||||||||||||||
| import logging | ||||||||||||||||||
|
|
||||||||||||||||||
| import django.core.serializers.json | ||||||||||||||||||
| from django.db import migrations, models | ||||||||||||||||||
|
|
||||||||||||||||||
| logger = logging.getLogger(__name__) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def convert_user_credentials_data(apps, schema_editor): | ||||||||||||||||||
| """ | ||||||||||||||||||
| Convert existing double-encoded JSON strings in user_credentials field | ||||||||||||||||||
| to proper JSON objects for Django's built-in JSONField. | ||||||||||||||||||
| """ | ||||||||||||||||||
| db_alias = schema_editor.connection.alias | ||||||||||||||||||
| RadiusBatch = apps.get_model("openwisp_radius", "RadiusBatch") | ||||||||||||||||||
| for batch in ( | ||||||||||||||||||
| RadiusBatch.objects.using(db_alias) | ||||||||||||||||||
| .exclude(user_credentials__isnull=True) | ||||||||||||||||||
| .iterator() | ||||||||||||||||||
| ): | ||||||||||||||||||
| if isinstance(batch.user_credentials, str): | ||||||||||||||||||
| try: | ||||||||||||||||||
| batch.user_credentials = json.loads(batch.user_credentials) | ||||||||||||||||||
| batch.save(using=db_alias, update_fields=["user_credentials"]) | ||||||||||||||||||
| except Exception as e: | ||||||||||||||||||
| logger.exception(f"Encountered error while processing {batch}: {e}") | ||||||||||||||||||
| print(f"Encountered error while processing {batch}: {e}") | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL: Logic error -
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| class Migration(migrations.Migration): | ||||||||||||||||||
|
|
||||||||||||||||||
| dependencies = [ | ||||||||||||||||||
| ( | ||||||||||||||||||
| "openwisp_radius", | ||||||||||||||||||
| "0043_alter_organizationradiussettings_sms_meta_data_and_more", | ||||||||||||||||||
| ), | ||||||||||||||||||
| ] | ||||||||||||||||||
|
|
||||||||||||||||||
| operations = [ | ||||||||||||||||||
| migrations.AlterField( | ||||||||||||||||||
| model_name="organizationradiussettings", | ||||||||||||||||||
| name="sms_meta_data", | ||||||||||||||||||
| field=models.JSONField( | ||||||||||||||||||
| blank=True, | ||||||||||||||||||
| encoder=django.core.serializers.json.DjangoJSONEncoder, | ||||||||||||||||||
| help_text=( | ||||||||||||||||||
| "Additional configuration for SMS backend in JSON format " | ||||||||||||||||||
| "(optional, leave blank if unsure)" | ||||||||||||||||||
| ), | ||||||||||||||||||
| null=True, | ||||||||||||||||||
| verbose_name="SMS meta data", | ||||||||||||||||||
| ), | ||||||||||||||||||
| ), | ||||||||||||||||||
| migrations.AlterField( | ||||||||||||||||||
| model_name="radiusbatch", | ||||||||||||||||||
| name="user_credentials", | ||||||||||||||||||
| field=models.JSONField( | ||||||||||||||||||
| blank=True, | ||||||||||||||||||
| encoder=django.core.serializers.json.DjangoJSONEncoder, | ||||||||||||||||||
| null=True, | ||||||||||||||||||
| verbose_name="PDF", | ||||||||||||||||||
| ), | ||||||||||||||||||
| ), | ||||||||||||||||||
| migrations.RunPython( | ||||||||||||||||||
| convert_user_credentials_data, reverse_code=migrations.RunPython.noop | ||||||||||||||||||
| ), | ||||||||||||||||||
| ] | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import importlib | ||
| import json | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from django.db import connection | ||
| from django.test import TestCase | ||
|
|
||
| from openwisp_radius.models import RadiusBatch | ||
|
|
||
| migration_module = importlib.import_module( | ||
| "openwisp_radius.migrations.0044_convert_user_credentials_data" | ||
| ) | ||
| convert_user_credentials_data = migration_module.convert_user_credentials_data | ||
|
|
||
|
|
||
| class Test0044Migration(TestCase): | ||
| def test_convert_user_credentials_data(self): | ||
| batch = RadiusBatch.objects.create( | ||
| name="test_batch_migration", strategy="prefix", prefix="test" | ||
| ) | ||
| RadiusBatch.objects.filter(pk=batch.pk).update( | ||
| user_credentials=json.dumps({"user1": "pass1"}) | ||
| ) | ||
|
|
||
| apps = MagicMock() | ||
| apps.get_model.return_value = RadiusBatch | ||
|
|
||
| schema_editor = MagicMock() | ||
| schema_editor.connection = connection | ||
|
|
||
| convert_user_credentials_data(apps, schema_editor) | ||
|
|
||
| batch.refresh_from_db() | ||
| self.assertEqual(batch.user_credentials, {"user1": "pass1"}) | ||
|
|
||
| def test_convert_user_credentials_data_invalid_json(self): | ||
| batch = RadiusBatch.objects.create( | ||
| name="test_batch_invalid", strategy="prefix", prefix="test2" | ||
| ) | ||
| RadiusBatch.objects.filter(pk=batch.pk).update( | ||
| user_credentials="invalid_json_string" | ||
| ) | ||
|
|
||
| apps = MagicMock() | ||
| apps.get_model.return_value = RadiusBatch | ||
|
|
||
| schema_editor = MagicMock() | ||
| schema_editor.connection = connection | ||
|
|
||
| convert_user_credentials_data(apps, schema_editor) | ||
|
|
||
| batch.refresh_from_db() | ||
| self.assertEqual(batch.user_credentials, "invalid_json_string") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Generated by Django 5.2.9 on 2026-02-13 18:15 | ||
|
|
||
| import django.core.serializers.json | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("sample_radius", "0031_radiusbatch_status"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name="organizationradiussettings", | ||
| name="sms_meta_data", | ||
| field=models.JSONField( | ||
| blank=True, | ||
| encoder=django.core.serializers.json.DjangoJSONEncoder, | ||
| help_text=( | ||
| "Additional configuration for SMS backend in JSON format " | ||
| "(optional, leave blank if unsure)" | ||
| ), | ||
| null=True, | ||
| verbose_name="SMS meta data", | ||
| ), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="radiusbatch", | ||
| name="user_credentials", | ||
| field=models.JSONField( | ||
| blank=True, | ||
| encoder=django.core.serializers.json.DjangoJSONEncoder, | ||
| null=True, | ||
| verbose_name="PDF", | ||
| ), | ||
| ), | ||
| ] |
Uh oh!
There was an error while loading. Please reload this page.