-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #523 from NASA-IMPACT/bugfix/casei-538-jsonfields
Restore JSONField columns on DOI model.
- Loading branch information
Showing
15 changed files
with
261 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
__pycache__ | ||
.github | ||
.pytest_cache | ||
env | ||
out | ||
staticfiles | ||
.dockerignore | ||
.env | ||
.env.* | ||
*.json | ||
docker-compose* | ||
Dockerfile* | ||
.backup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,3 +290,5 @@ cmr/config.py | |
*.sql | ||
hosts/* | ||
!hosts/*.sample | ||
|
||
.backup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import pytest | ||
from django.urls import reverse | ||
|
||
from admin_ui.tables.tables import ShortNamefromUUIDColumn | ||
from admin_ui.tests.factories import ChangeFactory | ||
from data_models.tests.factories import CampaignFactory | ||
from data_models.models import Campaign | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.parametrize( | ||
"model_name", | ||
[ | ||
"campaign", | ||
"doi", | ||
"collection_period", | ||
], | ||
) | ||
class TestPublished: | ||
def test_get(self, client, model_name): | ||
url = reverse("published-list", args=(model_name,)) | ||
response = client.get(url) | ||
assert 200 == response.status_code | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestShortNameFromUUIDColumn: | ||
def test_get_names_from_concrete_model(self): | ||
campaign = CampaignFactory() | ||
column = ShortNamefromUUIDColumn(Campaign) | ||
assert column.get_short_names([str(campaign.uuid)])[0] == campaign.short_name | ||
|
||
def test_get_name_from_concrete_model(self): | ||
campaign = CampaignFactory() | ||
column = ShortNamefromUUIDColumn(Campaign) | ||
assert column.get_short_name(str(campaign.uuid)) == campaign.short_name | ||
|
||
def test_get_names_from_change_model(self): | ||
change = ChangeFactory.make_create_change_object( | ||
CampaignFactory, custom_fields={"short_name": "test"} | ||
) | ||
column = ShortNamefromUUIDColumn(Campaign) | ||
assert column.get_short_names([str(change.uuid)])[0] == change.update["short_name"] | ||
|
||
def test_get_name_from_change_model(self): | ||
change = ChangeFactory.make_create_change_object( | ||
CampaignFactory, custom_fields={"short_name": "test"} | ||
) | ||
column = ShortNamefromUUIDColumn(Campaign) | ||
assert column.get_short_name(str(change.uuid)) == change.update["short_name"] | ||
|
||
def test_get_names_from_non_uuid_values(self): | ||
""" | ||
Should return the provided values intact | ||
""" | ||
column = ShortNamefromUUIDColumn(Campaign) | ||
assert column.get_short_names(["test"]) == ["test"] | ||
|
||
def test_get_name_from_non_uuid_value(self): | ||
""" | ||
Should return the provided value intact | ||
""" | ||
column = ShortNamefromUUIDColumn(Campaign) | ||
assert column.get_short_name("test") == "test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# 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 = "null" | ||
else: | ||
try: | ||
json_data = json.loads(d) | ||
except json.decoder.JSONDecodeError: | ||
logger.info( | ||
f"Failed to parse JSON, attempting to convert to JSON from serialized python object {d}" | ||
) | ||
json_data = ast.literal_eval(d) | ||
setattr(doi, field, json.dumps(json_data)) | ||
doi.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('data_models', '0054_remove_instrument_additional_metadata'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(convert_cmr_data_to_json, reverse_code=migrations.RunPython.noop) | ||
] |
37 changes: 37 additions & 0 deletions
37
data_models/migrations/0056_alter_doi_cmr_data_formats_alter_doi_cmr_dates_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Generated by Django 4.1.5 on 2023-06-13 19:23 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('data_models', '0055_auto_20230613_1348'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='doi', | ||
name='cmr_data_formats', | ||
field=models.JSONField(blank=True, default=None, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name='doi', | ||
name='cmr_dates', | ||
field=models.JSONField(blank=True, default=None, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name='doi', | ||
name='cmr_plats_and_insts', | ||
field=models.JSONField(blank=True, default=None, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name='doi', | ||
name='cmr_projects', | ||
field=models.JSONField(blank=True, default=None, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name='doi', | ||
name='cmr_science_keywords', | ||
field=models.JSONField(blank=True, default=None, null=True), | ||
), | ||
] |
Oops, something went wrong.