-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy path0044_convert_user_credentials_data.py
More file actions
70 lines (61 loc) · 2.23 KB
/
0044_convert_user_credentials_data.py
File metadata and controls
70 lines (61 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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}")
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
),
]