-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use TextField instead of JsonField for comma separated lists * pylance vs pyright * Test migrations * Rename validator * Improve lines validator * Adjust lines separator
- Loading branch information
Showing
40 changed files
with
581 additions
and
109 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
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
52 changes: 52 additions & 0 deletions
52
adit/batch_query/migrations/0015_alter_batchqueryresult_modalities_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,52 @@ | ||
# Generated by Django 4.2.2 on 2023-06-23 08:25 | ||
|
||
import adit.core.validators | ||
import django.core.validators | ||
from django.db import migrations, models | ||
import re | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("batch_query", "0014_alter_batchqueryresult_series_number"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="batchqueryresult", | ||
name="modalities", | ||
field=models.TextField( | ||
blank=True, default="", validators=[adit.core.validators.validate_modalities] | ||
), | ||
preserve_default=False, | ||
), | ||
migrations.AlterField( | ||
model_name="batchquerytask", | ||
name="lines", | ||
field=models.TextField( | ||
validators=[ | ||
django.core.validators.RegexValidator( | ||
re.compile("^\\d+(?:,\\d+)*\\Z"), | ||
code="invalid", | ||
message="Enter only digits separated by commas.", | ||
) | ||
] | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="batchquerytask", | ||
name="modalities", | ||
field=models.TextField( | ||
blank=True, default="", validators=[adit.core.validators.validate_modalities] | ||
), | ||
preserve_default=False, | ||
), | ||
migrations.AlterField( | ||
model_name="batchquerytask", | ||
name="series_numbers", | ||
field=models.TextField( | ||
blank=True, default="", validators=[adit.core.validators.validate_series_numbers] | ||
), | ||
preserve_default=False, | ||
), | ||
] |
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,73 @@ | ||
# Generated by Django 4.2.2 on 2023-06-23 09:46 | ||
|
||
import json | ||
from django.apps import AppConfig | ||
from django.db import migrations | ||
|
||
|
||
def convert_json_to_text(apps: AppConfig, schema_editor): | ||
BatchQueryTask = apps.get_model("batch_query.BatchQueryTask") | ||
for query in BatchQueryTask.objects.all(): | ||
if not query.lines: | ||
query.lines = "" | ||
else: | ||
query.lines = ", ".join(json.loads(query.lines)) | ||
|
||
if not query.modalities: | ||
query.modalities = "" | ||
else: | ||
query.modalities = ", ".join(json.loads(query.modalities)) | ||
|
||
if not query.series_numbers: | ||
query.series_number = "" | ||
else: | ||
query.series_numbers = ", ".join(json.loads(query.series_numbers)) | ||
|
||
query.save() | ||
|
||
BatchQueryResult = apps.get_model("batch_query.BatchQueryResult") | ||
for result in BatchQueryResult.objects.all(): | ||
if not result.modalities: | ||
result.modalities = "" | ||
else: | ||
result.modalities = ", ".join(json.loads(result.modalities)) | ||
|
||
result.save() | ||
|
||
|
||
def convert_text_to_json(apps: AppConfig, schema_editor): | ||
BatchQueryTask = apps.get_model("batch_query.BatchQueryTask") | ||
for query in BatchQueryTask.objects.all(): | ||
if not query.lines: | ||
query.lines = "[]" | ||
else: | ||
query.lines = json.dumps(list(map(str.strip, query.lines.split(",")))) | ||
|
||
if not query.modalities: | ||
query.modalities = "null" | ||
else: | ||
query.modalities = json.dumps(list(map(str.strip, query.modalities.split(",")))) | ||
|
||
if not query.series_numbers: | ||
query.series_number = "null" | ||
else: | ||
query.series_numbers = json.dumps(list(map(str.strip, query.series_numbers.split(",")))) | ||
|
||
query.save() | ||
|
||
BatchQueryResult = apps.get_model("batch_query.BatchQueryResult") | ||
for result in BatchQueryResult.objects.all(): | ||
if not result.modalities: | ||
result.modalities = None | ||
else: | ||
result.modalities = json.dumps(list(map(str.strip, result.modalities.split(",")))) | ||
|
||
result.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("batch_query", "0015_alter_batchqueryresult_modalities_and_more"), | ||
] | ||
|
||
operations = [migrations.RunPython(convert_json_to_text, convert_text_to_json)] |
25 changes: 25 additions & 0 deletions
25
adit/batch_query/migrations/0017_alter_batchquerytask_lines.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,25 @@ | ||
# Generated by Django 4.2.2 on 2023-06-25 10:57 | ||
|
||
import django.core.validators | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("batch_query", "0016_convert_json_to_text"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="batchquerytask", | ||
name="lines", | ||
field=models.TextField( | ||
validators=[ | ||
django.core.validators.RegexValidator( | ||
message="Enter only digits separated by commas.", | ||
regex="^\\s*\\d+(?:\\s*,\\s*\\d+)*\\s*\\Z", | ||
) | ||
] | ||
), | ||
), | ||
] |
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,48 @@ | ||
# type: ignore | ||
|
||
import pytest | ||
from django_test_migrations.migrator import Migrator | ||
|
||
from ..factories import BatchQueryJobFactory | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_0016_convert_json_to_text(migrator: Migrator): | ||
old_state = migrator.apply_initial_migration( | ||
("batch_query", "0015_alter_batchqueryresult_modalities_and_more") | ||
) | ||
BatchQueryTask = old_state.apps.get_model("batch_query", "BatchQueryTask") | ||
BatchQueryResult = old_state.apps.get_model("batch_query", "BatchQueryResult") | ||
|
||
job = BatchQueryJobFactory.create() | ||
query = BatchQueryTask.objects.create( | ||
job_id=job.id, | ||
task_id="123", | ||
lines='["1", "2", "3"]', | ||
modalities='["CT", "MR"]', | ||
series_numbers='["4", "5", "6"]', | ||
) | ||
result = BatchQueryResult.objects.create( | ||
job_id=job.id, | ||
query_id=query.id, | ||
patient_id="12345", | ||
patient_name="Doe^John", | ||
patient_birth_date="1980-01-01", | ||
study_date="2010-01-01", | ||
study_time="12:00", | ||
modalities='["CT", "MR"]', | ||
study_uid="1.2.3", | ||
) | ||
|
||
new_state = migrator.apply_tested_migration(("batch_query", "0016_convert_json_to_text")) | ||
BatchQueryTask = new_state.apps.get_model("batch_query", "BatchQueryTask") | ||
BatchQueryResult = new_state.apps.get_model("batch_query", "BatchQueryResult") | ||
|
||
query = BatchQueryTask.objects.get(id=query.id) | ||
result = BatchQueryResult.objects.get(id=result.id) | ||
|
||
assert query.lines == "1, 2, 3" | ||
assert query.modalities == "CT, MR" | ||
assert query.series_numbers == "4, 5, 6" | ||
|
||
assert result.modalities == "CT, MR" |
Oops, something went wrong.