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 delete_cohort(cohort_id): if cohort_id.isdigit(): cohort_id = int(cohort_id) if CohortFilter.is_cohort_owned_by(cohort_id, current_user.get_id()): CohortFilter.delete(cohort_id) return tolerant_jsonify( {'message': f'Cohort deleted (id={cohort_id})'}), 200 else: raise BadRequestError( f'User {current_user.get_uid()} does not own cohort 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.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)