def update_study_associates(study_id, associates): """ updates the list of associates in the database for a study_id and a list of dicts that contains associates """ if study_id is None: raise ApiError('study_id not specified', "This function requires the study_id parameter") for person in associates: if not LdapService.user_exists(person.get('uid', 'impossible_uid')): if person.get('uid', 'impossible_uid') == 'impossible_uid': raise ApiError( 'associate with no uid', 'One of the associates passed as a parameter doesnt have ' 'a uid specified') raise ApiError( 'trying_to_grant_access_to_user_not_found_in_ldap', "You are trying to grant access to " "%s, but that user was not found in " "ldap " "- please check to ensure it is a " "valid uva uid" % person.get('uid')) study = db.session.query(StudyModel).filter( StudyModel.id == study_id).first() if study is None: raise ApiError('study_id not found', "A study with id# %d was not found" % study_id) db.session.query(StudyAssociated).filter( StudyAssociated.study_id == study_id).delete() for person in associates: newAssociate = StudyAssociated() newAssociate.study_id = study_id newAssociate.uid = person['uid'] newAssociate.role = person.get('role', None) newAssociate.send_email = person.get('send_email', False) newAssociate.access = person.get('access', False) session.add(newAssociate) session.commit()
def update_study_associate(study_id=None, uid=None, role="", send_email=False, access=False): if study_id is None: raise ApiError('study_id not specified', "This function requires the study_id parameter") if uid is None: raise ApiError('uid not specified', "This function requires a uva uid parameter") if not LdapService.user_exists(uid): raise ApiError( 'trying_to_grant_access_to_user_not_found_in_ldap', "You are trying to grant access to " "%s but they were not found in ldap " "- please check to ensure it is a " "valid uva uid" % uid) study = db.session.query(StudyModel).filter( StudyModel.id == study_id).first() if study is None: raise ApiError('study_id not found', "A study with id# %d was not found" % study_id) db.session.query( StudyAssociated).filter((StudyAssociated.study_id == study_id) & (StudyAssociated.uid == uid)).delete() newAssociate = StudyAssociated() newAssociate.study_id = study_id newAssociate.uid = uid newAssociate.role = role newAssociate.send_email = send_email newAssociate.access = access session.add(newAssociate) session.commit() return True