def get_unit_route(request, unit_id): """ Get a specific unit given an ID. """ db_conn = request['db_conn'] unit = Unit.get_latest_accepted(db_conn, unit_id) if not unit: return abort(404) # TODO-2 SPLITUP create new endpoints for these instead topics = list_topics_by_entity_id(unit_id, {}, db_conn) versions = Unit.get_versions(db_conn, unit_id) requires = Unit.list_requires(db_conn, unit_id) required_by = Unit.list_required_by(db_conn, unit_id) subjects = Subject.list_by_unit_id(db_conn, unit_id) return 200, { 'unit': unit.deliver(), # 'unit_parameters': unit.fetch_parameters(), 'topics': [deliver_topic(topic) for topic in topics], 'versions': [version.deliver() for version in versions], 'requires': [require.deliver() for require in requires], 'required_by': [require.deliver() for require in required_by], 'belongs_to': [subject.deliver() for subject in subjects], }
def choose_unit_route(request, subject_id, unit_id): """ Updates the learner's information based on the unit they have chosen. NEXT STATE POST Chosen Unit -> GET Learn Card """ # TODO-3 simplify this method. should it be broken up or moved to model? db_conn = request['db_conn'] current_user = get_current_user(request) if not current_user: return abort(401) unit = Unit.get_latest_accepted(db_conn, unit_id) if not unit: return abort(404) # If the unit isn't in the subject... context = get_learning_context(current_user) subject_ids = [ subject['entity_id'] for subject in Subject.list_by_unit_id(db_conn, unit_id) ] if context.get('subject', {}).get('entity_id') not in subject_ids: return abort(400) status = judge(db_conn, unit, current_user) # Or, the unit doesn't need to be learned... if status == "done": return abort(400) # Choose a card for the learner to learn card = choose_card(db_conn, current_user, unit) if card: next_ = { 'method': 'GET', 'path': '/s/cards/{card_id}/learn'.format(card_id=card['entity_id']), } else: next_ = {} set_learning_context(current_user, unit=unit.data, card=card.data if card else None, next=next_) return 200, {'next': next_}
def test_list_by_unit_ids(db_conn, units_table, subjects_table): """ Expect to get a list of subjects which contain the given unit ID. Recursive. """ units_table.insert({ 'entity_id': 'Z', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'name': 'Z', }).run(db_conn) subjects_table.insert([{ 'entity_id': 'A', 'name': 'Apple', 'body': 'Apple', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'unit', 'id': 'Z', }] }, { 'entity_id': 'B1', 'name': 'Banana', 'body': 'Banana', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'subject', 'id': 'A', }] }, { 'entity_id': 'B2', 'name': 'Blueberry', 'body': 'Blueberry', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'subject', 'id': 'A', }] }, { 'entity_id': 'B3', 'name': 'Brazil Nut', 'body': 'Brazil Nut', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'subject', 'id': 'N', }] }, { 'entity_id': 'C', 'name': 'Coconut', 'body': 'Coconut', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'subject', 'id': 'B1', }, { 'kind': 'subject', 'id': 'B2', }] }, { 'entity_id': 'D', 'name': 'Date', 'body': 'Date', 'created': r.now(), 'modified': r.now(), 'status': 'accepted' }]).run(db_conn) subjects = Subject.list_by_unit_id(db_conn, 'Z') subject_ids = set(subject['entity_id'] for subject in subjects) assert subject_ids == {'A', 'B1', 'B2', 'C'}