def refresh_department_memberships(): from boac.models.authorized_user import AuthorizedUser from boac.models.university_dept import UniversityDept from boac.models.university_dept_member import UniversityDeptMember depts = UniversityDept.query.all() for dept in depts: dept.delete_automated_members() std_commit(allow_test_environment=True) for dept in depts: for membership in dept.memberships_from_loch(): # A non-numeric "uid" indicates a row from SIS advising tables best ignored. if not re.match(r'^\d+$', membership['uid']): continue user = AuthorizedUser.create_or_restore( uid=membership['uid'], created_by='0', can_access_canvas_data=membership['can_access_canvas_data'], ) if user: UniversityDeptMember.create_or_update_membership( dept, user, is_advisor=True, is_director=False, is_scheduler=False, )
def test_allows_advisor_to_change_departments(self): """Updates membership for a former CoE advisor who switches to L&S.""" user = AuthorizedUser.find_by_uid('242881') dept_coe = UniversityDept.query.filter_by(dept_code='COENG').first() UniversityDeptMember.create_or_update_membership( dept_coe.id, user.id, role='advisor', ) dept_ucls = UniversityDept.query.filter_by( dept_code='QCADVMAJ').first() UniversityDeptMember.delete_membership(dept_ucls.id, user.id) std_commit(allow_test_environment=True) ucls_users = [au.authorized_user for au in dept_ucls.authorized_users] assert len(ucls_users) == 2 from boac.api.cache_utils import refresh_department_memberships refresh_department_memberships() std_commit(allow_test_environment=True) ucls_users = [au.authorized_user for au in dept_ucls.authorized_users] assert len(ucls_users) == 3 assert next(u for u in ucls_users if u.uid == '242881') updated_user = AuthorizedUser.query.filter_by(uid='242881').first() assert updated_user.deleted_at is None assert updated_user.created_by == '0' assert updated_user.department_memberships[ 0].university_dept_id == dept_ucls.id
def test_deletes_drop_in_advisor_orphans(self): """Cleans up drop-in advisor record for a department membership that no longer exists.""" from boac.models.authorized_user_extension import DropInAdvisor dept_ucls = UniversityDept.query.filter_by( dept_code='QCADVMAJ').first() bad_user = AuthorizedUser.create_or_restore(uid='666', created_by='2040') UniversityDeptMember.create_or_update_membership( dept_ucls.id, bad_user.id, role='advisor', ) DropInAdvisor.create_or_update_membership(dept_ucls.dept_code, bad_user.id) std_commit(allow_test_environment=True) ucls_drop_in_advisors = DropInAdvisor.advisors_for_dept_code( dept_ucls.dept_code) assert len(ucls_drop_in_advisors) == 2 assert bad_user.id in [ d.authorized_user_id for d in ucls_drop_in_advisors ] from boac.api.cache_utils import refresh_department_memberships refresh_department_memberships() std_commit(allow_test_environment=True) ucls_drop_in_advisors = DropInAdvisor.advisors_for_dept_code( dept_ucls.dept_code) assert len(ucls_drop_in_advisors) == 1
def test_removes_coe_advisors(self, app): """Removes COE advisors not found in the loch.""" dept_coe = UniversityDept.query.filter_by(dept_code='COENG').first() bad_user = AuthorizedUser.create_or_restore(uid='666', created_by='2040') UniversityDeptMember.create_or_update_membership( dept_coe.id, bad_user.id, role='advisor', ) std_commit(allow_test_environment=True) coe_users = [au.authorized_user for au in dept_coe.authorized_users] coe_user_count = len(coe_users) assert coe_user_count assert next(u for u in coe_users if u.uid == '666') assert AuthorizedUser.query.filter_by( uid='666').first().deleted_at is None from boac.api.cache_utils import refresh_department_memberships refresh_department_memberships() std_commit(allow_test_environment=True) coe_users = [au.authorized_user for au in dept_coe.authorized_users] assert len(coe_users) == coe_user_count - 1 assert next((u for u in coe_users if u.uid == '666'), None) is None assert AuthorizedUser.query.filter_by(uid='666').first().deleted_at
def refresh_department_memberships(): from boac.models.authorized_user import AuthorizedUser from boac.models.authorized_user_extension import DropInAdvisor, SameDayAdvisor, Scheduler from boac.models.university_dept import UniversityDept from boac.models.university_dept_member import UniversityDeptMember depts = UniversityDept.query.all() for dept in depts: dept.delete_automated_members() std_commit(allow_test_environment=True) for dept in depts: for membership in dept.memberships_from_loch(): # A non-numeric "uid" indicates a row from SIS advising tables best ignored. if not re.match(r'^\d+$', membership['uid']): continue user = AuthorizedUser.create_or_restore( uid=membership['uid'], created_by='0', can_access_advising_data=membership['can_access_advising_data'], can_access_canvas_data=membership['can_access_canvas_data'], degree_progress_permission=membership['degree_progress_permission'], ) if user: UniversityDeptMember.create_or_update_membership( university_dept_id=dept.id, authorized_user_id=user.id, role='advisor', ) DropInAdvisor.delete_orphans() SameDayAdvisor.delete_orphans() Scheduler.delete_orphans()
def test_respects_automate_memberships_flag(self, app, db): dept_coe = UniversityDept.query.filter_by(dept_code='COENG').first() manually_added_user = AuthorizedUser.create_or_restore( uid='1024', created_by='2040') manual_membership = UniversityDeptMember.create_or_update_membership( dept_coe, manually_added_user, is_advisor=True, is_director=False, is_scheduler=False, automate_membership=False, ) from boac.api.cache_utils import refresh_department_memberships refresh_department_memberships() std_commit(allow_test_environment=True) coe_users = [au.authorized_user for au in dept_coe.authorized_users] assert len(coe_users) == 6 assert next(u for u in coe_users if u.uid == '1024') assert AuthorizedUser.query.filter_by( uid='1024').first().deleted_at is None manual_membership.automate_membership = True db.session.add(manual_membership) std_commit(allow_test_environment=True) refresh_department_memberships() std_commit(allow_test_environment=True) coe_users = [au.authorized_user for au in dept_coe.authorized_users] assert len(coe_users) == 5 assert next((u for u in coe_users if u.uid == '1024'), None) is None assert AuthorizedUser.query.filter_by(uid='1024').first().deleted_at
def _create_department_memberships(authorized_user, memberships): for membership in [ m for m in memberships if m['role'] in ('advisor', 'director', 'scheduler') ]: university_dept = UniversityDept.find_by_dept_code(membership['code']) role = membership['role'] UniversityDeptMember.create_or_update_membership( university_dept_id=university_dept.id, authorized_user_id=authorized_user.id, role=role, automate_membership=to_bool_or_none( membership['automateMembership']), ) if role == 'scheduler': _delete_drop_in_advisor_status(authorized_user, university_dept.dept_code) UserSession.flush_cache_for_id(authorized_user.id)
def _create_department_memberships(): for dept_code, dept_membership in _university_depts.items(): university_dept = UniversityDept.find_by_dept_code(dept_code) db.session.add(university_dept) for user in dept_membership['users']: authorized_user = AuthorizedUser.find_by_uid(user['uid']) UniversityDeptMember.create_or_update_membership( university_dept_id=university_dept.id, authorized_user_id=authorized_user.id, role=user['role'], automate_membership=user['automate_membership'], ) if user['isDropInAdvisor']: DropInAdvisor.create_or_update_membership( dept_code=dept_code, authorized_user_id=authorized_user.id, is_available=True, )
def add_university_dept_membership(): params = request.get_json() or {} dept = UniversityDept.find_by_dept_code(params.get('deptCode', None)) user = AuthorizedUser.find_by_uid(params.get('uid', None)) membership = UniversityDeptMember.create_or_update_membership( university_dept_id=dept.id, authorized_user_id=user.id, role=params.get('role', None), automate_membership=params.get('automateMembership', True), ) return tolerant_jsonify(membership.to_api_json())
def _create_department_memberships(): for dept_code, dept_membership in _university_depts.items(): university_dept = UniversityDept.find_by_dept_code(dept_code) db.session.add(university_dept) for user in dept_membership['users']: authorized_user = AuthorizedUser.find_by_uid(user['uid']) UniversityDeptMember.create_or_update_membership( university_dept=university_dept, authorized_user=authorized_user, is_advisor=user['isAdvisor'], is_director=user['isDirector'], is_scheduler=user['isScheduler'], automate_membership=user['automate_membership'], ) if user['isDropInAdvisor']: DropInAdvisor.create_or_update_status( university_dept=university_dept, authorized_user=authorized_user, is_available=True, )
def add_university_dept_membership(): params = request.get_json() or {} dept = UniversityDept.find_by_dept_code(params.get('deptCode', None)) user = AuthorizedUser.find_by_uid(params.get('uid', None)) membership = UniversityDeptMember.create_or_update_membership( university_dept=dept, authorized_user=user, is_advisor=params.get('isAdvisor', False), is_director=params.get('isDirector', False), is_scheduler=params.get('isScheduler', False), automate_membership=params.get('automateMembership', True), ) return tolerant_jsonify(membership.to_api_json())
def test_respects_automate_memberships_flag(self, app, db): dept_coe = UniversityDept.query.filter_by(dept_code='COENG').first() manually_added_user = AuthorizedUser.create_or_restore( uid='1024', created_by='2040', degree_progress_permission='read_write', ) manual_membership = UniversityDeptMember.create_or_update_membership( dept_coe.id, manually_added_user.id, role='advisor', automate_membership=False, ) from boac.api.cache_utils import refresh_department_memberships refresh_department_memberships() std_commit(allow_test_environment=True) coe_users = [au.authorized_user for au in dept_coe.authorized_users] coe_user_count = len(coe_users) assert coe_user_count assert next(u for u in coe_users if u.uid == '1024') user = AuthorizedUser.find_by_uid(uid='1024') assert user assert user.degree_progress_permission == 'read_write' manual_membership.automate_membership = True db.session.add(manual_membership) std_commit(allow_test_environment=True) refresh_department_memberships() std_commit(allow_test_environment=True) coe_users = [au.authorized_user for au in dept_coe.authorized_users] assert len(coe_users) == coe_user_count - 1 assert next((u for u in coe_users if u.uid == '1024'), None) is None assert not AuthorizedUser.find_by_uid(uid='1024')