|
| 1 | +# Generated by Django 5.2.14 on 2026-05-15 15:59 |
| 2 | +from functools import partial |
| 3 | +from django.db import migrations |
| 4 | +from codelists.actions import update_codelist, publish_version |
| 5 | +from codelists.models import Status |
| 6 | +from opencodelists.models import Organisation |
| 7 | + |
| 8 | + |
| 9 | +def move_codelists(codelists, to_org): |
| 10 | + for codelist in codelists: |
| 11 | + update_codelist( |
| 12 | + codelist=codelist, |
| 13 | + owner=to_org, |
| 14 | + name=codelist.current_handle.name, |
| 15 | + slug=codelist.current_handle.slug, |
| 16 | + description=codelist.description, |
| 17 | + methodology=codelist.methodology, |
| 18 | + references=codelist.references.all(), |
| 19 | + signoffs=codelist.signoffs.all(), |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +def publish_codelist_versions(codelists): |
| 24 | + for codelist in codelists: |
| 25 | + for version in codelist.versions.filter(status=Status.UNDER_REVIEW): |
| 26 | + version.status = Status.PUBLISHED |
| 27 | + version.save() |
| 28 | + |
| 29 | + |
| 30 | +def unpublish_codelist_versions(codelists): |
| 31 | + for codelist in codelists: |
| 32 | + for version in codelist.versions.filter(status=Status.UNDER_REVIEW): |
| 33 | + version.status = Status.UNDER_REVIEW |
| 34 | + version.save() |
| 35 | + |
| 36 | + |
| 37 | +def move_and_publish(apps, schema_editor, reverse=False): |
| 38 | + try: |
| 39 | + oxhpru = Organisation.objects.get(slug="OxHPRU") |
| 40 | + ukhsa = Organisation.objects.get(slug="ukhsa") |
| 41 | + except Organisation.DoesNotExist: |
| 42 | + return |
| 43 | + if reverse: |
| 44 | + from_org = oxhpru |
| 45 | + to_org = ukhsa |
| 46 | + print("Organisation for SSI codelists not found") |
| 47 | + else: |
| 48 | + from_org = ukhsa |
| 49 | + to_org = oxhpru |
| 50 | + codelists = [ |
| 51 | + codelist |
| 52 | + for codelist in from_org.codelists.filter(coding_system_id="opcs4") |
| 53 | + if not reverse |
| 54 | + and all(cv.status == Status.UNDER_REVIEW for cv in codelist.versions.all()) |
| 55 | + ] |
| 56 | + move_codelists(codelists=codelists, to_org=to_org) |
| 57 | + if reverse: |
| 58 | + unpublish_codelist_versions(codelists=codelists) |
| 59 | + else: |
| 60 | + publish_codelist_versions(codelists=codelists) |
| 61 | + |
| 62 | + |
| 63 | +class Migration(migrations.Migration): |
| 64 | + """ |
| 65 | + Moves the Surgical Site Infection codelists from the UKHSA organisation to the OxHPRU one and publishes them. |
| 66 | + These are identified as all under review OPCS4 codelists |
| 67 | + """ |
| 68 | + |
| 69 | + dependencies = [ |
| 70 | + ("codelists", "0063_alter_handle_name"), |
| 71 | + ] |
| 72 | + |
| 73 | + operations = [ |
| 74 | + migrations.RunPython( |
| 75 | + move_and_publish, |
| 76 | + reverse_code=partial(move_and_publish, reverse=True), |
| 77 | + ), |
| 78 | + ] |
0 commit comments