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 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 enable_drop_in_advising(dept_code): drop_in_membership = DropInAdvisor.create_or_update_membership( dept_code, current_user.user_id, is_available=False, ) UserSession.flush_cache_for_id(current_user.user_id) return tolerant_jsonify(drop_in_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_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 drop_in_advisors_for_dept_code(dept_code): dept_code = dept_code.upper() advisor_assignments = DropInAdvisor.advisors_for_dept_code(dept_code) advisors = [] for a in advisor_assignments: advisor = authorized_users_api_feed([a.authorized_user])[0] if advisor['canAccessAdvisingData']: advisor['available'] = a.is_available advisor['status'] = a.status advisors.append(advisor) return sorted(advisors, key=lambda u: ((u.get('firstName') or '').upper(), (u.get('lastName') or '').upper(), u.get('id')))
def _delete_drop_in_advisor_status(authorized_user, dept_code): DropInAdvisor.delete(authorized_user_id=authorized_user.id, dept_code=dept_code) Appointment.unreserve_all_for_advisor(authorized_user.uid, current_user.get_id())