def remove_results_below_q_threshold(search_results):
    """removes buildings if total count of buildings grouped by org is less
    than their org's public query threshold

    :param list/queryset search_results: search results
    :returns: list or queryset
    """
    manual_group_by = {}
    thresholds = {}
    for b in search_results:
        parent_org = b.super_organization.get_parent()
        if parent_org.id not in manual_group_by:
            manual_group_by[parent_org.id] = 1
            thresholds[parent_org.id] = parent_org.query_threshold
        else:
            manual_group_by[parent_org.id] += 1
    # build list of parent orgs with not enough results
    orgs_below_threshold = []
    for org_id, count in manual_group_by.items():
        if count < thresholds[org_id]:
            orgs_below_threshold.append(org_id)
    # remove buildings from blacklisted parent orgs
    results = []
    for sr in search_results:
        if sr.super_organization.get_parent() not in orgs_below_threshold:
            results.append(sr)
    return results
dataset/ETHPy150Open buildingenergy/buildingenergy-platform/seed/search.py/remove_results_below_q_threshold/original