def update_cohort(): params = request.get_json() uid = current_user.get_id() label = params['label'] if not label: raise BadRequestError('Requested cohort label is empty or invalid') cohort = get_cohort_owned_by(params['id'], uid) if not cohort: raise BadRequestError( 'Cohort does not exist or is not owned by {}'.format(uid)) CohortFilter.update(cohort_id=cohort['id'], label=label) return jsonify({'message': 'Cohort updated (label: {})'.format(label)}), 200
def update_cohort(): params = request.get_json() cohort_id = int(params.get('id')) name = params.get('name') filters = params.get('filters') # Validation if not name and not filters: raise BadRequestError('Invalid request') if not CohortFilter.is_cohort_owned_by(cohort_id, current_user.get_id()): raise ForbiddenRequestError(f'Invalid or unauthorized request') filter_keys = list(map(lambda f: f['key'], filters)) if is_unauthorized_search(filter_keys): raise ForbiddenRequestError( 'You are unauthorized to access student data managed by other departments' ) filter_criteria = _translate_filters_to_cohort_criteria(filters) updated = CohortFilter.update( cohort_id=cohort_id, name=name, filter_criteria=filter_criteria, include_students=False, include_alerts_for_user_id=current_user.get_id(), ) _decorate_cohort(updated) return tolerant_jsonify(updated)
def test_cohort_update(self): cohort = CohortFilter.create(label='Football teams', team_codes=['FBM', 'FBW'], uid='2040') foosball_label = 'Foosball teams' cohort = CohortFilter.update(cohort['id'], foosball_label) assert cohort['label'] == foosball_label
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 update_cohort(): params = request.get_json() cohort_id = int(params.get('id')) name = params.get('name') # Filter criteria can be submitted as (1) ready-to-save JSON in 'criteria' param or (2) 'filters' param which # is a serialized version of what user sees in /cohort view. filter_criteria = _filters_to_filter_criteria(params.get( 'filters')) if 'filters' in params else params.get('criteria') if not name and not filter_criteria: raise BadRequestError('Invalid request') if not CohortFilter.is_cohort_owned_by(cohort_id, current_user.get_id()): raise ForbiddenRequestError(f'Invalid or unauthorized request') updated = CohortFilter.update( cohort_id=cohort_id, name=name, filter_criteria=filter_criteria, include_students=False, include_alerts_for_user_id=current_user.get_id(), ) _decorate_cohort(updated) return tolerant_jsonify(updated)