def _add_user_to_patient(username, patient_id, allow_edit=True): """Grant a PhenoTips user access to the given patient. Args: username (string): PhenoTips username to grant access to. patient_id (string): PhenoTips internal patient id. """ headers = {"Content-Type": "application/x-www-form-urlencoded"} data = { 'collaborator': 'XWiki.' + str(username), 'patient': patient_id, 'accessLevel': 'edit' if allow_edit else 'view', 'xaction': 'update', 'submit': 'Update' } url = '/bin/get/PhenoTips/PatientAccessRightsManagement?outputSyntax=plain' make_phenotips_api_call( 'POST', url, http_headers=headers, data=data, auth_tuple=(settings.PHENOTIPS_ADMIN_UNAME, settings.PHENOTIPS_ADMIN_PWD), expected_status_code=204, parse_json_resonse=False, )
def update_individual_hpo_terms(request, individual_guid): individual = Individual.objects.get(guid=individual_guid) project = individual.family.project check_permissions(project, request.user, CAN_EDIT) features = json.loads(request.body) _create_patient_if_missing(project, individual) patient_json = _get_patient_data(project, individual) patient_json["features"] = features patient_json_string = json.dumps(patient_json) url = phenotips_patient_url(individual) auth_tuple = get_phenotips_uname_and_pwd_for_project(project.phenotips_user_id, read_only=False) make_phenotips_api_call('PUT', url, data=patient_json_string, auth_tuple=auth_tuple, expected_status_code=204) phenotips_patient_id = patient_json['id'] phenotips_eid = patient_json.get('external_id') individual.phenotips_data = json.dumps(patient_json) individual.phenotips_patient_id = phenotips_patient_id individual.phenotips_eid = phenotips_eid individual.save() return create_json_response({ individual.guid: { 'phenotipsData': patient_json, 'phenotipsPatientId': phenotips_patient_id, 'phenotipsEid': phenotips_eid } })
def _create_patient_if_missing(project, individual): """Create a new PhenoTips patient record with the given patient id. Args: project (Model): seqr Project - used to retrieve PhenoTips credentials individual (Model): seqr Individual Returns: True if patient created Raises: PhenotipsException: if unable to create patient record """ if phenotips_patient_exists(individual): return False url = '/rest/patients' headers = {"Content-Type": "application/json"} data = json.dumps({'external_id': individual.guid}) auth_tuple = get_phenotips_uname_and_pwd_for_project(project.phenotips_user_id) response_items = make_phenotips_api_call('POST', url, auth_tuple=auth_tuple, http_headers=headers, data=data, expected_status_code=201, parse_json_resonse=False) patient_id = response_items['Location'].split('/')[-1] logger.info("Created PhenoTips record with patient id {patient_id} and external id {external_id}".format(patient_id=patient_id, external_id=individual.guid)) username_read_only, _ = get_phenotips_uname_and_pwd_for_project(project.phenotips_user_id, read_only=True) _add_user_to_patient(username_read_only, patient_id, allow_edit=False) logger.info("Added PhenoTips user {username} to {patient_id}".format(username=username_read_only, patient_id=patient_id)) individual.phenotips_patient_id = patient_id individual.phenotips_eid = individual.guid individual.save() return True
def _get_patient_data(project, individual): """Retrieves patient data from PhenoTips and returns a json obj. Args: project (Model): seqr Project - used to retrieve PhenoTips credentials individual (Model): seqr Individual Returns: dict: json dictionary containing all PhenoTips information for this patient Raises: PhenotipsException: if unable to retrieve data from PhenoTips """ url = phenotips_patient_url(individual) auth_tuple = get_phenotips_uname_and_pwd_for_project(project.phenotips_user_id) return make_phenotips_api_call('GET', url, auth_tuple=auth_tuple, verbose=False)