Skip to content

Instantly share code, notes, and snippets.

@fredkingham
fredkingham / merge_episode.py
Created March 17, 2023 14:35
Merge episode
from intrahospital_api import merge_patient
from opal import models as opal_models
from django.db import transaction
from django.db.models import Count
import reversion
def update_singleton(subrecord_cls, old_parent, new_parent):
if new_parent.__class__ == opal_models.Episode:
@fredkingham
fredkingham / times_reviews.py
Created February 8, 2023 11:29
A quick and easy review of timings for lab test loads.
from django.core.management.base import BaseCommand
"""
A script to look at timings for loads
"""
import time
from django.utils import timezone
import datetime
from django.conf import settings
import pytds
@fredkingham
fredkingham / delete_old_module.sql
Created December 21, 2022 10:21
Deletes the lab module
DELETE FROM django_migrations WHERE app='lab';
DELETE FROM auth_permission WHERE content_type_id in (SELECT id FROM django_content_type WHERE app_label='lab');
DELETE FROM django_admin_log WHERE content_type_id in (SELECT id FROM django_content_type WHERE app_label = 'lab');
DELETE FROM reversion_version WHERE content_type_id in (
SELECT id FROM django_content_type WHERE app_label = 'lab'
);
DELETE FROM django_content_type WHERE app_label='lab';
DROP TABLE lab_labtest CASCADE;
DROP TABLE lab_labtest_resistant_antibiotics CASCADE;
@fredkingham
fredkingham / 107_delete_patients.py
Last active December 21, 2022 10:45
delete zero prefixed patients or their counterparts where at least one does not have elcid native entries
from plugins.covid import calculator
from opal.models import Patient
from elcid.models import Demographics
from intrahospital_api import loader
from opal.core import subrecords
from django.db import transaction
from django.core.management import call_command
from plugins.dischargesummary import loader as dicharge_summary_loader
@fredkingham
fredkingham / merge_delete_the_easy_patients.py
Last active November 29, 2022 14:07
Merge or delete the easy non zero patients
from opal.models import Patient
from elcid.models import Demographics
from intrahospital_api import loader
from opal.core import subrecords
from django.db import transaction
@transaction.atomic
def update_patients_with_leading_zero_with_no_counter_part():
# patients with leading 0s but no duplicate, remove the 0, re-sync all upstream
@fredkingham
fredkingham / merge_patients.py
Last active November 24, 2022 09:26
Merges patients that have leading zeros.
from django.db import transaction
from opal.core import subrecords
from intrahospital_api import loader
from elcid.models import Demographics
from opal.models import Patient
from plugins.labtests.models import LabTest
subrecordsToIgnore = set([
'InitialPatientLoad',
@fredkingham
fredkingham / find_duplicates.py
Created November 15, 2022 11:56
Finds duplicates with the same hospital number and categorises whether they have elcid native data
from django.core.management.base import BaseCommand
from django.db.models import Count
from opal.core import subrecords
from opal.models import Patient
from elcid.models import Demographics
import logging
logger = logging.getLogger('intrahospital_api')
import datetime
from django.db import transaction
from collections import defaultdict
from django.utils import timezone
from elcid.utils import timing
from elcid import models
from django.conf import settings
from django.db.models import DateTimeField, DateField
from opal.core.fields import ForeignKeyOrFreeText
import csv
@fredkingham
fredkingham / check_transfer_histories.py
Created October 28, 2022 10:55
Check the transfer history counts to make sure we are not way out
"""
Management command that sends a sanity check about the transfer histories
"""
import datetime
from django.conf import settings
from django.db.models import Max
from django.core.management import BaseCommand
from django.utils import timezone
from intrahospital_api.apis.prod_api import ProdApi as ProdAPI
from plugins.admissions.models import TransferHistory
import datetime
from collections import defaultdict
from elcid.models import Demographics
from django.db import transaction
from django.db.models import DateTimeField, Count, BooleanField, DateField
from django.utils import timezone
from plugins.admissions.models import TransferHistory
from django.conf import settings
from django.contrib.auth.models import User
from plugins.ipc.episode_categories import IPCEpisode