def flush_entities(db_conn, descs): """ Given a list of kinds and entity_ids, return a list filled out with entities. """ output = [] for desc in descs: if desc['kind'] == 'card': card = Card.get_latest_accepted(db_conn, entity_id=desc['id']) card = flip_card_into_kind(card) if card: output.append(card) elif desc['kind'] == 'unit': output.append(Unit.get_latest_accepted( db_conn, entity_id=desc['id'] )) elif desc['kind'] == 'subject': output.append(Subject.get_latest_accepted( db_conn, entity_id=desc['id'] )) else: output.append(None) return output
def get_latest_accepted(db_conn, kind, entity_id): """ Given a kind and an entity_id, pull the latest accepted version out of the database. """ if kind == 'card': card = Card.get_latest_accepted(db_conn, entity_id) return flip_card_into_kind(card) elif kind == 'unit': return Unit.get_latest_accepted(db_conn, entity_id) elif kind == 'subject': return Subject.get_latest_accepted(db_conn, entity_id)
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) 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 = Subject.get_latest_accepted(db_conn, subject_id) # 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': [unit.deliver() for unit in units], # For the menu, it must return the name and ID of the subject 'subject': subject.deliver(), 'current_unit_id': context.get('unit', {}).get('entity_id'), }
def get_subject_route(request, subject_id): """ Get a specific subject given an ID. """ db_conn = request['db_conn'] subject = Subject.get_latest_accepted(db_conn, subject_id) if not subject: return abort(404) # TODO-2 SPLITUP create new endpoints for these instead topics = list_topics_by_entity_id(subject_id, {}, db_conn) versions = Subject.get_versions(db_conn, entity_id=subject_id) units = subject.list_units(db_conn) return 200, { 'subject': subject.deliver(), # 'subject_parameters': subject.fetch_parameters(), 'topics': [deliver_topic(topic) for topic in topics], 'versions': [version.deliver() for version in versions], 'units': [unit.deliver() for unit in units], }
def select_subject_route(request, user_id, subject_id): """ Select the subject to work on. NEXT STATE POST Choose Subject (Update Learner Context) -> GET View Subject Tree """ db_conn = request['db_conn'] current_user = get_current_user(request) if not current_user: return abort(401) subject = Subject.get_latest_accepted(db_conn, subject_id) next_ = { 'method': 'GET', 'path': '/s/subjects/{subject_id}/tree'.format(subject_id=subject_id), } set_learning_context(current_user, subject=subject.data, next=next_) return 200, {'next': next_}
def test_latest_accepted_subject(db_conn, subjects_table): """ Expect to get the latest accepted subject version. """ subjects_table.insert([{ 'id': 'A1', 'entity_id': 'A', 'created': r.time(2004, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'B2', 'entity_id': 'A', 'created': r.time(2005, 11, 3, 'Z'), 'status': 'accepted', }, { 'id': 'C3', 'entity_id': 'B', 'created': r.time(2006, 11, 3, 'Z'), 'status': 'accepted', }]).run(db_conn) subject = Subject.get_latest_accepted(db_conn, 'A') assert subject['id'] == 'B2'