예제 #1
0
async def remove_user_connections(
    username: str,
    conn_ids: List[int],
    current_user: User = Depends(get_current_user),
    session=Depends(get_db)):
    user = get_user(session,
                    username,
                    HTTP_404_NOT_FOUND,
                    err_msg="User '" + username + "' not found")
    to_delete = []
    for conn_id in conn_ids:
        connection = get_connection(session,
                                    conn_id,
                                    HTTP_404_NOT_FOUND,
                                    err_msg="Connection with id='" +
                                    str(conn_id) + "' not found")
        conn_exists = [
            connection for connection in user.connections
            if connection.id == conn_id
        ]
        if len(conn_exists) > 0:
            to_delete.append(conn_exists[0])
            user.connections.remove(conn_exists[0])
    session.commit()
    return to_delete
예제 #2
0
async def add_user_connections(conn_ids: List[int],
                               username: str,
                               current_user: User = Depends(get_current_user),
                               session=Depends(get_db)):
    user = get_user(session,
                    username,
                    HTTP_404_NOT_FOUND,
                    err_msg="User '" + username + "' not found")
    for conn_id in conn_ids:
        connection = get_connection(session,
                                    conn_id,
                                    HTTP_404_NOT_FOUND,
                                    err_msg="Connection with id='" +
                                    str(conn_id) + "' not found")
        conn_exists = [
            connection for connection in user.connections
            if connection.id == conn_id
        ]
        if len(conn_exists) < 1:  # Should I instead raise an exception here?
            user.connections.append(connection)
        #raise HTTPException(
        #	status_code=HTTP_409_CONFLICT,
        #	detail="Connection with id='" + str(conn_id) + "' already exists in user '" + username + "'"
        #)
    session.commit()
    return [connection for connection in user.connections]
예제 #3
0
async def get_connection_users(conn_id: int, session=Depends(get_db)):
    connection = get_connection(session,
                                conn_id,
                                HTTP_404_NOT_FOUND,
                                err_msg="Connection with id='" + str(conn_id) +
                                "' not found")
    return connection.campaigns
예제 #4
0
async def remove_campaign_from_connection(conn_id: int,
                                          campaign: str,
                                          session=Depends(get_db)):
    connection = get_connection(session,
                                conn_id,
                                HTTP_404_NOT_FOUND,
                                err_msg="Connection with id='" + str(conn_id) +
                                "' not found")
    return remove_campaign(connection, campaign)
예제 #5
0
async def remove_connection(conn_id: int, session=Depends(get_db)):
    connection = get_connection(session,
                                conn_id,
                                HTTP_404_NOT_FOUND,
                                err_msg="Connection with id='" + str(conn_id) +
                                "' not found")
    return delete_local_resource(db=session,
                                 Resource=Connection,
                                 instance=connection)
예제 #6
0
async def unassign_users_from_connection(conn_id: int,
                                         usernames: List[str],
                                         session=Depends(get_db)):
    connection = get_connection(session,
                                conn_id,
                                HTTP_404_NOT_FOUND,
                                err_msg="Connection with id='" + str(conn_id) +
                                "' not found")
    to_delete = []
    for username in usernames:
        user = get_user(session,
                        username,
                        HTTP_404_NOT_FOUND,
                        err_msg="User '" + username + "' not found")
        user_exists = [
            user for user in connection.users if user.username == username
        ]
        # Should consider raising an exception here if the user is not found
        if len(user_exists) > 0:
            connection.users.remove(user)
            to_delete.append(user)
    session.commit()
    return to_delete
예제 #7
0
async def assign_users_to_connection(conn_id: int,
                                     usernames: List[str],
                                     session=Depends(get_db)):
    connection = get_connection(session,
                                conn_id,
                                HTTP_404_NOT_FOUND,
                                err_msg="Connection with id='" + str(conn_id) +
                                "' not found")
    for username in usernames:
        user = get_user(session,
                        username,
                        HTTP_404_NOT_FOUND,
                        err_msg="User '" + username + "' not found")
        user_exists = [
            user for user in connection.users if user.username == username
        ]
        if len(user_exists) < 1:  #Should I instead raise an exception here?
            connection.users.append(user)
        #raise HTTPException(
        #	status_code=HTTP_409_CONFLICT,
        #	detail="Username '" + username + "' already assigned to this connection (id='" + str(conn_id) + "')"
        #)
    session.commit()
    return [user for user in connection.users]
예제 #8
0
async def change_connection(conn_id: int,
                            new_data: ConnectionInput,
                            session=Depends(get_db)):
    connection = get_connection(session,
                                conn_id,
                                HTTP_404_NOT_FOUND,
                                err_msg="Connection with id='" + str(conn_id) +
                                "' not found")
    connection_dict = connection.dict()
    exception = HTTPException(
        status_code=HTTP_409_CONFLICT,
        detail="Attempting to change to already existing connection")
    if not connection:
        raise exception
    for attr, val in new_data.dict().items():
        if val:
            connection_dict[attr] = val
    new_connection = Connection(**connection_dict)
    key_vals = {
        attr: getattr(new_connection, attr)
        for attr in ["hostname", "port", "type"]
    }
    new_connection_query = get_local_resources(db=session,
                                               Resource=Connection,
                                               key_vals=key_vals).first()
    if new_connection_query:
        raise exception
    new_connection_test = new_connection.test()
    if not new_connection_test[0]:
        exception.status_code = HTTP_503_SERVICE_UNAVAILABLE
        exception.detail = new_connection_test[1]
        raise exception
    return add_local_resource(db=session,
                              Resource=Connection,
                              user_input=new_data,
                              r_id=connection.id)
예제 #9
0
async def get_single_connection(conn_id: int, session=Depends(get_db)):
    return get_connection(session,
                          conn_id,
                          HTTP_404_NOT_FOUND,
                          err_msg="Connection with id='" + str(conn_id) +
                          "' not found")