def update_complications_from_baseline(apps, schema_editor): """Splits the complications model into baseline and followup.""" complications_model_cls = apps.get_model( "inte_subject.complicationsbaseline") complications_followup_model_cls = apps.get_model( "inte_subject.complicationsfollowup") clinical_review_model_cls = apps.get_model("inte_subject.clinicalreview") names = [ "stroke", "heart_attack", "renal_disease", "vision", "numbness", "foot_ulcers", ] for clinical_review in clinical_review_model_cls.objects.all(): try: complications = complications_model_cls.objects.get( subject_visit=clinical_review.subject_visit) except ObjectDoesNotExist: clinical_review.complications = NO else: clinical_review.complications = YES complications_followup = complications_followup_model_cls() complications_followup.subject_visit = clinical_review.subject_visit complications_followup.created = clinical_review.created complications_followup.modified = clinical_review.modified complications_followup.user_created = clinical_review.user_created complications_followup.user_modified = clinical_review.user_modified complications_followup.hostname_created = clinical_review.hostname_created complications_followup.hostname_modified = clinical_review.hostname_modified complications_followup.device_created = clinical_review.device_created complications_followup.device_modified = clinical_review.device_modified complications_followup.consent_model = clinical_review.consent_model complications_followup.consent_version = clinical_review.consent_version complications_followup.site_id = clinical_review.site_id for name in names: setattr(complications_followup, name, getattr(complications, name)) setattr( complications_followup, f"{name}_date", getattr(complications, f"{name}_estimated_date"), ) complications_followup.complications = complications.complications complications_followup.other_complications = complications.other_complications with DisableSignals(disabled_signals=[pre_save]): complications_followup.save() complications.delete() with DisableSignals(disabled_signals=[pre_save]): clinical_review.save() print( "\nDone. Remember to run the INTE managment command to update metadata." )
def update_appointment_schedule(apps=None): apps = apps or django_apps registered_subject_model_cls = apps.get_model( "edc_registration.registeredsubject") appointment_model_cls = apps.get_model("edc_appointment.appointment") subject_visit_model_cls = apps.get_model("inte_subject.subjectvisit") crfmetadata_model_cls = apps.get_model("edc_metadata.crfmetadata") for registered_subject in tqdm( registered_subject_model_cls.objects.all().order_by( "subject_identifier")): for index, appointment in enumerate( appointment_model_cls.objects.filter( subject_identifier=registered_subject.subject_identifier, ).order_by("timepoint")): if appointment.timepoint in [ Decimal("0.0"), Decimal("6.0"), Decimal("12.0"), ]: continue elif appointment.timepoint < Decimal("6.0"): appointment.visit_code = "1000" appointment.visit_code_sequence = index appointment.timepoint = Decimal("0.0") with DisableSignals(disabled_signals=[pre_save]): appointment.save() appointment.refresh_from_db() try: subject_visit = subject_visit_model_cls.objects.get( appointment=appointment) except ObjectDoesNotExist: appointment.delete() else: crfmetadata_model_cls.objects.filter( subject_identifier=registered_subject. subject_identifier, visit_code=subject_visit.visit_code, visit_code_sequence=subject_visit.visit_code_sequence, ).update( visit_code=appointment.visit_code, visit_code_sequence=appointment.visit_code_sequence, timepoint=appointment.timepoint, ) with DisableSignals(disabled_signals=[pre_save]): subject_visit.visit_code = appointment.visit_code subject_visit.visit_code_sequence = appointment.visit_code_sequence subject_visit.save() else: with DisableSignals(disabled_signals=[pre_delete]): appointment.delete() for appointment in appointment_model_cls.objects.filter( visit_code_sequence__gt=0): try: subject_visit_model_cls.objects.get(appointment=appointment) except ObjectDoesNotExist: appointment.delete()
def convert_investigations_to_clinical_review(apps, schema_editor): try: investigations_model_cls = apps.get_model( "inte_subject.investigations") except LookupError: pass else: clinical_review_model_cls = apps.get_model( "inte_subject.clinicalreview") for obj in investigations_model_cls.objects.all(): opts = dict( created=obj.created, modified=obj.modified, user_created=obj.user_created, user_modified=obj.user_modified, hostname_created=obj.hostname_created, hostname_modified=obj.hostname_modified, device_created=obj.device_created, device_modified=obj.device_modified, consent_model=obj.consent_model, consent_version=obj.consent_version, site_id=obj.site_id, subject_visit_id=obj.subject_visit_id, report_datetime=obj.report_datetime, hiv_tested=obj.hiv_tested, hypertension_tested=obj.hypertension_tested, diabetes_tested=obj.diabetes_tested, crf_status=obj.crf_status, crf_status_comments=obj.crf_status_comments, ) clinical_review = clinical_review_model_cls() for k, v in opts.items(): setattr(clinical_review, k, v) with DisableSignals(disabled_signals=[pre_save]): clinical_review.save()
def update_from_reason_for_visit(apps, schema_editor): subject_visit_model_cls = apps.get_model("inte_subject.subjectvisit") reason_for_visit_model_cls = apps.get_model("inte_subject.reasonforvisit") medications_model_cls = apps.get_model("inte_subject.medications") for reason_for_visit in reason_for_visit_model_cls.objects.all(): subject_visit = subject_visit_model_cls.objects.get( id=reason_for_visit.subject_visit_id) for obj in reason_for_visit.clinic_services.all(): subject_visit.clinic_services.add(obj) for obj in reason_for_visit.health_services.all(): subject_visit.health_services.add(obj) medications = medications_model_cls() medications.created = reason_for_visit.created medications.modified = reason_for_visit.modified medications.user_created = reason_for_visit.user_created medications.user_modified = reason_for_visit.user_modified medications.hostname_created = reason_for_visit.hostname_created medications.hostname_modified = reason_for_visit.hostname_modified medications.device_created = reason_for_visit.device_created medications.device_modified = reason_for_visit.device_modified medications.consent_model = reason_for_visit.consent_model medications.consent_version = reason_for_visit.consent_version medications.site_id = reason_for_visit.site_id medications.subject_visit_id = reason_for_visit.subject_visit_id medications.report_datetime = reason_for_visit.report_datetime medications.refill_hiv = reason_for_visit.refill_hiv medications.refill_diabetes = reason_for_visit.refill_diabetes medications.refill_hypertension = reason_for_visit.refill_hypertension medications.crf_status = reason_for_visit.crf_status medications.crf_status_comments = reason_for_visit.crf_status_comments with DisableSignals(disabled_signals=[pre_save]): medications.save() subject_visit.save()
def update_subjectvisit(apps, schema_editor): """Adds or removes selection on subject visit clinic services according to study timepoint. * Adds STUDY_DEFINED_TIMEPOINT if scheduled timepoint (XXXX.0) * Removes if not a scheduled timepoint (XXXX.1+) """ subject_visit_model_cls = apps.get_model("inte_subject.subjectvisit") clinical_services_model_cls = apps.get_model("inte_lists.clinicservices") study_defined_timepoint = clinical_services_model_cls.objects.get( name=STUDY_DEFINED_TIMEPOINT ) for subject_visit in subject_visit_model_cls.objects.all(): if ( subject_visit.visit_code_sequence == 0 and not subject_visit.clinic_services.filter(name=STUDY_DEFINED_TIMEPOINT).exists() ): subject_visit.clinic_services.add(study_defined_timepoint) elif ( subject_visit.visit_code_sequence != 0 and subject_visit.clinic_services.filter(name=STUDY_DEFINED_TIMEPOINT).exists() ): subject_visit.clinic_services.remove(study_defined_timepoint) with DisableSignals(disabled_signals=[pre_save]): subject_visit.save()
def convert_clinical_review_hiv_dx(apps, schema_editor): clinical_review_baseline_model_cls = apps.get_model( "inte_subject.clinicalreviewbaseline") for obj in clinical_review_baseline_model_cls.objects.all(): hiv_status = copy(obj.hiv_test) if hiv_status == NEVER: obj.hiv_test = NO obj.hiv_dx = NOT_APPLICABLE with DisableSignals(disabled_signals=[pre_save]): obj.save() elif hiv_status == POS: obj.hiv_test = YES obj.hiv_dx = YES with DisableSignals(disabled_signals=[pre_save]): obj.save() elif hiv_status == NEG: obj.hiv_test = YES obj.hiv_dx = NO with DisableSignals(disabled_signals=[pre_save]): obj.save() else: raise DataMigrationError( f"Invalid response. Got hiv_test==`{hiv_status}`")
def update_medications(apps, schema_editor): medications_model_cls = apps.get_model("inte_subject.medications") refill_hiv_model_cls = apps.get_model("inte_subject.drugrefillhiv") refill_htn_model_cls = apps.get_model("inte_subject.drugrefillhtn") refill_dm_model_cls = apps.get_model("inte_subject.drugrefilldm") subject_visit_model_cls = apps.get_model("inte_subject.subjectvisit") for subject_visit in subject_visit_model_cls.objects.filter( visit_code_sequence__gt=0).order_by("subject_identifier", "visit_code", "visit_code_sequence"): try: diagnoses = Diagnoses(subject_visit=subject_visit) except ClinicalReviewBaselineRequired: print( f" * {subject_visit.subject_identifier}: ClinicalReviewBaselineRequired (skipping) [0060]" ) else: medications = get_obj(medications_model_cls, subject_visit) opts = [ ("hiv_dx", "refill_hiv", refill_hiv_model_cls), ("htn_dx", "refill_htn", refill_htn_model_cls), ("dm_dx", "refill_dm", refill_dm_model_cls), ] if medications: for dx_attr, refill_attr, refill_model_cls in opts: if getattr(diagnoses, dx_attr): obj = get_obj(refill_model_cls, subject_visit) if obj: setattr(medications, refill_attr, YES) else: setattr(medications, refill_attr, NO) else: setattr(medications, refill_attr, NOT_APPLICABLE) with DisableSignals([pre_save]): medications.save() print( "\nDone. Remember to run the INTE managment command to update metadata." )
def update_visit_and_metadata(apps, schema_editor): subject_visit_model_cls = apps.get_model("inte_subject.subjectvisit") for subject_visit in subject_visit_model_cls.objects.all(): with DisableSignals(disabled_signals=[pre_save]): subject_visit.save()