def _get_phenotips_username_and_password(user, project, permissions_level): """Checks if user has permission to access the given project, and raises an exception if not. Args: user (User): the django user object project(Model): Project model permissions_level (string): 'edit' or 'view' Raises: PermissionDenied: if user doesn't have permission to access this project. Returns: 2-tuple: PhenoTips username, password that can be used to access patients in this project. """ if permissions_level == CAN_EDIT: uname, pwd = get_phenotips_uname_and_pwd_for_project( project.phenotips_user_id, read_only=False) elif permissions_level == CAN_VIEW: uname, pwd = get_phenotips_uname_and_pwd_for_project( project.phenotips_user_id, read_only=True) else: raise ValueError("Unexpected auth_permissions value: %s" % permissions_level) auth_tuple = (uname, pwd) return auth_tuple
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 handle(self, *args, **options): from_project = Project.objects.get(guid=options['from_project']) to_project = Project.objects.get(guid=options['to_project']) family_ids = options['family_ids'] families = Family.objects.filter(project=from_project, family_id__in=family_ids) print('Found {} out of {} families. No match for: {}.'.format( len(families), len(set(family_ids)), set(family_ids) - set([f.family_id for f in families]))) for f in families: print("==> Moving phenotips for {}".format(f)) for seqr_individual in f.individual_set.all(): if phenotips_patient_exists(seqr_individual): # make sure phenotips_patient_id is up to date data_json = _get_patient_data( from_project, seqr_individual, ) seqr_individual.phenotips_patient_id = data_json["id"] seqr_individual.save() # update permissions phenotips_readonly_username, _ = get_phenotips_uname_and_pwd_for_project( to_project.phenotips_user_id, read_only=True) _add_user_to_patient(phenotips_readonly_username, seqr_individual.phenotips_patient_id, allow_edit=False) phenotips_readwrite_username, _ = get_phenotips_uname_and_pwd_for_project( to_project.phenotips_user_id, read_only=False) _add_user_to_patient(phenotips_readwrite_username, seqr_individual.phenotips_patient_id, allow_edit=True) for variant_tag_type in VariantTagType.objects.filter( project=from_project): variant_tags = VariantTag.objects.filter( saved_variant__family__in=families, variant_tag_type=variant_tag_type) if variant_tags: print('Updating "{}" tags'.format(variant_tag_type.name)) to_tag_type, created = VariantTagType.objects.get_or_create( project=to_project, name=variant_tag_type.name) if created: to_tag_type.category = variant_tag_type.category to_tag_type.description = variant_tag_type.description to_tag_type.color = variant_tag_type.color to_tag_type.order = variant_tag_type.order to_tag_type.save() variant_tags.update(variant_tag_type=to_tag_type) print("Updating families") families.update(project=to_project) print("Done.")
def _enable_phenotips_for_project(project): """Creates 2 users in PhenoTips for this project (one that will be view-only and one that'll have edit permissions for patients in the project). """ project.is_phenotips_enabled = True project.phenotips_user_id = _slugify(project.name) # view-only user username, password = get_phenotips_uname_and_pwd_for_project(project.phenotips_user_id, read_only=True) create_phenotips_user(username, password) # user with edit permissions username, password = get_phenotips_uname_and_pwd_for_project(project.phenotips_user_id, read_only=False) create_phenotips_user(username, password) project.save()
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 _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)