@post('/s/cards/{card_id}/responses')
def respond_to_card_route(request, card_id):

    # TODO-3 simplify this method.
    #      perhaps smaller methods or move to model layer?

    db_conn = request['db_conn']
    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    card = get_card_by_kind(db_conn, card_id)
    if not card:
        return abort(404)

    # Make sure the card is the current one
    context = current_user.get_learning_context()
    if context.get('card', {}).get('entity_id') != card['entity_id']:
        return abort(400)

    r = seq_update(db_conn, current_user, card,
                   request['params'].get('response'))
    errors, response, feedback = (r.get('errors'), r.get('response'),
                                  r.get('feedback'))
    if errors:
        return 400, {
            'errors': errors,
            'ref': 'wtyOJPoy4bh76OIbYp8mS3LP',
        }

    set_ = Set(context.get('set'))
    unit = Unit(context.get('unit'))

    status = judge(db_conn, unit, current_user)

    # If we are done with this current unit...
    if status == "done":
        buckets = traverse(db_conn, current_user, set_)

        # If there are units to be diagnosed...
        if buckets['diagnose']:
            unit = buckets['diagnose'][0]
            next_card = choose_card(db_conn, current_user, unit)
            next_ = {
                'method': 'GET',
                'path': '/s/cards/{card_id}/learn'
                        .format(card_id=next_card['entity_id']),
            }
            current_user.set_learning_context(
                card=next_card.data, unit=unit.data, next=next_)

        # If there are units to be learned or reviewed...
        elif buckets['learn'] or buckets['review']:
            next_ = {
                'method': 'GET',
                'path': '/s/sets/{set_id}/units'
                        .format(set_id=set_['entity_id']),
            }
            current_user.set_learning_context(card=None, unit=None, next=next_)

        # If we are out of units...
        else:
            next_ = {
                'method': 'GET',
                'path': '/s/sets/{set_id}/tree'
                        .format(set_id=set_['entity_id']),
            }
            current_user.set_learning_context(card=None, unit=None, next=next_)

    # If we are still reviewing, learning or diagnosing this unit...
    else:
        next_card = choose_card(db_conn, current_user, unit)
        if next_card:
            next_ = {
                'method': 'GET',
                'path': '/s/cards/{card_id}/learn'
                        .format(card_id=next_card['entity_id']),
            }
            current_user.set_learning_context(card=next_card.data, next=next_)
        else:
            next_ = {}
            current_user.set_learning_context(next=next_)

    return 200, {
        'response': response.deliver(),
        'feedback': feedback,
        'next': next_,
    }
Record and process a learner's response to a card.
dataset/ETHPy150Open heiskr/sagefy/server/routes/card.py/respond_to_card_route