def find_related(request):
    """
    Given a list of site, vlan, and network primary keys, help a user make
    choices about where to put an IP address

    A user can select from choices:
        Networks
        Vlans
        Sites

    The goal of the UI is to help a user choose a range -- which for this
    function can be seen as filtering down to exactly 1 network.

    When a user selects a site, this can limit which networks and in turn which
    vlans are displayed.

    When a user selects a vlan, this can limit which networks are displayed
    which in turn can limit which sites are displayed

    When a user selects a network, this will limit both networks, vlans, and
    sites to at most one object per each type.

    input::
        {
            'choice': [<type>, <pk>],
            'sites': [1, ...],
            'vlans': [1, ...],
            'networks': [1, ...],
        }

    The value of '<type>' is a string that is either 'site', 'vlan', or
    'network'. The value of '<pk>' is a number.

    output:
        Same as input but with things filtered plus a new list of 'range'
        information. E.x.:
        {
            'sites': [<pks>],
            'vlans': [<pks>],
            'networks': [<pks>],
            'range': [
                {'name': ...
                 'ip_start': ...
                 'ip_end': ...
                 'reserved': ...
                },
                ...
                ...
                ...
                {'name': ...
                 'ip_start': ...
                 'ip_end': ...
                 'reserved': ...
                }
            ]
        }

    This function will key off of 'choice' to determine how to slim down a
    users choice of objects.
    """
    state = json.loads(request.raw_post_data)

    if not state:
        raise Exception("No state?")

    if 'choice' not in state:
        raise Exception("No choice?")

    try:
        choice_type, choice_pk = state['choice']
    except __HOLE__:
        raise Exception(
            "Choice was '{0}'. This is wrong".format(state['choice'])
        )

    filter_network, filter_site, filter_vlan = calculate_filters(
        choice_type, choice_pk
    )
    format_network, format_site, format_vlan = label_value_maker()

    new_state = {
        'sites': format_site(filter_site(state['sites'])),
        'vlans': format_vlan(filter_vlan(state['vlans'])),
    }

    # Network are special. If there is only one, we need to add some range
    # info. If there are zero or more than one, don't add any range objects
    networks = filter_network(state['networks'])
    if len(networks) == 1:
        new_state['ranges'] = integrate_real_ranges(
            networks[0], calc_template_ranges(networks[0])
        )
    new_state['networks'] = format_network(networks)

    return HttpResponse(json.dumps(new_state))
dataset/ETHPy150Open mozilla/inventory/core/range/views.py/find_related