def get_version(kind, id_): if kind == 'card': return Card.get(id=id_) # TODO-1 This needs to also get the right card kind... elif kind == 'unit': return Unit.get(id=id_) elif kind == 'set': return Set.get(id=id_)
def get_version(db_conn, kind, id_): if kind == 'card': card = Card.get(db_conn, id=id_) return flip_card_into_kind(card) elif kind == 'unit': return Unit.get(db_conn, id=id_) elif kind == 'set': return Set.get(db_conn, id=id_)
def test_traverse(db_conn, units_table, users_table, responses_table, sets_table): """ Expect to take a list of units and traverse them correctly. Basic test. """ add_test_set(db_conn, users_table, units_table, responses_table, sets_table) set_ = Set.get(db_conn, entity_id="set") user = get_user({"id": "user"}, db_conn) buckets = traverse(db_conn, user, set_) assert buckets["diagnose"][0]["entity_id"] == "divide" assert buckets["learn"][0]["entity_id"] == "multiply" assert buckets["review"][0]["entity_id"] == "subtract"
def test_traverse(db_conn, units_table, users_table, responses_table, sets_table): """ Expect to take a list of units and traverse them correctly. Basic test. """ add_test_set(db_conn, users_table, units_table, responses_table, sets_table) set_ = Set.get(db_conn, entity_id='set') user = get_user({'id': 'user'}, db_conn) buckets = traverse(db_conn, user, set_) assert buckets['diagnose'][0]['entity_id'] == 'divide' assert buckets['learn'][0]['entity_id'] == 'multiply' assert buckets['review'][0]['entity_id'] == 'subtract'
def test_traverse(db_conn, units_table, users_table, responses_table, sets_table): """ Expect to take a list of units and traverse them correctly. Basic test. """ add_test_set(db_conn, users_table, units_table, responses_table, sets_table) set_ = Set.get(entity_id='set') user = User.get(id='user') buckets = traverse(user, set_) assert buckets['diagnose'][0]['entity_id'] == 'divide' assert buckets['learn'][0]['entity_id'] == 'multiply' assert buckets['review'][0]['entity_id'] == 'subtract'
def add_set_route(request, user_id, set_id): """ Add a set to the learner's list of sets. """ db_conn = request['db_conn'] current_user = get_current_user(request) if not current_user: return abort(401) if user_id != current_user['id']: return abort(403) set_ = Set.get(db_conn, entity_id=set_id) if not set_: return abort(404) uset = UserSets.get(db_conn, user_id=user_id) if uset and set_id in uset['set_ids']: return 400, { 'errors': [{ 'name': 'set_id', 'message': 'Set is already added.', }], 'ref': 'kPZ95zM3oxFDGGl8vBdR3J3o', } if uset: uset['set_ids'].append(set_id) uset, errors = uset.save(db_conn) else: uset, errors = UserSets.insert(db_conn, { 'user_id': user_id, 'set_ids': [set_id], }) if errors: return 400, { 'errors': errors, 'ref': 'zCFUbLBTg9n2DnTkQYbqO4X9' } return 200, {'sets': uset['set_ids']}
def add_set_route(request, user_id, set_id): """ Add a set to the learner's list of sets. """ db_conn = request['db_conn'] current_user = get_current_user(request) if not current_user: return abort(401) if user_id != current_user['id']: return abort(403) set_ = Set.get(db_conn, entity_id=set_id) if not set_: return abort(404) uset = get_user_sets(user_id, db_conn) if uset and set_id in uset['set_ids']: return 400, { 'errors': [{ 'name': 'set_id', 'message': 'Set is already added.', }], 'ref': 'kPZ95zM3oxFDGGl8vBdR3J3o', } # TODO-2 move some of this logic to the database file if uset: uset, errors = append_user_sets(user_id, set_id, db_conn) else: uset, errors = insert_user_sets( { 'user_id': user_id, 'set_ids': [set_id], }, db_conn) if errors: return 400, {'errors': errors, 'ref': 'zCFUbLBTg9n2DnTkQYbqO4X9'} return 200, {'sets': uset['set_ids']}
def add_set_route(request, user_id, set_id): """ Add a set to the learner's list of sets. """ current_user = get_current_user(request) if not current_user: return abort(401) if user_id != current_user['id']: return abort(403) set_ = Set.get(entity_id=set_id) if not set_: return abort(404) uset = UserSets.get(user_id=user_id) if uset and set_id in uset['set_ids']: return 400, { 'errors': [{ 'name': 'set_id', 'message': 'Set is already added.', }], 'ref': 'kPZ95zM3oxFDGGl8vBdR3J3o', } if uset: uset['set_ids'].append(set_id) uset, errors = uset.save() else: uset, errors = UserSets.insert({ 'user_id': user_id, 'set_ids': [set_id], }) if errors: return 400, {'errors': errors, 'ref': 'zCFUbLBTg9n2DnTkQYbqO4X9'} return 200, {'sets': uset['set_ids']}
def get_set_tree_route(request, set_id): """ Render the tree of units that exists within a set. Contexts: - Search set, preview units in set - Pre diagnosis - Learner view progress in set - Set complete NEXT STATE GET View Set Tree -> GET Choose Set ...when set is complete -> GET Choose Unit ...when in learn or review mode -> GET Learn Card ...when in diagnosis (Unit auto chosen) TODO-2 merge with get_set_units_route TODO-2 simplify this method """ db_conn = request['db_conn'] set_ = Set.get(db_conn, entity_id=set_id) if not set_: return abort(404) units = set_.list_units(db_conn) # For the menu, it must return the name and ID of the set output = { 'set': set_.deliver(), 'units': [u.deliver() for u in units], } current_user = get_current_user(request) if not current_user: return 200, output context = get_learning_context(current_user) if current_user else {} buckets = traverse(db_conn, current_user, set_) output['buckets'] = { 'diagnose': [u['entity_id'] for u in buckets['diagnose']], 'review': [u['entity_id'] for u in buckets['review']], 'learn': [u['entity_id'] for u in buckets['learn']], 'done': [u['entity_id'] for u in buckets['done']], } # If we are just previewing, don't update anything if set_id != context.get('set', {}).get('entity_id'): return 200, output # When in diagnosis, choose the unit and card automagically. if buckets['diagnose']: unit = buckets['diagnose'][0] card = choose_card(db_conn, current_user, unit) next_ = { 'method': 'GET', 'path': '/s/cards/{card_id}/learn'.format(card_id=card['entity_id']), } set_learning_context(current_user, next=next_, unit=unit.data, card=card.data) # When in learn or review mode, lead me to choose a unit. elif buckets['review'] or buckets['learn']: next_ = { 'method': 'GET', 'path': '/s/sets/{set_id}/units'.format(set_id=set_id), } set_learning_context(current_user, next=next_) # If the set is complete, lead the learner to choose another set. else: next_ = { 'method': 'GET', 'path': '/s/users/{user_id}/sets'.format(user_id=current_user['id']), } set_learning_context(current_user, next=next_, unit=None, set=None) output['next'] = next_ return 200, output
def test_list_units(db_conn, units_table, sets_table): """ Expect to get a list of units contained within the set. Recursive. """ units_table.insert([{ 'entity_id': 'B', 'name': 'B', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'require_ids': ['A', 'N'] }, { 'entity_id': 'V', 'name': 'V', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'require_ids': ['Q'] }, { 'entity_id': 'Q', 'name': 'Q', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', }, { 'entity_id': 'A', 'name': 'A', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', }, { 'entity_id': 'N', 'name': 'N', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'require_ids': ['Q', 'A'] }]).run(db_conn) sets_table.insert([{ 'entity_id': 'T', 'name': 'TRex', 'body': 'TRex', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'unit', 'id': 'B', }, { 'kind': 'unit', 'id': 'V', }] }, { 'entity_id': 'S', 'name': 'Saurus', 'body': 'Saurus', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'set', 'id': 'T', }, { 'kind': 'unit', 'id': 'Q', }] }]).run(db_conn) set_ = Set.get(entity_id='S') cards = set_.list_units() card_ids = set(card['entity_id'] for card in cards) assert card_ids == {'B', 'V', 'Q', 'N'}
def test_list_units(db_conn, units_table, sets_table): """ Expect to get a list of units contained within the set. Recursive. """ units_table.insert([{ 'entity_id': 'B', 'name': 'B', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'require_ids': ['A', 'N'] }, { 'entity_id': 'V', 'name': 'V', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'require_ids': ['Q'] }, { 'entity_id': 'Q', 'name': 'Q', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', }, { 'entity_id': 'A', 'name': 'A', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', }, { 'entity_id': 'N', 'name': 'N', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'require_ids': ['Q', 'A'] }]).run(db_conn) sets_table.insert([{ 'entity_id': 'T', 'name': 'TRex', 'body': 'TRex', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'unit', 'id': 'B', }, { 'kind': 'unit', 'id': 'V', }] }, { 'entity_id': 'S', 'name': 'Saurus', 'body': 'Saurus', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'members': [{ 'kind': 'set', 'id': 'T', }, { 'kind': 'unit', 'id': 'Q', }] }]).run(db_conn) set_ = Set.get(db_conn, entity_id='S') cards = set_.list_units(db_conn) card_ids = set(card['entity_id'] for card in cards) assert card_ids == {'B', 'V', 'Q', 'N'}
def get_set_tree_route(request, set_id): """ Render the tree of units that exists within a set. Contexts: - Search set, preview units in set - Pre diagnosis - Learner view progress in set - Set complete NEXT STATE GET View Set Tree -> GET Choose Set ...when set is complete -> GET Choose Unit ...when in learn or review mode -> GET Learn Card ...when in diagnosis (Unit auto chosen) TODO-2 merge with get_set_units_route TODO-2 simplify this method """ db_conn = request['db_conn'] set_ = Set.get(db_conn, entity_id=set_id) if not set_: return abort(404) units = set_.list_units(db_conn) # For the menu, it must return the name and ID of the set output = { 'set': set_.deliver(), 'units': [u.deliver() for u in units], } current_user = get_current_user(request) if not current_user: return 200, output context = get_learning_context(current_user) if current_user else {} buckets = traverse(db_conn, current_user, set_) output['buckets'] = { 'diagnose': [u['entity_id'] for u in buckets['diagnose']], 'review': [u['entity_id'] for u in buckets['review']], 'learn': [u['entity_id'] for u in buckets['learn']], 'done': [u['entity_id'] for u in buckets['done']], } # If we are just previewing, don't update anything if set_id != context.get('set', {}).get('entity_id'): return 200, output # When in diagnosis, choose the unit and card automagically. if buckets['diagnose']: unit = buckets['diagnose'][0] card = choose_card(db_conn, current_user, unit) next_ = { 'method': 'GET', 'path': '/s/cards/{card_id}/learn' .format(card_id=card['entity_id']), } set_learning_context( current_user, next=next_, unit=unit.data, card=card.data) # When in learn or review mode, lead me to choose a unit. elif buckets['review'] or buckets['learn']: next_ = { 'method': 'GET', 'path': '/s/sets/{set_id}/units' .format(set_id=set_id), } set_learning_context(current_user, next=next_) # If the set is complete, lead the learner to choose another set. else: next_ = { 'method': 'GET', 'path': '/s/users/{user_id}/sets' .format(user_id=current_user['id']), } set_learning_context(current_user, next=next_, unit=None, set=None) output['next'] = next_ return 200, output
def test_list_units(db_conn, units_table, sets_table): """ Expect to get a list of units contained within the set. Recursive. """ units_table.insert( [ { "entity_id": "B", "name": "B", "created": r.now(), "modified": r.now(), "status": "accepted", "require_ids": ["A", "N"], }, { "entity_id": "V", "name": "V", "created": r.now(), "modified": r.now(), "status": "accepted", "require_ids": ["Q"], }, {"entity_id": "Q", "name": "Q", "created": r.now(), "modified": r.now(), "status": "accepted"}, {"entity_id": "A", "name": "A", "created": r.now(), "modified": r.now(), "status": "accepted"}, { "entity_id": "N", "name": "N", "created": r.now(), "modified": r.now(), "status": "accepted", "require_ids": ["Q", "A"], }, ] ).run(db_conn) sets_table.insert( [ { "entity_id": "T", "name": "TRex", "body": "TRex", "created": r.now(), "modified": r.now(), "status": "accepted", "members": [{"kind": "unit", "id": "B"}, {"kind": "unit", "id": "V"}], }, { "entity_id": "S", "name": "Saurus", "body": "Saurus", "created": r.now(), "modified": r.now(), "status": "accepted", "members": [{"kind": "set", "id": "T"}, {"kind": "unit", "id": "Q"}], }, ] ).run(db_conn) set_ = Set.get(db_conn, entity_id="S") cards = set_.list_units(db_conn) card_ids = set(card["entity_id"] for card in cards) assert card_ids == {"B", "V", "Q", "N"}