def get_card_route(request, card_id): """ Get a specific card given an ID. Show all relevant data, but not used for the learning interface. """ db_conn = request['db_conn'] card = get_latest_accepted_card(db_conn, card_id) if not card: return abort(404, 'fPebfwqfRNmOiSWqWISeaA') unit = get_latest_accepted_unit(db_conn, card['unit_id']) if not unit: return abort(404, 'IKSqfvHvRK6hbSAIOkQuLg') # TODO-2 SPLITUP create new endpoints for these instead requires = list_required_cards(db_conn, card_id) required_by = list_required_by_cards(db_conn, card_id) params = get_card_parameters(db_conn, {'entity_id': card_id}) return 200, { 'card': deliver_card(card, access='view'), 'card_parameters': (get_card_parameters_values(params) if params else None), 'unit': deliver_unit(unit), 'requires': [deliver_card(require) for require in requires], 'required_by': [deliver_card(require) for require in required_by], }
def get_unit_versions_route(request, unit_id): """ Get unit versions given an ID. Paginates. """ db_conn = request['db_conn'] versions = list_one_unit_versions(db_conn, unit_id) return 200, { 'versions': [deliver_unit(version, access='view') for version in versions] }
def get_unit_route(request, unit_id): """ Get a specific unit given an ID. """ db_conn = request['db_conn'] unit = get_latest_accepted_unit(db_conn, unit_id) if not unit: return abort(404, '3UyybCLYQwW_kHO1AumIuA') # TODO-2 SPLITUP create new endpoints for these instead requires = list_required_units(db_conn, unit_id) required_by = list_required_by_units(db_conn, unit_id) subjects = list_subjects_by_unit_flat(db_conn, unit_id) return 200, { 'unit': deliver_unit(unit), # TODO-3 unit parameters 'requires': [deliver_unit(require) for require in requires], 'required_by': [deliver_unit(require) for require in required_by], 'belongs_to': [deliver_subject(subject) for subject in subjects], }
def get_my_recently_created_units_route(request): """ Get the units the user most recently created. """ current_user = get_current_user(request) if not current_user: return abort(401, '2fReKMNNQg6BhnmfBI3UiQ') db_conn = request['db_conn'] units = list_my_recently_created_units(db_conn, current_user['id']) return 200, { 'units': [deliver_unit(unit) for unit in units], }
def list_units_route(request): """ Return a collection of units by `entity_id`. """ db_conn = request['db_conn'] entity_ids = request['params'].get('entity_ids') if not entity_ids: return abort(404, 'Jxs1KZP-TpilXZ-7f-AYvw') entity_ids = entity_ids.split(',') units = list_latest_accepted_units(db_conn, entity_ids) if not units: return abort(404, 'qR6uk5uZQimx-5mutK5t2g') return 200, {'units': [deliver_unit(unit, 'view') for unit in units]}
def get_subject_route(request, subject_id): """ Get a specific subject given an ID. """ db_conn = request['db_conn'] subject = get_latest_accepted_subject(db_conn, subject_id) if not subject: return abort(404, 'pY3NrIXFTuaxy2F0dnf7Cg') # TODO-2 SPLITUP create new endpoints for these instead units = list_units_in_subject_recursive(db_conn, subject) return 200, { 'subject': deliver_subject(subject), # TODO-3 subject parameters 'units': [deliver_unit(unit) for unit in units], }
def create_new_unit_version_route(request): """ Create a new unit version for a brand new unit. """ current_user = get_current_user(request) if not current_user: return abort(401, 'BVR9m3BrSnC5uvFqvoJ7og') db_conn = request['db_conn'] data = deepcopy(request['params']) if 'entity_id' in data: return abort(403, '2LEzNrxlRL6YyAarsC7MAg') data['user_id'] = current_user['id'] unit, errors = insert_unit(db_conn, data) if errors: return 400, { 'errors': errors, 'ref': 'bNAxJ9RBS_SDJcLpe7K0Gg', } return 200, {'version': deliver_unit(unit, 'view')}
def create_existing_unit_version_route(request, unit_id): """ Create a new unit version for an existing unit. """ current_user = get_current_user(request) if not current_user: return abort(401, 'Xfy4fdwwSnuRCiTci-leYQ') db_conn = request['db_conn'] next_data = deepcopy(request['params']) next_data['entity_id'] = unit_id next_data['user_id'] = current_user['id'] current_data = get_latest_accepted_unit(db_conn, unit_id) if not current_data: return abort(404, 'SDJ6WP_gRe28hmsTEKejsg') unit, errors = insert_unit_version(db_conn, current_data, next_data) if errors: return 400, { 'errors': errors, 'ref': 'Oy9dmO9BRiSciigcbeiqrQ', } return 200, {'version': deliver_unit(unit, 'view')}
def get_subject_units_route(request, subject_id): """ Present a small number of units the learner can choose from. NEXT STATE GET Choose Unit -> POST Choose Unit """ db_conn = request['db_conn'] # TODO-3 simplify this method. should it be part of the models? current_user = get_current_user(request) if not current_user: return abort(401, 'p_wleq0FRQ2HEuHKOJIb7Q') context = get_learning_context(current_user) next_ = { 'method': 'POST', 'path': '/s/subjects/{subject_id}/units/{unit_id}'.format( subject_id=context.get('subject', {}).get('entity_id'), unit_id='{unit_id}'), } set_learning_context(current_user, next=next_) subject = get_latest_accepted_subject(db_conn, subject_id) if not subject: return abort(404, '68VOXmd6Shq2cwAAktkFtw') # Pull a list of up to 5 units to choose from based on priority. buckets = traverse(db_conn, current_user, subject) units = buckets['learn'][:5] # TODO-3 Time estimates per unit for mastery. return 200, { 'next': next_, 'units': [deliver_unit(unit) for unit in units], # For the menu, it must return the name and ID of the subject 'subject': deliver_subject(subject), 'current_unit_id': context.get('unit', {}).get('entity_id'), }
def get_subject_tree_route(request, subject_id): """ Render the tree of units that exists within a subject. Contexts: - Search subject, preview units in subject - Pre diagnosis - Learner view progress in subject - Subject complete TODO-2 merge with get_subject_units_route TODO-2 simplify this method """ db_conn = request['db_conn'] subject = get_latest_accepted_subject(db_conn, subject_id) if not subject: return abort(404, '66ejBlFdQ4aYy5MBuP501Q') units = list_units_in_subject_recursive(db_conn, subject) # For the menu, it must return the name and ID of the subject output = { 'subjects': deliver_subject(subject), 'units': [deliver_unit(unit) for unit in units], } current_user = get_current_user(request) if not current_user: return 200, output buckets = traverse(db_conn, current_user, subject) output['buckets'] = { # 'diagnose': [u['entity_id'] for u in buckets['diagnose']], # 'review': [u['entity_id'] for u in buckets['review']], 'blocked': [u['entity_id'] for u in buckets['blocked']], 'learn': [u['entity_id'] for u in buckets['learn']], 'done': [u['entity_id'] for u in buckets['done']], } return 200, output
def test_deliver_unit(db_conn): create_unit_test_data(db_conn) data = get_unit_version(db_conn, unit_version_a_uuid) assert deliver_unit(data, access=None)