|
| 1 | +from django.core.management import BaseCommand, call_command |
| 2 | + |
| 3 | +from coding_systems.versioning.models import ( |
| 4 | + CodingSystemRelease, |
| 5 | + build_db_path, |
| 6 | + update_coding_system_database_connections, |
| 7 | +) |
| 8 | +from opencodelists.tests.fixtures import build_fixtures |
| 9 | + |
| 10 | + |
| 11 | +class Command(BaseCommand): |
| 12 | + help = """Populates local opencodelists dbs with test fixture data: Loads the CodingSystemReleases needed for the snomed and dmd fixtures into the Core db (you will be prompted for consent to do this), deletes existing test coding system release dbs, creates new coding system release dbs, populates these with test fixture data, instantiates a universe of fixture objects, and creates a superuser. Running this command directly is not recommended, instead use `just build-dbs-for-local-development`""" |
| 13 | + |
| 14 | + def handle(self, *args, **options): |
| 15 | + load_versioning_fixture_answer = input( |
| 16 | + "Running this command will load the CodingSystemReleases needed for the snomed and dmd test fixtures, into the Core db. Any local test coding system release dbs will then be deleted and recreated. Do you want to proceed? (Y/n)" |
| 17 | + ) |
| 18 | + |
| 19 | + if load_versioning_fixture_answer == "n": |
| 20 | + return |
| 21 | + |
| 22 | + call_command( |
| 23 | + "loaddata", |
| 24 | + "coding_systems/versioning/fixtures/coding_system_releases.json", |
| 25 | + ) |
| 26 | + |
| 27 | + self.stdout.write("Deleting local coding system release dbs...") |
| 28 | + for coding_system_release in CodingSystemRelease.objects.all(): |
| 29 | + db_path = build_db_path(coding_system_release) |
| 30 | + if db_path.exists(): |
| 31 | + db_path.unlink() |
| 32 | + self.stdout.write(f"{str(db_path)} has been deleted") |
| 33 | + |
| 34 | + self.stdout.write( |
| 35 | + "Migrating coding system release dbs and populating them with test fixture data" |
| 36 | + ) |
| 37 | + |
| 38 | + update_coding_system_database_connections() |
| 39 | + |
| 40 | + # migrate snomedct test db and load test fixtures |
| 41 | + call_command("migrate", "snomedct", database="snomedct_test_20200101") |
| 42 | + |
| 43 | + call_command( |
| 44 | + "loaddata", |
| 45 | + "coding_systems/snomedct/fixtures/core-model-components.snomedct_test_20200101.json", |
| 46 | + database="snomedct_test_20200101", |
| 47 | + ) |
| 48 | + |
| 49 | + call_command( |
| 50 | + "loaddata", |
| 51 | + "coding_systems/snomedct/fixtures/tennis-elbow.snomedct_test_20200101.json", |
| 52 | + database="snomedct_test_20200101", |
| 53 | + ) |
| 54 | + |
| 55 | + call_command( |
| 56 | + "loaddata", |
| 57 | + "coding_systems/snomedct/fixtures/tennis-toe.snomedct_test_20200101.json", |
| 58 | + database="snomedct_test_20200101", |
| 59 | + ) |
| 60 | + |
| 61 | + # migrate dmd test db and load test fixture |
| 62 | + call_command("migrate", "dmd", database="dmd_test_20200101") |
| 63 | + |
| 64 | + call_command( |
| 65 | + "loaddata", |
| 66 | + "coding_systems/dmd/fixtures/asthma-medication.dmd_test_20200101.json", |
| 67 | + database="dmd_test_20200101", |
| 68 | + ) |
| 69 | + |
| 70 | + # migrate bnf test db and load test fixture |
| 71 | + call_command("migrate", "bnf", database="bnf_test_20200101") |
| 72 | + |
| 73 | + call_command( |
| 74 | + "loaddata", |
| 75 | + "coding_systems/bnf/fixtures/asthma.bnf_test_20200101.json", |
| 76 | + database="bnf_test_20200101", |
| 77 | + ) |
| 78 | + |
| 79 | + # migrate icd10 test db and load test fixture |
| 80 | + call_command("migrate", "icd10", database="icd10_test_20200101") |
| 81 | + call_command( |
| 82 | + "loaddata", |
| 83 | + "coding_systems/icd10/fixtures/icd10.icd10_test_20200101.json", |
| 84 | + database="icd10_test_20200101", |
| 85 | + ) |
| 86 | + |
| 87 | + # instantiate the test data universe |
| 88 | + build_fixtures() |
| 89 | + |
| 90 | + call_command( |
| 91 | + "createsuperuser", |
| 92 | + no_input=True, |
| 93 | + username="localdev", |
| 94 | + email="localdev@example.com", |
| 95 | + ) |
| 96 | + |
| 97 | + self.stdout.write( |
| 98 | + "Local setup complete! You can now:\n" |
| 99 | + " - Log in as `localdev`\n" |
| 100 | + " - Search for 'arthritis', 'tennis', and 'elbow'\n" |
| 101 | + " - Build codelists with the concepts returned from these searches (see /opencodelists/opencodelists/tests/fixtures.py for more info)\n" |
| 102 | + " - View a BNF codelist, a minimal codelist, and a new-style SNOMED CT codelist" |
| 103 | + ) |
0 commit comments