def tier_2_search(self, ancestor_array, filter_string) -> list: filters = [ Q(has_faba=True), ] if len(ancestor_array) > 0: agency = ancestor_array[0] filters.append(Q(agency=agency)) if len(ancestor_array) > 1: fed_account = ancestor_array[1] filters.append(Q(federal_account__federal_account_code=fed_account)) if filter_string: filters.append( Q(Q(tas_rendering_label__icontains=filter_string) | Q(account_title__icontains=filter_string)) ) data = TreasuryAppropriationAccount.objects.annotate( has_faba=Exists(faba_with_file_D_data().filter(treasury_account=OuterRef("pk"))), agency=F("federal_account__parent_toptier_agency__toptier_code"), fed_account=F("federal_account__federal_account_code"), ).filter(*filters) retval = [] for item in data: ancestor_array = [item.agency, item.fed_account] retval.append( { "id": item.tas_rendering_label, "ancestors": ancestor_array, "description": item.account_title, "count": 0, "children": None, } ) return retval
def _tas_given_fa(self, agency, fed_account): return TreasuryAppropriationAccount.objects.annotate( has_faba=Exists(faba_with_file_D_data( ).filter(treasury_account=OuterRef("pk")))).filter( has_faba=True, federal_account__federal_account_code=fed_account, federal_account__parent_toptier_agency__toptier_code=agency, )
def _fa_given_agency(self, agency, filter_string): filters = [ Q(has_faba=True), Q(parent_toptier_agency__toptier_code=agency) ] return FederalAccount.objects.annotate( has_faba=Exists(faba_with_file_D_data().filter( treasury_account__federal_account=OuterRef("pk")))).filter( *filters)
def _toptier_search(self): agency_set = (ToptierAgency.objects.annotate( has_faba=Exists(faba_with_file_D_data().filter( treasury_account__federal_account__parent_toptier_agency= OuterRef("pk")))).filter(has_faba=True).values( "toptier_code", "name", "abbreviation")) agency_dictionaries = [ self._dictionary_from_agency(agency) for agency in agency_set ] cfo_sort_results = cfo_presentation_order(agency_dictionaries) return cfo_sort_results["cfo_agencies"] + cfo_sort_results[ "other_agencies"]
def _tas_given_fa(self, agency, fed_account, filter_string: str): filters = [ Q(has_faba=True), Q(federal_account__federal_account_code=fed_account), Q(federal_account__parent_toptier_agency__toptier_code=agency), ] if filter_string: filters.append( Q( Q(tas_rendering_label__icontains=filter_string) | Q(account_title__icontains=filter_string))) return TreasuryAppropriationAccount.objects.annotate( has_faba=Exists(faba_with_file_D_data().filter( treasury_account=OuterRef("pk")))).filter(*filters)
def tier_1_search(self, ancestor_array, filter_string, tier2_nodes=None) -> list: filters = [Q(has_faba=True)] query = Q() if tier2_nodes: fed_account = [node["ancestors"][1] for node in tier2_nodes] query |= Q(federal_account__federal_account_code__in=fed_account) if len(ancestor_array): agency = ancestor_array[0] filters.append(Q(agency=agency)) if filter_string: query |= Q( Q(federal_account__federal_account_code__icontains=filter_string) | Q(federal_account__account_title__icontains=filter_string) ) if query != Q(): filters.append(query) data = ( TreasuryAppropriationAccount.objects.annotate( has_faba=Exists(faba_with_file_D_data().filter(treasury_account=OuterRef("pk"))), ) .annotate(agency=F("federal_account__parent_toptier_agency__toptier_code")) .filter(*filters) .values("federal_account__federal_account_code", "federal_account__account_title", "agency") .annotate(count=Count("treasury_account_identifier")) ) retval = [] for item in data: ancestor_array = [item["agency"]] retval.append( { "id": item["federal_account__federal_account_code"], "ancestors": ancestor_array, "description": item["federal_account__account_title"], "count": item["count"], "children": None, } ) return retval
def toptier_search(self, filter_string=None, tier1_nodes=None): filters = [Q(has_faba=True)] query = Q() if tier1_nodes: agency_ids = [node["ancestors"][0] for node in tier1_nodes] query |= Q(federal_account__parent_toptier_agency__toptier_code__in=agency_ids) if filter_string: query |= Q( Q(federal_account__parent_toptier_agency__toptier_code__icontains=filter_string) | Q(federal_account__parent_toptier_agency__name__icontains=filter_string) ) if query != Q(): filters.append(query) agency_set = ( TreasuryAppropriationAccount.objects.annotate( has_faba=Exists(faba_with_file_D_data().filter(treasury_account=OuterRef("pk"))), ) .filter(*filters) .values( "federal_account__parent_toptier_agency__toptier_code", "federal_account__parent_toptier_agency__name", "federal_account__parent_toptier_agency__abbreviation", ) .annotate(count=Count("treasury_account_identifier")) ) agency_dictionaries = [self._dictionary_from_agency(agency) for agency in agency_set] cfo_sort_results = cfo_presentation_order(agency_dictionaries) agencies = cfo_sort_results["cfo_agencies"] + cfo_sort_results["other_agencies"] return [ { "id": agency["toptier_code"], "ancestors": [], "description": f"{agency['name']} ({agency['abbreviation']})", "count": agency["count"], "children": None, } for agency in agencies ]