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 test_delete_cohort(self, client, coe_advisor_session): """Deletes existing custom cohort while enforcing rules of ownership.""" label = 'Water polo teams' cohort = CohortFilter.create(uid=coe_advisor_uid, label=label, group_codes=['WWP', 'MWP']) # Verify deletion response = client.delete(f'/api/filtered_cohort/delete/{cohort.id}') assert response.status_code == 200 cohorts = CohortFilter.all_owned_by(coe_advisor_uid) assert not next((c for c in cohorts if c.id == cohort.id), None)
def test_undeclared_major(self, asc_advisor_session, client): """Returns a well-formed response with custom cohort.""" name = 'Undeclared students' cohort = next(c for c in CohortFilter.all_owned_by(asc_advisor_uid) if c.label == name) response = client.get(f'/api/filtered_cohort/{cohort.id}') assert response.status_code == 200 cohort = json.loads(response.data) assert cohort['label'] == name students = cohort['students'] assert cohort['totalStudentCount'] == len(students) == 1 # We expect the student with 'Letters & Sci Undeclared UG' major assert students[0]['sid'] == '5678901234'
def test_delete_cohort(self, authenticated_session, client): """deletes existing custom cohort while enforcing rules of ownership""" label = 'Water polo teams' cohort = CohortFilter.create(label=label, team_codes=['WPW', 'WPM'], uid=test_uid) assert cohort and 'id' in cohort id_of_created_cohort = cohort['id'] # Verify deletion response = client.delete('/api/cohort/delete/{}'.format(id_of_created_cohort)) assert response.status_code == 200 cohorts = CohortFilter.all_owned_by(test_uid) assert not next((c for c in cohorts if c['id'] == id_of_created_cohort), None)
def my_cohorts(): uid = current_user.get_id() cohorts = [ decorate_cohort(c, include_alerts_for_uid=uid, include_students=False) for c in CohortFilter.all_owned_by(uid) ] alert_sids = [] for cohort in cohorts: alert_sids += [a['sid'] for a in cohort.get('alerts', [])] alert_profiles = get_summary_student_profiles(alert_sids) alert_profiles_by_sid = {p['sid']: p for p in alert_profiles} for cohort in cohorts: for alert in cohort.get('alerts', []): alert.update(alert_profiles_by_sid[alert['sid']]) strip_analytics(alert) return tolerant_jsonify(cohorts)
def delete_cohort(cohort_id): if cohort_id.isdigit(): cohort_id = int(cohort_id) uid = current_user.get_id() cohort = next( (c for c in CohortFilter.all_owned_by(uid) if c.id == cohort_id), None) if cohort: CohortFilter.delete(cohort_id) return tolerant_jsonify( {'message': f'Cohort deleted (id={cohort_id})'}), 200 else: raise BadRequestError( f'User {uid} does not own cohort_filter with id={cohort_id}') else: raise ForbiddenRequestError( f'Programmatic deletion of canned cohorts is not allowed (id={cohort_id})' )
def update_cohort(): params = request.get_json() cohort_id = int(params['id']) uid = current_user.get_id() label = params.get('label') filter_criteria = params.get('filterCriteria') student_count = params.get('studentCount') if not label and not filter_criteria and not student_count: raise BadRequestError('Invalid request') cohort = next( (c for c in CohortFilter.all_owned_by(uid) if c.id == cohort_id), None) if not cohort: raise ForbiddenRequestError(f'Invalid or unauthorized request') label = label or cohort.label filter_criteria = filter_criteria or cohort.filter_criteria updated = CohortFilter.update(cohort_id=cohort.id, label=label, filter_criteria=filter_criteria, student_count=student_count) return tolerant_jsonify(decorate_cohort(updated, include_students=False))
def test_no_cohort(self): assert not CohortFilter.find_by_id(99999999) assert not CohortFilter.all_owned_by('88888888')
def cohort_count(user_uid): return len(CohortFilter.all_owned_by(user_uid))
def my_cohorts(): return jsonify(CohortFilter.all_owned_by(current_user.get_id()))
def get_cohort_owned_by(cohort_filter_id, uid): return next((c for c in CohortFilter.all_owned_by(uid) if c['id'] == cohort_filter_id), None)
def coe_owned_cohort(): cohorts = CohortFilter.all_owned_by(coe_advisor_uid) return next(c for c in cohorts if c.label == 'Radioactive Women and Men') if len(cohorts) else None
def asc_owned_cohort(): cohorts = CohortFilter.all_owned_by(asc_advisor_uid) return next(c for c in cohorts if c.label == 'All sports') if len(cohorts) else None
def test_can_view_cohort(self, admin_user, asc_advisor, coe_advisor): coe_cohorts = CohortFilter.all_owned_by('1133399') assert len(coe_cohorts) assert berkeley.can_view_cohort(admin_user, coe_cohorts[0]) assert not berkeley.can_view_cohort(asc_advisor, coe_cohorts[0]) assert berkeley.can_view_cohort(coe_advisor, coe_cohorts[0])