def my_profile(): uid = current_user.get_id() profile = calnet.get_calnet_user_for_uid(app, uid) if current_user.is_active: authorized_user_id = current_user.id curated_cohorts = CuratedCohort.get_curated_cohorts_by_owner_id(authorized_user_id) curated_cohorts = [c.to_api_json(sids_only=True) for c in curated_cohorts] departments = {} for m in current_user.department_memberships: departments.update({ m.university_dept.dept_code: { 'isAdvisor': m.is_advisor, 'isDirector': m.is_director, }, }) my_cohorts = CohortFilter.all_owned_by(uid) profile.update({ 'myFilteredCohorts': [c.to_api_json(include_students=False) for c in my_cohorts], 'myCuratedCohorts': curated_cohorts, 'isAdmin': current_user.is_admin, 'departments': departments, }) else: profile.update({ 'myFilteredCohorts': None, 'myCuratedCohorts': None, 'isAdmin': False, 'departments': None, }) return tolerant_jsonify(profile)
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 _get_api_json(cls, user=None): calnet_profile = None departments = [] is_asc = False is_coe = False if user: calnet_profile = calnet.get_calnet_user_for_uid( app, user.uid, force_feed=False, skip_expired_users=True, ) for m in user.department_memberships: dept_code = m.university_dept.dept_code departments.append({ 'code': dept_code, 'name': BERKELEY_DEPT_CODE_TO_NAME[dept_code] or dept_code, 'role': get_dept_role(m), 'isAdvisor': m.is_advisor, 'isDirector': m.is_director, }) dept_codes = get_dept_codes(user) if user else [] is_asc = 'UWASC' in dept_codes is_coe = 'COENG' in dept_codes is_active = False if user: if not calnet_profile: is_active = False elif user.is_admin: is_active = True elif len(user.department_memberships): for m in user.department_memberships: is_active = m.is_advisor or m.is_director if is_active: break is_admin = user and user.is_admin return { **(calnet_profile or {}), **{ 'id': user and user.id, 'canViewAsc': is_asc or is_admin, 'canViewCoe': is_coe or is_admin, 'departments': departments, 'isActive': is_active, 'isAdmin': is_admin, 'isAnonymous': not is_active, 'isAsc': is_asc, 'isAuthenticated': is_active, 'isCoe': is_coe, 'inDemoMode': user and user.in_demo_mode, 'uid': user and user.uid, }, }
def _get_api_json(cls, user=None): calnet_profile = None departments = [] if user: calnet_profile = calnet.get_calnet_user_for_uid( app, user.uid, force_feed=False, skip_expired_users=True, ) for m in user.department_memberships: dept_code = m.university_dept.dept_code departments.append( { 'code': dept_code, 'name': BERKELEY_DEPT_CODE_TO_NAME.get(dept_code, dept_code), 'role': get_dept_role(m), 'isAdvisor': m.is_advisor, 'isDirector': m.is_director, 'isScheduler': m.is_scheduler, }) is_active = False if user: if not calnet_profile: is_active = False elif user.is_admin: is_active = True elif len(user.department_memberships): for m in user.department_memberships: is_active = m.is_advisor or m.is_director or m.is_scheduler if is_active: break is_admin = user and user.is_admin drop_in_advisor_status = [] if user and len(user.drop_in_departments): drop_in_advisor_status = [d.to_api_json() for d in user.drop_in_departments] return { **(calnet_profile or {}), **{ 'id': user and user.id, 'departments': departments, 'isActive': is_active, 'isAdmin': is_admin, 'isAnonymous': not is_active, 'isAuthenticated': is_active, 'inDemoMode': user and user.in_demo_mode, 'canAccessCanvasData': user and user.can_access_canvas_data, 'uid': user and user.uid, 'dropInAdvisorStatus': drop_in_advisor_status, }, }
def _get_author_profile(): author = current_user.to_api_json() role = current_user.departments[0]['role'] if current_user.departments else None calnet_profile = get_calnet_user_for_uid(app, author['uid']) if calnet_profile and calnet_profile.get('departments'): dept_codes = [dept.get('code') for dept in calnet_profile.get('departments')] else: dept_codes = current_user.dept_codes return { 'author_uid': author['uid'], 'author_name': author['name'], 'author_role': role, 'author_dept_codes': dept_codes, }
def authorized_users_api_feed(users, sort_by='lastName'): if not users: return [] profiles = [] for user in users: profile = calnet.get_calnet_user_for_uid(app, user.uid) profile.update({ 'is_admin': user.is_admin, 'departments': {}, }) for m in user.department_memberships: profile['departments'].update({ m.university_dept.dept_code: { 'isAdvisor': m.is_advisor, 'isDirector': m.is_director, }, }) profiles.append(profile) return sorted(profiles, key=lambda p: p.get(sort_by) or '')
def all_cohorts(): cohorts_per_uid = {} for cohort in CohortFilter.all_cohorts(): if can_view_cohort(current_user, cohort): for authorized_user in cohort.owners: uid = authorized_user.uid if uid not in cohorts_per_uid: cohorts_per_uid[uid] = [] cohorts_per_uid[uid].append( decorate_cohort(cohort, include_students=False)) owners = [] for uid in cohorts_per_uid.keys(): owner = calnet.get_calnet_user_for_uid(app, uid) owner.update({ 'cohorts': sorted(cohorts_per_uid[uid], key=lambda c: c['name']), }) owners.append(owner) owners = sorted(owners, key=lambda o: (o['firstName'], o['lastName'])) return tolerant_jsonify(owners)
def _get_author_profile(): author = current_user.to_api_json() calnet_profile = get_calnet_user_for_uid(app, author['uid']) if calnet_profile and calnet_profile.get('departments'): dept_codes = [dept.get('code') for dept in calnet_profile.get('departments')] else: dept_codes = dept_codes_where_advising(current_user) if calnet_profile and calnet_profile.get('title'): role = calnet_profile['title'] elif current_user.departments: role = current_user.departments[0]['role'] else: role = None return { 'author_uid': author['uid'], 'author_name': author['name'], 'author_role': role, 'author_dept_codes': dept_codes, }
def _status_by_user(): uid = AuthorizedUser.get_uid_per_id(event.user_id) return { 'id': event.user_id, **calnet.get_calnet_user_for_uid(app, uid), }
def calnet_profile_by_user_id(user_id): user = AuthorizedUser.find_by_id(user_id) if user: return tolerant_jsonify(calnet.get_calnet_user_for_uid(app, user.uid)) else: raise errors.ResourceNotFoundError('User not found')
def calnet_profile_by_uid(uid): return tolerant_jsonify(calnet.get_calnet_user_for_uid(app, uid))
def user_profile(uid): if not AuthorizedUser.find_by_uid(uid): raise errors.ResourceNotFoundError('Unknown path') return tolerant_jsonify(calnet.get_calnet_user_for_uid(app, uid))
def _get_api_json(cls, user=None): calnet_profile = None departments = [] if user: calnet_profile = calnet.get_calnet_user_for_uid( app, user.uid, force_feed=False, skip_expired_users=True, ) for m in user.department_memberships: dept_code = m.university_dept.dept_code departments.append( { 'code': dept_code, 'isDropInEnabled': dept_code in app.config['DEPARTMENTS_SUPPORTING_DROP_INS'], 'isSameDayEnabled': dept_code in app.config['DEPARTMENTS_SUPPORTING_SAME_DAY_APPTS'], 'name': BERKELEY_DEPT_CODE_TO_NAME.get(dept_code, dept_code), 'role': m.role, }) drop_in_advisor_status = [] same_day_advisor_status = [] is_active = False if user: if not calnet_profile: is_active = False elif user.is_admin: is_active = True elif len(user.department_memberships): for m in user.department_memberships: is_active = True if m.role else False if is_active: break drop_in_advisor_status = [ d.to_api_json() for d in user.drop_in_departments if d.dept_code in app.config['DEPARTMENTS_SUPPORTING_DROP_INS'] ] same_day_advisor_status = [ d.to_api_json() for d in user.same_day_departments if d.dept_code in app.config['DEPARTMENTS_SUPPORTING_SAME_DAY_APPTS'] ] is_admin = user and user.is_admin if app.config['FEATURE_FLAG_DEGREE_CHECK']: degree_progress_permission = 'read_write' if is_admin else (user and user.degree_progress_permission) else: degree_progress_permission = None return { **(calnet_profile or {}), **{ 'id': user and user.id, 'canAccessAdvisingData': user and user.can_access_advising_data, 'canAccessCanvasData': user and user.can_access_canvas_data, 'canEditDegreeProgress': degree_progress_permission == 'read_write', 'canReadDegreeProgress': degree_progress_permission in ['read', 'read_write'], 'degreeProgressPermission': degree_progress_permission, 'departments': departments, 'dropInAdvisorStatus': drop_in_advisor_status, 'inDemoMode': user and user.in_demo_mode, 'isActive': is_active, 'isAdmin': is_admin, 'isAnonymous': not is_active, 'isAuthenticated': is_active, 'sameDayAdvisorStatus': same_day_advisor_status, 'uid': user and user.uid, }, }
def user_profile(uid): match = next((u for u in AuthorizedUser.query.all() if u.uid == uid), None) if not match: raise errors.ResourceNotFoundError('Unknown path') return tolerant_jsonify(calnet.get_calnet_user_for_uid(app, uid))