def response_with_students_csv_download(sids, benchmark): rows = [] for student in get_student_profiles(sids=sids): profile = student.get('profile') profile = profile and json.loads(profile) rows.append({ 'first_name': profile.get('firstName'), 'last_name': profile.get('lastName'), 'sid': profile.get('sid'), 'email': profile.get('sisProfile', {}).get('emailAddress'), 'phone': profile.get('sisProfile', {}).get('phoneNumber'), }) benchmark('end') def _norm(row, key): value = row.get(key) return value and value.upper() return response_with_csv_download( rows=sorted( rows, key=lambda r: (_norm(r, 'last_name'), _norm(r, 'first_name'), _norm(r, 'sid'))), filename_prefix='cohort', fieldnames=['first_name', 'last_name', 'sid', 'email', 'phone'], )
def download_boa_users_csv(): users = _get_boa_users() fieldnames = users[-1].keys() return response_with_csv_download( rows=sorted(users, key=lambda row: row['last_name'].upper()), filename_prefix='boa_users', fieldnames=fieldnames, )
def response_with_admits_csv_download(sids, fieldnames, benchmark): key_aliases = { 'cs_empl_id': 'sid', } def _row_for_csv(result): return {f: result.get(key_aliases.get(f, f)) for f in fieldnames} rows = [_row_for_csv(student) for student in get_admitted_students_by_sids(sids=sids)] benchmark('end') return response_with_csv_download( rows=sorted(rows, key=lambda r: (_norm(r, 'last_name'), _norm(r, 'first_name'), _norm(r, 'cs_empl_id'))), filename_prefix='cohort', fieldnames=fieldnames, )
def get_boa_notes_metadata(): return response_with_csv_download( rows=get_summary_of_boa_notes(), filename_prefix='boa_advising_notes_metadata', fieldnames=[ 'author_uid', 'author_name', 'author_role', 'author_dept_codes', 'sid', 'subject', 'topics', 'created_at', 'updated_at', ], )
def alerts_log_export(): def _param_to_utc_date(key): value = (params.get(key) or '').strip() return localized_timestamp_to_utc( value, date_format='%m/%d/%Y') if value else None params = request.get_json() from_date_utc = _param_to_utc_date('fromDate') to_date_utc = _param_to_utc_date('toDate') + timedelta(days=1) if from_date_utc and to_date_utc: def _to_api_json(alert): term_id_match = re.search(r'^2[012]\d[0258]', alert.key[0:4]) active_until = alert.deleted_at or utc_now() return { 'sid': alert.sid, 'term': term_name_for_sis_id(term_id_match.string) if term_id_match else None, 'key': alert.key, 'type': alert.alert_type, 'is_active': not alert.deleted_at, 'active_duration_hours': round( (active_until - alert.created_at).total_seconds() / 3600), 'created_at': alert.created_at, 'deleted_at': alert.deleted_at, } alerts = Alert.get_alerts_per_date_range(from_date_utc, to_date_utc) return response_with_csv_download( rows=[_to_api_json(a) for a in alerts], filename_prefix='alerts_log', fieldnames=[ 'sid', 'term', 'key', 'type', 'is_active', 'active_duration_hours', 'created_at', 'deleted_at' ], ) else: raise BadRequestError('Invalid arguments')
def download_boa_users_csv(): rows = [] for dept in _get_boa_user_groups(): for user in dept['users']: rows.append( { 'last_name': user.get('lastName') or '', 'first_name': user.get('firstName') or '', 'uid': user.get('uid'), 'email': user.get('campusEmail') or user.get('email'), 'dept_code': dept.get('code'), 'dept_name': dept.get('name'), }, ) return response_with_csv_download( rows=sorted(rows, key=lambda row: row['last_name'].upper()), filename_prefix='boa_users', fieldnames=[ 'last_name', 'first_name', 'uid', 'email', 'dept_code', 'dept_name' ], )
def response_with_students_csv_download(sids, fieldnames, benchmark): rows = [] getters = { 'first_name': lambda profile: profile.get('firstName'), 'last_name': lambda profile: profile.get('lastName'), 'sid': lambda profile: profile.get('sid'), 'email': lambda profile: profile.get('sisProfile', {}).get('emailAddress'), 'phone': lambda profile: profile.get('sisProfile', {}).get('phoneNumber'), 'majors': lambda profile: ';'.join([ plan.get('description') for plan in profile.get('sisProfile', {}).get('plans', []) if plan.get('status') == 'Active' ], ), 'level': lambda profile: profile.get('sisProfile', {}).get('level', {}).get( 'description'), 'terms_in_attendance': lambda profile: profile.get('sisProfile', {}).get('termsInAttendance'), 'expected_graduation_date': lambda profile: profile.get('sisProfile', {}).get( 'expectedGraduationTerm', {}).get('name'), 'units_completed': lambda profile: profile.get('sisProfile', {}).get('cumulativeUnits'), 'term_gpa': lambda profile: profile.get('termGpa'), 'cumulative_gpa': lambda profile: profile.get('sisProfile', {}).get('cumulativeGPA'), 'program_status': lambda profile: profile.get('sisProfile', {}).get( 'academicCareerStatus'), } term_gpas = get_term_gpas_by_sid(sids, as_dicts=True) for student in get_student_profiles(sids=sids): profile = student.get('profile') profile = profile and json.loads(profile) student_term_gpas = term_gpas.get(profile['sid']) profile['termGpa'] = student_term_gpas[sorted( student_term_gpas)[-1]] if student_term_gpas else None row = {} for fieldname in fieldnames: row[fieldname] = getters[fieldname](profile) rows.append(row) benchmark('end') def _norm(row, key): value = row.get(key) return value and value.upper() return response_with_csv_download( rows=sorted( rows, key=lambda r: (_norm(r, 'last_name'), _norm(r, 'first_name'), _norm(r, 'sid'))), filename_prefix='cohort', fieldnames=fieldnames, )
def response_with_students_csv_download(sids, fieldnames, benchmark): term_id_last = previous_term_id(current_term_id()) term_id_previous = previous_term_id(term_id_last) rows = [] getters = { 'first_name': lambda profile: profile.get('firstName'), 'last_name': lambda profile: profile.get('lastName'), 'sid': lambda profile: profile.get('sid'), 'email': lambda profile: profile.get('sisProfile', {}).get('emailAddress'), 'phone': lambda profile: profile.get('sisProfile', {}).get('phoneNumber'), 'majors': lambda profile: ';'.join([ plan.get('description') for plan in profile.get('sisProfile', {}).get('plans', []) if plan.get('status') == 'Active' ], ), 'intended_majors': lambda profile: ';'.join([ major.get('description') for major in profile.get('sisProfile', {}).get('intendedMajors') ], ), 'level_by_units': lambda profile: profile.get('sisProfile', {}).get('level', {}).get( 'description'), 'minors': lambda profile: ';'.join([ plan.get('description') for plan in profile.get('sisProfile', {}).get('plansMinor', []) if plan.get('status') == 'Active' ], ), 'subplans': lambda profile: ';'.join([ subplan for subplan in profile.get('sisProfile', {}).get('subplans', []) ]), 'terms_in_attendance': lambda profile: profile.get('sisProfile', {}).get('termsInAttendance'), 'expected_graduation_term': lambda profile: profile.get('sisProfile', {}).get( 'expectedGraduationTerm', {}).get('name'), 'units_completed': lambda profile: profile.get('sisProfile', {}).get('cumulativeUnits'), f'term_gpa_{term_id_previous}': lambda profile: profile.get('termGpa', {}).get(term_id_previous), f'term_gpa_{term_id_last}': lambda profile: profile.get('termGpa', {}).get(term_id_last), 'cumulative_gpa': lambda profile: profile.get('sisProfile', {}).get('cumulativeGPA'), 'program_status': lambda profile: ';'.join( list( set([ plan.get('status') for plan in profile.get('sisProfile', {}).get('plans', []) ], ), ), ), 'academic_standing': lambda profile: profile.get('academicStanding'), 'transfer': lambda profile: 'Yes' if profile.get('sisProfile', {}).get('transfer') else '', 'intended_major': lambda profile: ', '.join([ major.get('description') for major in (profile.get('sisProfile', {}).get('intendedMajors') or []) ]), 'units_in_progress': lambda profile: profile.get('enrolledUnits', {}), } academic_standing = get_academic_standing_by_sid(sids, as_dicts=True) term_gpas = get_term_gpas_by_sid(sids, as_dicts=True) term_units = get_term_units_by_sid(current_term_id(), sids) def _get_last_element(results): return results[sorted(results)[-1]] if results else None def _add_row(student_profile): student_profile['academicStanding'] = _get_last_element( academic_standing.get(student_profile['sid'])) student_profile['termGpa'] = term_gpas.get(student_profile['sid'], {}) student_profile['enrolledUnits'] = term_units[student_profile['sid']] row = {} for fieldname in fieldnames: row[fieldname] = getters[fieldname](student_profile) rows.append(row) students = get_student_profiles(sids=sids) for student in students: profile = student.get('profile') if profile: _add_row(json.loads(profile)) remaining_sids = list(set(sids) - set([s.get('sid') for s in students])) if remaining_sids: for profile in get_historical_student_profiles(remaining_sids): _add_row(profile) benchmark('end') return response_with_csv_download( rows=sorted( rows, key=lambda r: (_norm(r, 'last_name'), _norm(r, 'first_name'), _norm(r, 'sid'))), filename_prefix='cohort', fieldnames=fieldnames, )