def get(self, request, recipient_id): get_request = request.query_params year = validate_year(get_request.get("year", "latest")) recipient_hash, recipient_level = validate_recipient_id(recipient_id) recipient_duns, recipient_name = extract_name_duns_from_hash( recipient_hash) if not (recipient_name or recipient_duns): raise InvalidParameterException( "Recipient Hash not found: '{}'.".format(recipient_hash)) alternate_names = (RecipientLookup.objects.filter( recipient_hash=recipient_hash).values("alternate_names").first()) alternate_names = sorted(alternate_names.get("alternate_names", [])) parents = [] if recipient_level == "C": parents = extract_parents_from_hash(recipient_hash) elif recipient_level == "P": parents = [{ "parent_id": recipient_id, "parent_duns": recipient_duns, "parent_name": recipient_name }] location = extract_location(recipient_hash) business_types = extract_business_categories(recipient_name, recipient_duns, recipient_hash) results = obtain_recipient_totals(recipient_id, year=year, subawards=False) # subtotal, subcount = obtain_recipient_totals(recipient_hash, recipient_level, year=year, subawards=False) parent_id, parent_name, parent_duns = None, None, None if parents: parent_id = parents[0].get("parent_id") parent_name = parents[0].get("parent_name") parent_duns = parents[0].get("parent_duns") result = { "name": recipient_name, "alternate_names": alternate_names, "duns": recipient_duns, "recipient_id": recipient_id, "recipient_level": recipient_level, "parent_id": parent_id, "parent_name": parent_name, "parent_duns": parent_duns, "parents": parents, "business_types": business_types, "location": location, "total_transaction_amount": results[0]["total"] if results else 0, "total_transactions": results[0]["count"] if results else 0, # 'total_sub_transaction_amount': subtotal, # 'total_sub_transaction_total': subcount } return Response(result)
def get(self, request, duns): get_request = request.query_params year = validate_year(get_request.get('year', 'latest')) parent_hash, parent_name = extract_hash_name_from_duns(duns) if not parent_hash: raise InvalidParameterException('DUNS not found: \'{}\'.'.format(duns)) totals = list(obtain_recipient_totals('{}-P'.format(parent_hash), children=True, year=year, subawards=False)) # Get child info for each child DUNS results = [] for total in totals: results.append({ 'recipient_id': '{}-C'.format(total['recipient_hash']), 'name': total['recipient_name'], 'duns': total['recipient_unique_id'], 'amount': total['total'] }) # Add children recipients without totals in this time period (if we already got all, ignore) if year != 'all': # Get all possible child duns children_duns = RecipientProfile.objects.filter(recipient_hash=parent_hash, recipient_level='P').values( 'recipient_affiliations') if not children_duns: raise InvalidParameterException('DUNS is not listed as a parent: \'{}\'.'.format(duns)) children = children_duns[0]['recipient_affiliations'] # Gather their data points with Recipient Profile found_duns = [result['duns'] for result in results] missing_duns = [duns for duns in children if duns not in found_duns] missing_duns_qs = RecipientProfile.objects.filter(recipient_unique_id__in=missing_duns, recipient_level='C').values('recipient_hash', 'recipient_name', 'recipient_unique_id') for child_duns in list(missing_duns_qs): results.append({ 'recipient_id': '{}-C'.format(child_duns['recipient_hash']), 'name': child_duns['recipient_name'], 'duns': child_duns['recipient_unique_id'], 'amount': 0 }) # Add state/provinces to each result child_hashes = [result['recipient_id'][:-2] for result in results] states_qs = RecipientLookup.objects.filter(recipient_hash__in=child_hashes).values('recipient_hash', 'state') state_map = {str(state_result['recipient_hash']): state_result['state'] for state_result in list(states_qs)} for result in results: recipient_hash = result['recipient_id'][:-2] if recipient_hash not in state_map: logger.warning('Recipient Hash not in state map: {}'.format(recipient_hash)) else: result['state_province'] = state_map[recipient_hash] return Response(results)
def get(self, request, recipient_id): get_request = request.query_params year = validate_year(get_request.get('year', 'latest')) recipient_hash, recipient_level = validate_recipient_id(recipient_id) recipient_duns, recipient_name = extract_name_duns_from_hash( recipient_hash) if not (recipient_name or recipient_duns): raise InvalidParameterException( 'Recipient Hash not found: \'{}\'.'.format(recipient_hash)) parents = [] if recipient_level == "C": parents = extract_parents_from_hash(recipient_hash) elif recipient_level == "P": parents = [{ "parent_id": recipient_id, "parent_duns": recipient_duns, "parent_name": recipient_name }] location = extract_location(recipient_hash) business_types = extract_business_categories(recipient_name, recipient_duns) results = obtain_recipient_totals(recipient_id, year=year, subawards=False) # subtotal, subcount = obtain_recipient_totals(recipient_hash, recipient_level, year=year, subawards=False) parent_id, parent_name, parent_duns = None, None, None if parents: parent_id = parents[0].get("parent_id") parent_name = parents[0].get("parent_name") parent_duns = parents[0].get("parent_duns") result = { 'name': recipient_name, 'duns': recipient_duns, 'recipient_id': recipient_id, 'recipient_level': recipient_level, 'parent_id': parent_id, 'parent_name': parent_name, 'parent_duns': parent_duns, 'parents': parents, 'business_types': business_types, 'location': location, 'total_transaction_amount': results[0]['total'] if results else 0, 'total_transactions': results[0]['count'] if results else 0, # 'total_sub_transaction_amount': subtotal, # 'total_sub_transaction_total': subcount } return Response(result)
def get(self, request, fips): get_request = request.query_params year = validate_year(get_request.get('year', 'latest')) fips = validate_fips(fips) results = [] for award_type, award_type_codes in _all_award_types_mappings.items(): result = obtain_state_totals(fips, year=year, award_type_codes=award_type_codes) results.append({ 'type': award_type, 'amount': result['total'], 'count': result['count'], }) return Response(results)
def get(self, request, fips): get_request = request.query_params year = validate_year(get_request.get("year", "latest")) fips = validate_fips(fips) results = [] for award_type, award_type_codes in _all_award_types_mappings.items(): result = obtain_state_totals(fips, year=year, award_type_codes=award_type_codes) results.append({ "type": award_type, "amount": result["total"], "count": result["count"] }) return Response(results)
def get(self, request, fips): get_request = request.query_params year = validate_year(get_request.get("year", "latest")) fips = validate_fips(fips) state_data_qs = StateData.objects.filter(fips=fips) state_data_results = state_data_qs.values() general_state_data = state_data_results[0] state_pop_data = self.get_state_data(state_data_results, "population", year) state_mhi_data = self.get_state_data(state_data_results, "median_household_income", year) state_aggregates = obtain_state_totals(fips, year=year) if year == "all" or (year and year.isdigit() and int(year) == generate_fiscal_year(datetime.now())): amt_per_capita = None else: amt_per_capita = (round( state_aggregates["total"] / state_pop_data["population"], 2) if state_aggregates["count"] else 0) result = { "name": general_state_data["name"], "code": general_state_data["code"], "fips": general_state_data["fips"], "type": general_state_data["type"], "population": state_pop_data["population"], "pop_year": state_pop_data["year"], "pop_source": state_pop_data["pop_source"], "median_household_income": state_mhi_data["median_household_income"], "mhi_year": state_mhi_data["year"], "mhi_source": state_mhi_data["mhi_source"], "total_prime_amount": state_aggregates["total"], "total_prime_awards": state_aggregates["count"], "award_amount_per_capita": amt_per_capita, # Commented out for now # 'total_subaward_amount': total_subaward_amount, # 'total_subawards': total_subaward_count, } return Response(result)
def get(self, request, fips): get_request = request.query_params year = validate_year(get_request.get('year', 'latest')) fips = validate_fips(fips) state_data_qs = StateData.objects.filter(fips=fips) state_data_results = state_data_qs.values() general_state_data = state_data_results[0] state_pop_data = self.get_state_data(state_data_results, 'population', year) state_mhi_data = self.get_state_data(state_data_results, 'median_household_income', year) state_aggregates = obtain_state_totals(fips, year=year) if year == 'all' or (year and year.isdigit() and int(year) == generate_fiscal_year(datetime.now())): amt_per_capita = None else: amt_per_capita = (round( state_aggregates['total'] / state_pop_data['population'], 2) if state_aggregates['count'] else 0) result = { 'name': general_state_data['name'], 'code': general_state_data['code'], 'fips': general_state_data['fips'], 'type': general_state_data['type'], 'population': state_pop_data['population'], 'pop_year': state_pop_data['year'], 'pop_source': state_pop_data['pop_source'], 'median_household_income': state_mhi_data['median_household_income'], 'mhi_year': state_mhi_data['year'], 'mhi_source': state_mhi_data['mhi_source'], 'total_prime_amount': state_aggregates['total'], 'total_prime_awards': state_aggregates['count'], 'award_amount_per_capita': amt_per_capita, # Commented out for now # 'total_subaward_amount': total_subaward_amount, # 'total_subawards': total_subaward_count, } return Response(result)
def get(self, request, recipient_id): get_request = request.query_params year = validate_year(get_request.get('year', 'latest')) recipient_hash, recipient_level = validate_recipient_id(recipient_id) recipient_duns, recipient_name = extract_name_duns_from_hash(recipient_hash) if not (recipient_name or recipient_duns): raise InvalidParameterException('Recipient Hash not found: \'{}\'.'.format(recipient_hash)) parents = [] if recipient_level == "C": parents = extract_parents_from_hash(recipient_hash) elif recipient_level == "P": parents = [{"parent_id": recipient_id, "parent_duns": recipient_duns, "parent_name": recipient_name}] location = extract_location(recipient_hash) business_types = extract_business_categories(recipient_name, recipient_duns) results = obtain_recipient_totals(recipient_id, year=year, subawards=False) # subtotal, subcount = obtain_recipient_totals(recipient_hash, recipient_level, year=year, subawards=False) parent_id, parent_name, parent_duns = None, None, None if parents: parent_id = parents[0].get("parent_id") parent_name = parents[0].get("parent_name") parent_duns = parents[0].get("parent_duns") result = { 'name': recipient_name, 'duns': recipient_duns, 'recipient_id': recipient_id, 'recipient_level': recipient_level, 'parent_id': parent_id, 'parent_name': parent_name, 'parent_duns': parent_duns, 'parents': parents, 'business_types': business_types, 'location': location, 'total_transaction_amount': results[0]['total'] if results else 0, 'total_transactions': results[0]['count'] if results else 0, # 'total_sub_transaction_amount': subtotal, # 'total_sub_transaction_total': subcount } return Response(result)
def test_validate_year_failure(): year = 'abc' with pytest.raises(InvalidParameterException): validate_year(year)
def test_validate_year_success_all(): year = 'all' assert validate_year(year) == year
def test_validate_year_success_latest(): year = 'latest' assert validate_year(year) == year
def get(self, request, duns): get_request = request.query_params year = validate_year(get_request.get('year', 'latest')) parent_hash, parent_name = extract_hash_name_from_duns(duns) if not parent_hash: raise InvalidParameterException( 'DUNS not found: \'{}\'.'.format(duns)) totals = list( obtain_recipient_totals('{}-P'.format(parent_hash), children=True, year=year, subawards=False)) # Get child info for each child DUNS results = [] for total in totals: results.append({ 'recipient_id': '{}-C'.format(total['recipient_hash']), 'name': total['recipient_name'], 'duns': total['recipient_unique_id'], 'amount': total['total'] }) # Add children recipients without totals in this time period (if we already got all, ignore) if year != 'all': # Get all possible child duns children_duns = RecipientProfile.objects.filter( recipient_hash=parent_hash, recipient_level='P').values('recipient_affiliations') if not children_duns: raise InvalidParameterException( 'DUNS is not listed as a parent: \'{}\'.'.format(duns)) children = children_duns[0]['recipient_affiliations'] # Gather their data points with Recipient Profile found_duns = [result['duns'] for result in results] missing_duns = [ duns for duns in children if duns not in found_duns ] missing_duns_qs = RecipientProfile.objects.filter( recipient_unique_id__in=missing_duns, recipient_level='C').values('recipient_hash', 'recipient_name', 'recipient_unique_id') for child_duns in list(missing_duns_qs): results.append({ 'recipient_id': '{}-C'.format(child_duns['recipient_hash']), 'name': child_duns['recipient_name'], 'duns': child_duns['recipient_unique_id'], 'amount': 0 }) # Add state/provinces to each result child_hashes = [result['recipient_id'][:-2] for result in results] states_qs = RecipientLookup.objects.filter( recipient_hash__in=child_hashes).values('recipient_hash', 'state') state_map = { str(state_result['recipient_hash']): state_result['state'] for state_result in list(states_qs) } for result in results: recipient_hash = result['recipient_id'][:-2] if recipient_hash not in state_map: logger.warning('Recipient Hash not in state map: {}'.format( recipient_hash)) else: result['state_province'] = state_map[recipient_hash] return Response(results)
def test_validate_year_success_digit(): year = '2000' assert validate_year(year) == year
def get(self, request, duns): get_request = request.query_params year = validate_year(get_request.get("year", "latest")) parent_hash, parent_name = extract_hash_name_from_duns(duns) if not parent_hash: raise InvalidParameterException( "DUNS not found: '{}'.".format(duns)) totals = list( obtain_recipient_totals("{}-P".format(parent_hash), children=True, year=year, subawards=False)) # Get child info for each child DUNS results = [] for total in totals: results.append({ "recipient_id": "{}-C".format(total["recipient_hash"]), "name": total["recipient_name"], "duns": total["recipient_unique_id"], "amount": total["total"], }) # Add children recipients without totals in this time period (if we already got all, ignore) if year != "all": # Get all possible child duns children_duns = RecipientProfile.objects.filter( recipient_hash=parent_hash, recipient_level="P").values("recipient_affiliations") if not children_duns: raise InvalidParameterException( "DUNS is not listed as a parent: '{}'.".format(duns)) children = children_duns[0]["recipient_affiliations"] # Gather their data points with Recipient Profile found_duns = [result["duns"] for result in results] missing_duns = [ duns for duns in children if duns not in found_duns ] missing_duns_qs = RecipientProfile.objects.filter( recipient_unique_id__in=missing_duns, recipient_level="C").values("recipient_hash", "recipient_name", "recipient_unique_id") for child_duns in list(missing_duns_qs): results.append({ "recipient_id": "{}-C".format(child_duns["recipient_hash"]), "name": child_duns["recipient_name"], "duns": child_duns["recipient_unique_id"], "amount": 0, }) # Add state/provinces to each result child_hashes = [result["recipient_id"][:-2] for result in results] states_qs = RecipientLookup.objects.filter( recipient_hash__in=child_hashes).values("recipient_hash", "state") state_map = { str(state_result["recipient_hash"]): state_result["state"] for state_result in list(states_qs) } for result in results: recipient_hash = result["recipient_id"][:-2] if recipient_hash not in state_map: logger.warning("Recipient Hash not in state map: {}".format( recipient_hash)) else: result["state_province"] = state_map[recipient_hash] return Response(results)
def test_validate_year_success_digit(): year = '2000' assert validate_year(year) == year
def test_validate_year_success_all(): year = 'all' assert validate_year(year) == year
def test_validate_year_success_latest(): year = 'latest' assert validate_year(year) == year
def test_validate_year_failure(): year = 'abc' with pytest.raises(InvalidParameterException): validate_year(year)