human
Complete the below
@require_admin_context
def network_associate(context, project_id, network_id=None, force=False):

    ''''Associate a project with a network.
    called by project_get_networks under certain conditions
    and network manager add_network_to_project()
    only associate if the project doesn\'t already have a network
    or if force is True
    force solves race condition where a fresh project has multiple instance
    builds simultaneously picked up by multiple network hosts which attempt
    to associate the project with multiple networks
    force should only be used as a direct consequence of user request
    all automated requests should not use force'
    '''

Give me a the Python program you requested?
assistant
#Here is the Python program you requested.
    session = get_session()
    with session.begin():
        def network_query(project_filter, id=None):
            filter_kwargs = {'project_id': project_filter}
            if (id is not None):
                filter_kwargs['id'] = id
            return model_query(context, models.Network, session=session, read_deleted='no').filter_by(**filter_kwargs).with_lockmode('update').first()
        if (not force):
            network_ref = network_query(project_id)
        if (force or (not network_ref)):
            network_ref = network_query(None, network_id)
            if (not network_ref):
                raise db.NoMoreNetworks()
            network_ref['project_id'] = project_id
            session.add(network_ref)
    return network_ref