human
Solve the following python programming problem given the following code:
@pick_context_manager_writer
def network_associate(context, project_id, network_id=None, force=False):
assistant
#Here is the Python program you requested.

    """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
    """
    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, 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 exception.NoMoreNetworks()
        network_ref['project_id'] = project_id
        context.session.add(network_ref)
    return network_ref