Example #1
0
def load_locs_json(domain, selected_loc_id=None):
    """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
    """
    def loc_to_json(loc):
        return {
            'name': loc.name,
            'location_type': loc.location_type,
            'uuid': loc._id,
        }
    loc_json = [loc_to_json(loc) for loc in root_locations(domain)]

    # 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 = Location.get(selected_loc_id)
        lineage = list(Location.view('_all_docs', keys=selected.path, include_docs=True))

        parent = {'children': loc_json}
        for loc in lineage:
            # find existing entry in the json tree that corresponds to this loc
            this_loc = [k for k in parent['children'] if k['uuid'] == loc._id][0]
            this_loc['children'] = [loc_to_json(loc) for loc in loc.children]
            parent = this_loc

    return loc_json
Example #2
0
def load_locs_json(domain, selected_loc_id=None):
    """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
    """

    def loc_to_json(loc):
        return {"name": loc.name, "location_type": loc.location_type, "uuid": loc._id}

    loc_json = [loc_to_json(loc) for loc in root_locations(domain)]

    # 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
    selected = Location.get_in_domain(domain, selected_loc_id)
    if selected:
        lineage = list(Location.view("_all_docs", keys=selected.path, include_docs=True))

        parent = {"children": loc_json}
        for loc in lineage:
            # find existing entry in the json tree that corresponds to this loc
            this_loc = [k for k in parent["children"] if k["uuid"] == loc._id][0]
            this_loc["children"] = [loc_to_json(loc) for loc in loc.children]
            parent = this_loc

    return loc_json
Example #3
0
    def obj_get_list(self, request, **kwargs):
        domain = kwargs['domain']
        parent_id = request.GET.get("parent_id", None)
        if parent_id:
            parent = get_object_or_not_exist(Location, parent_id, domain)
            return parent.children

        return root_locations(domain)
Example #4
0
    def obj_get_list(self, bundle, **kwargs):
        domain = kwargs['domain']
        parent_id = bundle.request.GET.get("parent_id", None)
        if parent_id:
            parent = get_object_or_not_exist(Location, parent_id, domain)
            return parent.children

        return root_locations(domain)
Example #5
0
    def obj_get_list(self, bundle, **kwargs):
        domain = kwargs['domain']
        parent_id = bundle.request.GET.get('parent_id', None)
        include_inactive = json.loads(bundle.request.GET.get('include_inactive', 'false'))
        if parent_id:
            parent = get_object_or_not_exist(Location, parent_id, domain)
            return parent.sql_location.child_locations(include_archive_ancestors=include_inactive)

        return root_locations(domain)