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) sets = Set.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': [set_.deliver() for set_ in sets], }
def get_unit_route(request, unit_id): """ Get a specific unit given an ID. """ unit = Unit.get_latest_accepted(unit_id) if not unit: return abort(404) # TODO-2 SPLITUP create new endpoints for these instead topics = Topic.list_by_entity_id(unit_id) versions = Unit.get_versions(unit_id) requires = Unit.list_requires(unit_id) required_by = Unit.list_required_by(unit_id) sets = Set.list_by_unit_id(unit_id) return 200, { 'unit': unit.deliver(), # 'unit_parameters': unit.fetch_parameters(), 'topics': [topic.deliver() 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': [set_.deliver() for set_ in sets], }
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 = Topic.list_by_entity_id(db_conn, unit_id) 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) sets = Set.list_by_unit_id(db_conn, unit_id) return ( 200, { "unit": unit.deliver(), # 'unit_parameters': unit.fetch_parameters(), "topics": [topic.deliver() 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": [set_.deliver() for set_ in sets], }, )
def choose_unit_route(request, set_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 set... context = get_learning_context(current_user) set_ids = [set_['entity_id'] for set_ in Set.list_by_unit_id(db_conn, unit_id)] if context.get('set', {}).get('entity_id') not in set_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 choose_unit_route(request, set_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 set... context = get_learning_context(current_user) set_ids = [ set_['entity_id'] for set_ in Set.list_by_unit_id(db_conn, unit_id) ] if context.get('set', {}).get('entity_id') not in set_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, sets_table): """ Expect to get a list of sets 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) sets_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': 'set', 'id': 'A', }] }, { 'entity_id': 'B2', 'name': 'Blueberry', 'body': 'Blueberry', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'set', 'id': 'A', }] }, { 'entity_id': 'B3', 'name': 'Brazil Nut', 'body': 'Brazil Nut', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'set', 'id': 'N', }] }, { 'entity_id': 'C', 'name': 'Coconut', 'body': 'Coconut', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'set', 'id': 'B1', }, { 'kind': 'set', 'id': 'B2', }] }, { 'entity_id': 'D', 'name': 'Date', 'body': 'Date', 'created': r.now(), 'modified': r.now(), 'status': 'accepted' }]).run(db_conn) sets = Set.list_by_unit_id('Z') set_ids = set(set_['entity_id'] for set_ in sets) assert set_ids == {'A', 'B1', 'B2', 'C'}
def test_list_by_unit_ids(db_conn, units_table, sets_table): """ Expect to get a list of sets 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) sets_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': 'set', 'id': 'A', }] }, { 'entity_id': 'B2', 'name': 'Blueberry', 'body': 'Blueberry', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'set', 'id': 'A', }] }, { 'entity_id': 'B3', 'name': 'Brazil Nut', 'body': 'Brazil Nut', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'set', 'id': 'N', }] }, { 'entity_id': 'C', 'name': 'Coconut', 'body': 'Coconut', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'set', 'id': 'B1', }, { 'kind': 'set', 'id': 'B2', }] }, { 'entity_id': 'D', 'name': 'Date', 'body': 'Date', 'created': r.now(), 'modified': r.now(), 'status': 'accepted' }]).run(db_conn) sets = Set.list_by_unit_id(db_conn, 'Z') set_ids = set(set_['entity_id'] for set_ in sets) assert set_ids == {'A', 'B1', 'B2', 'C'}
def test_list_by_unit_ids(db_conn, units_table, sets_table): """ Expect to get a list of sets 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) sets_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": "set", "id": "A"}], }, { "entity_id": "B2", "name": "Blueberry", "body": "Blueberry", "created": r.now(), "modified": r.now(), "status": "accepted", "members": [{"kind": "set", "id": "A"}], }, { "entity_id": "B3", "name": "Brazil Nut", "body": "Brazil Nut", "created": r.now(), "modified": r.now(), "status": "accepted", "members": [{"kind": "set", "id": "N"}], }, { "entity_id": "C", "name": "Coconut", "body": "Coconut", "created": r.now(), "modified": r.now(), "status": "accepted", "members": [{"kind": "set", "id": "B1"}, {"kind": "set", "id": "B2"}], }, { "entity_id": "D", "name": "Date", "body": "Date", "created": r.now(), "modified": r.now(), "status": "accepted", }, ] ).run(db_conn) sets = Set.list_by_unit_id(db_conn, "Z") set_ids = set(set_["entity_id"] for set_ in sets) assert set_ids == {"A", "B1", "B2", "C"}