def signup():
    error = None
    req = request.json
    request_email = req['email'].lower()
    password = req['password']
    entry = user.findSingleUser({'email': request_email})

    if entry is not None:
        error = 'Email is already in use'
        return jsonify(LoggedIn=False, error=error), HTTP_400_BAD_REQUEST
    try:
        invite_code = req['invite']
        if not invite.is_valid(invite_code):
            raise Exception("Invalid invite code")
        new_user = user.create_user(req)
        if (new_user is None):
            raise Exception()

        database_wrapper.save_entity(new_user)
        invite.consume(invite_code, new_user['_id'])

        # We need to log in the just-registered user.
        status = Auth.login(new_user, password)
        return jsonify(user.get_basic_info_from_users([new_user])[0])
    except Exception as e:
        return jsonify(error=str(e)), HTTP_400_BAD_REQUEST
def signup():
    error = None
    req = request.json
    request_email = req['email'].lower()
    password = req['password']
    entry = user.findSingleUser({'email': request_email})

    if entry is not None:
        error = 'Email is already in use'
        return jsonify(LoggedIn=False, error=error), HTTP_400_BAD_REQUEST
    try:
        invite_code = req['invite']
        if not invite.is_valid(invite_code):
            raise Exception("Invalid invite code")
        new_user = user.create_user(req)
        if (new_user is None):
            raise Exception()

        database_wrapper.save_entity(new_user)
        invite.consume(invite_code, new_user['_id'])

        # We need to log in the just-registered user.
        status = Auth.login(new_user, password)
        return jsonify(user.get_basic_info_from_users([new_user])[0])
    except Exception as e:
        return jsonify(error=str(e)), HTTP_400_BAD_REQUEST
def userEdges(user_id):
    entry = user.findUserByID(user_id)
    if entry is None:
        return '', HTTP_404_NOT_FOUND

    suggested_connections = []
    pending_connections = []
    pending_connections_messages = {}

    if user_id == 'me':
        suggested_connection_users = user.get_suggested_connections(entry)
        suggested_connections = user.get_basic_info_from_users(suggested_connection_users)

        pending_connection_ids = map(lambda connection : ObjectId(connection['user']),
                                     user.get_pending_connections(entry))

        pending_connections = user.get_basic_info_from_ids(pending_connection_ids)

        map(lambda connection: pending_connections_messages.update({connection['user']:connection['message']}),
            user.get_pending_connections(entry))

    connection_ids = map(ObjectId, user.get_connections(entry))
    connections = user.get_basic_info_from_ids(connection_ids)

    annotated = {'connections': connections,
                 'suggestedConnections': suggested_connections,
                 'pendingConnections': pending_connections,
                 'pendingConnectionsMessages': pending_connections_messages,
                 'associations': []}

    return jsonify(**annotated)
def userEdges(user_id):
    entry = user.findUserByID(user_id)
    if entry is None:
        return '', HTTP_404_NOT_FOUND

    suggested_connections = []
    pending_connections = []
    pending_connections_messages = {}

    if user_id == 'me':
        suggested_connection_users = user.get_suggested_connections(entry)
        suggested_connections = user.get_basic_info_from_users(
            suggested_connection_users)

        pending_connection_ids = map(
            lambda connection: ObjectId(connection['user']),
            user.get_pending_connections(entry))

        pending_connections = user.get_basic_info_from_ids(
            pending_connection_ids)

        map(
            lambda connection: pending_connections_messages.update(
                {connection['user']: connection['message']}),
            user.get_pending_connections(entry))

    connection_ids = map(ObjectId, user.get_connections(entry))
    connections = user.get_basic_info_from_ids(connection_ids)

    annotated = {
        'connections': connections,
        'suggestedConnections': suggested_connections,
        'pendingConnections': pending_connections,
        'pendingConnectionsMessages': pending_connections_messages,
        'associations': []
    }

    return jsonify(**annotated)