-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0057_auto_20230705_1319.py
More file actions
60 lines (51 loc) · 2.05 KB
/
Copy path0057_auto_20230705_1319.py
File metadata and controls
60 lines (51 loc) · 2.05 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
# Generated by Django 4.1.5 on 2023-06-13 18:48
import ast
import json
import logging
from django.db import migrations
logger = logging.getLogger(__name__)
fields_to_convert = [
"cmr_projects",
"cmr_dates",
"cmr_plats_and_insts",
"cmr_science_keywords",
"cmr_data_formats",
]
def convert_cmr_data_to_json(apps, schema_editor):
DOI = apps.get_model("data_models", "DOI")
dois = DOI.objects.all()
for doi in dois:
for field in fields_to_convert:
logger.info(f"Converting {field} on {doi.uuid} to JSON")
d = getattr(doi, field)
if d is None or d == "":
logger.info(f"Field {field} is empty, setting to null")
json_data = None
else:
if isinstance(d, str):
try:
json_data = json.loads(d)
except json.decoder.JSONDecodeError:
logger.info(
f"Failed to parse JSON on {doi.uuid} field {field}, attempting to convert to JSON from serialized python object {d}"
)
try:
if field == "cmr_data_formats" and "[" not in d:
d = f'["{d}"]'
decoded_string = d.encode("utf-8").decode('unicode_escape')
json_data = ast.literal_eval(decoded_string)
except Exception as err:
logger.error(f"Failed to convert {field} on {doi.uuid} to JSON: {err}")
logger.error(f"Value: {decoded_string}")
raise err
else:
json_data = d
setattr(doi, field, json_data)
doi.save()
class Migration(migrations.Migration):
dependencies = [
('data_models', '0056_alter_doi_cmr_data_formats_alter_doi_cmr_dates_and_more'),
]
operations = [
migrations.RunPython(convert_cmr_data_to_json, reverse_code=migrations.RunPython.noop)
]