Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions codelists/migrations/0064_move_and_publish_ukhsa_ssi_codelists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Generated by Django 5.2.14 on 2026-05-15 15:59
from functools import partial
from django.db import migrations
from codelists.actions import update_codelist, publish_version
from codelists.models import Status
from opencodelists.models import Organisation


def move_codelists(codelists, to_org):
for codelist in codelists:
update_codelist(
codelist=codelist,
owner=to_org,
name=codelist.current_handle.name,
slug=codelist.current_handle.slug,
description=codelist.description,
methodology=codelist.methodology,
references=codelist.references.all(),
signoffs=codelist.signoffs.all(),
)


def publish_codelist_versions(codelists):
for codelist in codelists:
for version in codelist.versions.filter(status=Status.UNDER_REVIEW):
version.status = Status.PUBLISHED
version.save()


def unpublish_codelist_versions(codelists):
for codelist in codelists:
for version in codelist.versions.filter(status=Status.PUBLISHED):
version.status = Status.UNDER_REVIEW
version.save()


def move_and_publish(apps, schema_editor, reverse=False):
try:
oxhpru = Organisation.objects.get(slug="OxHPRU")
ukhsa = Organisation.objects.get(slug="ukhsa")
except Organisation.DoesNotExist:
print("Organisation for SSI codelists not found")
return
if reverse:
from_org = oxhpru
to_org = ukhsa
else:
from_org = ukhsa
to_org = oxhpru
codelists = [
codelist
for codelist in from_org.codelists.filter(coding_system_id="opcs4")
if reverse
or all(cv.status == Status.UNDER_REVIEW for cv in codelist.versions.all())
]
move_codelists(codelists=codelists, to_org=to_org)
if reverse:
unpublish_codelist_versions(codelists=codelists)
else:
publish_codelist_versions(codelists=codelists)


class Migration(migrations.Migration):
"""
Moves the Surgical Site Infection codelists from the UKHSA organisation to the OxHPRU one and publishes them.
These are identified as all under review OPCS4 codelists
"""

dependencies = [
("codelists", "0063_alter_handle_name"),
]

operations = [
migrations.RunPython(
move_and_publish,
reverse_code=partial(move_and_publish, reverse=True),
),
]