def add_appointment_scheduler_to_dept(dept_code): _verify_membership_and_appointments_enabled(current_user, dept_code) params = request.get_json() or {} scheduler_uid = params.get('uid', None) if not scheduler_uid: raise errors.BadRequestError('Scheduler UID missing') calnet_user = calnet.get_calnet_user_for_uid(app, scheduler_uid, skip_expired_users=True) if not calnet_user or not calnet_user.get('csid'): raise errors.BadRequestError('Invalid scheduler UID') user = AuthorizedUser.create_or_restore( scheduler_uid, created_by=current_user.get_uid(), is_admin=False, is_blocked=False, can_access_canvas_data=False, ) Scheduler.create_or_update_membership( dept_code, user.id, drop_in=True, same_day=True, ) _create_department_memberships(user, [{ 'code': dept_code, 'role': 'scheduler', 'automateMembership': False }]) UserSession.flush_cache_for_id(user.id) return tolerant_jsonify( _get_appointment_scheduler_list(current_user, dept_code))
def _update_or_create_authorized_user(memberships, profile, include_deleted=False): user_id = profile.get('id') automate_degree_progress_permission = profile.get( 'automateDegreeProgressPermission') can_access_canvas_data = to_bool_or_none( profile.get('canAccessCanvasData')) can_access_advising_data = to_bool_or_none( profile.get('canAccessAdvisingData')) degree_progress_permission = profile.get('degreeProgressPermission') if (automate_degree_progress_permission or degree_progress_permission ) and 'COENG' not in dept_codes_where_advising( {'departments': memberships}): raise errors.BadRequestError( 'Degree Progress feature is only available to the College of Engineering.' ) is_admin = to_bool_or_none(profile.get('isAdmin')) is_blocked = to_bool_or_none(profile.get('isBlocked')) if user_id: user = AuthorizedUser.update_user( automate_degree_progress_permission= automate_degree_progress_permission, can_access_advising_data=can_access_advising_data, can_access_canvas_data=can_access_canvas_data, degree_progress_permission=degree_progress_permission, include_deleted=include_deleted, is_admin=is_admin, is_blocked=is_blocked, user_id=user_id, ) UserSession.flush_cache_for_id(user_id=user_id) return user else: uid = profile.get('uid') if AuthorizedUser.get_id_per_uid(uid, include_deleted=True): raise errors.BadRequestError( f'User with UID {uid} is already in the BOA database.') calnet_user = calnet.get_calnet_user_for_uid(app, uid, skip_expired_users=True) if calnet_user and calnet_user.get('csid', None): return AuthorizedUser.create_or_restore( automate_degree_progress_permission= automate_degree_progress_permission, can_access_advising_data=can_access_advising_data, can_access_canvas_data=can_access_canvas_data, created_by=current_user.get_uid(), degree_progress_permission=degree_progress_permission, is_admin=is_admin, is_blocked=is_blocked, uid=uid, ) else: raise errors.BadRequestError('Invalid UID')
def create_or_update_user_profile(): params = request.get_json() profile = params.get('profile', None) memberships = params.get('memberships', None) delete_action = to_bool_or_none(util.get(params, 'deleteAction')) if not profile or not profile.get('uid') or memberships is None: raise errors.BadRequestError('Required parameters are missing') authorized_user = _update_or_create_authorized_user(memberships, profile, include_deleted=True) _delete_existing_memberships(authorized_user) _create_department_memberships(authorized_user, memberships) if delete_action is True and not authorized_user.deleted_at: AuthorizedUser.delete(authorized_user.uid) elif delete_action is False and authorized_user.deleted_at: AuthorizedUser.un_delete(authorized_user.uid) user_id = authorized_user.id UserSession.flush_cache_for_id(user_id) updated_user = AuthorizedUser.find_by_id(user_id, include_deleted=True) users_json = authorized_users_api_feed([updated_user]) return tolerant_jsonify(users_json and users_json[0])
def set_drop_in_advising_status(dept_code): user = AuthorizedUser.find_by_id(current_user.get_id()) drop_in_membership = next((d for d in user.drop_in_departments if d.dept_code == dept_code.upper()), None) if not drop_in_membership: raise errors.ResourceNotFoundError( f'No drop-in advisor membership found: (uid={current_user.get_uid()}, dept_code={dept_code})' ) params = request.get_json() if 'status' not in params: raise errors.BadRequestError('Missing status') if params['status'] and len(params['status']) > 255: raise errors.BadRequestError('Invalid status') drop_in_membership.update_status(params['status']) UserSession.flush_cache_for_id(user.id) return tolerant_jsonify(drop_in_membership.to_api_json())
def set_demo_mode(): if app.config['DEMO_MODE_AVAILABLE']: in_demo_mode = request.get_json().get('demoMode', None) if in_demo_mode is None: raise errors.BadRequestError('Parameter \'demoMode\' not found') user = AuthorizedUser.find_by_id(current_user.get_id()) user.in_demo_mode = bool(in_demo_mode) current_user.flush_cached() app.login_manager.reload_user() return tolerant_jsonify(current_user.to_api_json()) else: raise errors.ResourceNotFoundError('Unknown path')
def remove_appointment_scheduler_from_dept(dept_code): _verify_membership_and_appointments_enabled(current_user, dept_code) params = request.get_json() or {} uid = params.get('uid') user = uid and AuthorizedUser.find_by_uid(uid) if not user: raise errors.BadRequestError(f'UID {uid} missing or invalid') scheduler_membership = next(( d for d in user.department_memberships if d.university_dept.dept_code == dept_code and d.role == 'scheduler'), None) if not scheduler_membership: raise errors.BadRequestError( f'UID {uid} is not a scheduler for department {dept_code}') UniversityDeptMember.delete_membership( university_dept_id=scheduler_membership.university_dept_id, authorized_user_id=user.id, ) Scheduler.delete(authorized_user_id=user.id, dept_code=dept_code) if not len(user.department_memberships): AuthorizedUser.delete(uid) return tolerant_jsonify( _get_appointment_scheduler_list(current_user, dept_code))
def update_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.update_membership( university_dept_id=dept.id, authorized_user_id=user.id, role=params.get('role', None), automate_membership=params.get('automateMembership', None), ) if not membership: raise errors.BadRequestError( f'Failed to update university dept membership: university_dept_id={dept.id} authorized_user_id={user.id}' ) return tolerant_jsonify(membership.to_api_json())