def get_cohort_filter_option_groups(cls, owner_uid, domain, existing_filters=()): # Disable filter options based on existing cohort criteria. option_groups = cls( owner_uid, get_student_query_scope()).get_available_filter_option_groups( domain) cohort_filter_per_key = {} filter_type_per_key = {} for label, option_group in option_groups.items(): for option in option_group: _key = option['key'] cohort_filter_per_key[_key] = option filter_type_per_key[_key] = option['type']['db'] selected_values_per_key = {} for existing_filter in existing_filters: key = existing_filter['key'] if key not in selected_values_per_key: selected_values_per_key[key] = [] value = True if filter_type_per_key[ key] == 'boolean' else existing_filter['value'] selected_values_per_key[key].append(value) cls.populate_cohort_filter_options(cohort_filter_per_key, selected_values_per_key) return option_groups
def translate_to_filter_options(cls, owner_uid, domain, criteria=None): # Transform cohort filter criteria in the database to a UX-compatible data structure. rows = [] if criteria: option_groups = cls(owner_uid, get_student_query_scope()).get_available_filter_option_groups(domain) for label, option_group in option_groups.items(): for option in option_group: selected = criteria.get(option['key']) if selected is not None: def _append_row(value_): clone = deepcopy(option) row = {k: clone.get(k) for k in ['key', 'label', 'options', 'type', 'validation']} row['value'] = value_ rows.append(row) filter_type = option['type']['db'] if filter_type == 'string[]': all_options = option.get('options') for selection in selected: if cls._is_value_in_filter_options(selection, all_options): _append_row(selection) elif filter_type == 'boolean': _append_row(selected) elif filter_type == 'json[]': for obj in selected: _append_row(copy(obj)) else: raise ValueError(f'Unrecognized filter_type "{filter_type}"') return rows
def get_filter_definitions(): categories = get_cohort_filter_definitions(get_student_query_scope()) for category in categories: for definition in category: if definition['type'] == 'array': for index, option in enumerate(definition['options']): option['position'] = index return tolerant_jsonify(categories)
def result_to_dict(result): result_dict = { 'sid': result.get('sid'), 'uid': result.get('uid'), 'firstName': result.get('firstName'), 'lastName': result.get('lastName'), 'alertCount': alert_counts_by_sid.get(result.get('sid')), } scope = get_student_query_scope() if 'UWASC' in scope or 'ADMIN' in scope: result_dict['isActiveAsc'] = result.get( 'athleticsProfile', {}).get('isActiveAsc') return result_dict
def get_cohort_filter_options(owner_uid, existing_filters=()): # Disable filter options based on existing cohort criteria. cohort_filter_options = _get_filter_options(get_student_query_scope(), owner_uid) cohort_filter_per_key = {} filter_type_per_key = {} for category in cohort_filter_options: for _filter in category: _key = _filter['key'] cohort_filter_per_key[_key] = _filter filter_type_per_key[_key] = _filter['type']['db'] selected_values_per_key = {} for existing_filter in existing_filters: key = existing_filter['key'] if key not in selected_values_per_key: selected_values_per_key[key] = [] value = True if filter_type_per_key[ key] == 'boolean' else existing_filter['value'] selected_values_per_key[key].append(value) for key, selected_values in selected_values_per_key.items(): # Disable options that are represented in 'existing_filters' cohort_filter = cohort_filter_per_key[key] if cohort_filter['type']['ux'] == 'boolean': cohort_filter['disabled'] = True if cohort_filter['type']['ux'] == 'dropdown': # Populate dropdown selected_values = selected_values_per_key[key] available_options = cohort_filter['options'] if len(available_options) - len(selected_values) == 0: # This filter has zero available options. cohort_filter['disabled'] = True for option in available_options: option['disabled'] = True else: for option in available_options: if option.get('value') in selected_values: # Disable option option['disabled'] = True return cohort_filter_options
def translate_to_filter_options(owner_uid, criteria=None): # Transform cohort filter criteria in the database to a UX-compatible data structure. rows = [] if criteria: for category in _get_filter_options(get_student_query_scope(), owner_uid): for filter_option in category: selected = criteria.get(filter_option['key']) if selected is not None: def _append_row(value): clone = deepcopy(filter_option) row = { k: clone.get(k) for k in ['key', 'label', 'options', 'type', 'validation'] } row['value'] = value rows.append(row) filter_type = filter_option['type']['db'] if filter_type == 'string[]': all_options = filter_option.get('options') for selection in selected: value = next((o.get('value') for o in all_options if o.get('value') == selection), None) if value: _append_row(value) elif filter_type == 'boolean': _append_row(selected) elif filter_type == 'json[]': for obj in selected: _append_row(copy(obj)) else: raise ValueError( f'Unrecognized filter_type "{filter_type}"') return rows
def _majors(): relevant_majors = [ row['major'] for row in data_loch.get_majors(get_student_query_scope()) ] return [{'name': major, 'value': major} for major in relevant_majors]