(domain, selected_loc_id=None, include_archived=False,
        user=None, only_administrative=False)
return loc_json
initialize a json location tree for drill-down controls on
    the client. tree is only partially initialized and branches
    will be filled in on the client via ajax.

    what is initialized:
    * all top level locs
    * if a 'selected' loc is provided, that loc and its complete
      ancestry

    only_administrative - if False get all locations
                          if True get only administrative locations
initialize a json location tree for drill-down controls on
    the client. tree is only partially initialized and branches
    will be filled in on the client via ajax.
initialize a json location tree for drill - down controls on the client . tree is only partially initialized and branches will be filled in on the client via ajax .
def load_locs_json(domain, selected_loc_id=None, include_archived=False,
        user=None, only_administrative=False):
    """initialize a json location tree for drill-down controls on
    the client. tree is only partially initialized and branches
    will be filled in on the client via ajax.

    what is initialized:
    * all top level locs
    * if a 'selected' loc is provided, that loc and its complete
      ancestry

    only_administrative - if False get all locations
                          if True get only administrative locations
    """
    def loc_to_json(loc):
        return {
            'name': loc.name,
            'location_type': loc.location_type.name,  # todo: remove when types aren't optional
            'uuid': loc.location_id,
            'is_archived': loc.is_archived,
            'can_edit': True
        }

    locations = SQLLocation.root_locations(
        domain, include_archive_ancestors=include_archived
    )

    if only_administrative:
        locations = locations.filter(location_type__administrative=True)

    loc_json = [loc_to_json(loc) for loc in locations]

    # if a location is selected, we need to pre-populate its location hierarchy
    # so that the data is available client-side to pre-populate the drop-downs
    if selected_loc_id:
        selected = SQLLocation.objects.get(
            domain=domain,
            location_id=selected_loc_id
        )

        lineage = selected.get_ancestors()

        parent = {'children': loc_json}
        for loc in lineage:
            children = loc.child_locations(include_archive_ancestors=include_archived)
            if only_administrative:
                children = children.filter(location_type__administrative=True)

            # find existing entry in the json tree that corresponds to this loc
            try:
                this_loc = [k for k in parent['children'] if k['uuid'] == loc.location_id][0]
            except IndexError:
                # if we couldn't find this location the view just break out of the loop.
                # there are some instances in viewing archived locations where we don't actually
                # support drilling all the way down.
                break
            this_loc['children'] = [loc_to_json(loc) for loc in children]
            parent = this_loc

    return loc_json
def load_locs_json ( domain , selected_loc_id = None , include_archived = False , user = None , only_administrative = False ) : def loc_to_json ( loc ) : return { 'name' : loc . name , 'location_type' : loc . location_type . name , # todo: remove when types aren't optional 'uuid' : loc . location_id , 'is_archived' : loc . is_archived , 'can_edit' : True } locations = SQLLocation . root_locations ( domain , include_archive_ancestors = include_archived ) if only_administrative : locations = locations . filter ( location_type__administrative = True ) loc_json = [ loc_to_json ( loc ) for loc in locations ] # if a location is selected, we need to pre-populate its location hierarchy # so that the data is available client-side to pre-populate the drop-downs if selected_loc_id : selected = SQLLocation . objects . get ( domain = domain , location_id = selected_loc_id ) lineage = selected . get_ancestors ( ) parent = { 'children' : loc_json } for loc in lineage : children = loc . child_locations ( include_archive_ancestors = include_archived ) if only_administrative : children = children . filter ( location_type__administrative = True ) # find existing entry in the json tree that corresponds to this loc try : this_loc = [ k for k in parent [ 'children' ] if k [ 'uuid' ] == loc . location_id ] [ 0 ] except IndexError : # if we couldn't find this location the view just break out of the loop. # there are some instances in viewing archived locations where we don't actually # support drilling all the way down. break this_loc [ 'children' ] = [ loc_to_json ( loc ) for loc in children ] parent = this_loc return loc_json