コード例 #1
0
    def state_results(self, filter_args: Dict[str, str],
                      lookup_fields: List[str], loc_lookup: str) -> List[dict]:
        # Adding additional state filters if specified
        if self.geo_layer_filters:
            self.queryset = self.queryset.filter(
                **{f"{loc_lookup}__in": self.geo_layer_filters})
        else:
            # Adding null filter for state for specific partial index
            # when not using geocode_filter
            filter_args[f"{loc_lookup}__isnull"] = False

        geo_queryset = self.queryset.filter(**filter_args).values(
            *lookup_fields)

        if self.subawards:
            geo_queryset = geo_queryset.annotate(
                transaction_amount=Sum("amount"))
        else:
            geo_queryset = geo_queryset.annotate(transaction_amount=Sum(
                "generated_pragmatic_obligation")).values(
                    "transaction_amount", *lookup_fields)

        state_pop_rows = PopCounty.objects.filter(county_number="000").values()
        populations = {
            row["state_name"].lower(): row["latest_population"]
            for row in state_pop_rows
        }

        # State names are inconsistent in database (upper, lower, null)
        # Used lookup instead to be consistent
        results = []
        for x in geo_queryset:
            shape_code = x[loc_lookup]
            per_capita = None
            population = populations.get(
                code_to_state.get(shape_code, {
                    "name": "None"
                }).get("name").lower())
            if population:
                per_capita = (Decimal(x["transaction_amount"]) /
                              Decimal(population)).quantize(Decimal(".01"))

            results.append({
                "shape_code":
                shape_code,
                "aggregated_amount":
                x["transaction_amount"],
                "display_name":
                code_to_state.get(shape_code, {
                    "name": "None"
                }).get("name").title(),
                "population":
                population,
                "per_capita":
                per_capita,
            })

        return results
コード例 #2
0
    def build_elasticsearch_result(self, response: dict) -> Dict[str, dict]:
        results = {}
        geo_info_buckets = response.get("group_by_agg_key",
                                        {}).get("buckets", [])
        for bucket in geo_info_buckets:
            if bucket.get("key") == "NULL":
                display_name = None
                shape_code = None
                population = None
            else:
                geo_info = json.loads(bucket.get("key"))
                state_code = geo_info["state_code"] or ""
                population = int(
                    geo_info["population"]) if geo_info["population"] else None

                if self.geo_layer == GeoLayer.STATE:
                    shape_code = state_code.upper()
                    display_name = geo_info["state_name"] or code_to_state.get(
                        state_code, {}).get("name", "")
                    display_name = display_name.title()
                elif self.geo_layer == GeoLayer.COUNTY:
                    state_fips = geo_info["state_fips"] or code_to_state.get(
                        state_code, {}).get("fips", "")
                    display_name = (geo_info["county_name"] or "").title()
                    shape_code = f"{state_fips}{geo_info['county_code']}"
                else:
                    state_fips = geo_info["state_fips"] or code_to_state.get(
                        state_code, {}).get("fips", "")
                    display_name = f"{state_code}-{geo_info['congressional_code']}".upper(
                    )
                    shape_code = f"{state_fips}{geo_info['congressional_code']}"

            per_capita = None
            amount = int(bucket.get("sum_field",
                                    {"value": 0})["value"]) / Decimal("100")

            if population:
                per_capita = (Decimal(amount) / Decimal(population)).quantize(
                    Decimal(".01"))

            results[shape_code] = {
                "amount": amount,
                "display_name": display_name or None,
                "shape_code": shape_code or None,
                "population": population,
                "per_capita": per_capita,
                "award_count": int(bucket.get("doc_count", 0)),
            }

        return results
コード例 #3
0
    def district_results(self, state_lookup: str, geo_queryset: QuerySet) -> List[dict]:
        # Returns congressional district results formatted for map

        state_pop_rows = PopCongressionalDistrict.objects.all().values()
        populations = {
            f"{row['state_code']}{row['congressional_district']}": row["latest_population"] for row in state_pop_rows
        }

        results = []
        for x in geo_queryset:
            shape_code = code_to_state.get(x[state_lookup])["fips"] + pad_codes(
                self.geo_layer.value, x["code_as_float"]
            )
            per_capita = None
            population = populations.get(shape_code)
            if population:
                per_capita = (Decimal(x["transaction_amount"]) / Decimal(population)).quantize(Decimal(".01"))

            results.append(
                {
                    "shape_code": shape_code,
                    "aggregated_amount": x["transaction_amount"],
                    "display_name": x[state_lookup] + "-" + pad_codes(self.geo_layer.value, x["code_as_float"]),
                    "population": population,
                    "per_capita": per_capita,
                }
            )

        return results
コード例 #4
0
ファイル: search.py プロジェクト: workexpress/usaspending-api
    def state_results(self, filter_args, lookup_fields, loc_lookup):
        # Adding additional state filters if specified
        if self.geo_layer_filters:
            self.queryset = self.queryset.filter(
                **{'{}__{}'.format(loc_lookup, 'in'): self.geo_layer_filters})
        else:
            # Adding null filter for state for specific partial index
            # when not using geocode_filter
            filter_args['{}__isnull'.format(loc_lookup)] = False

        self.geo_queryset = self.queryset.filter(**filter_args).values(
            *lookup_fields)

        if self.subawards:
            self.geo_queryset = self.geo_queryset.annotate(
                transaction_amount=Sum('amount'))
        else:
            self.geo_queryset = self.geo_queryset \
                .annotate(transaction_amount=Sum('generated_pragmatic_obligation')) \
                .values('transaction_amount', *lookup_fields)
        # State names are inconsistent in database (upper, lower, null)
        # Used lookup instead to be consistent
        results = [{
            'shape_code':
            x[loc_lookup],
            'aggregated_amount':
            x['transaction_amount'],
            'display_name':
            code_to_state.get(x[loc_lookup], {
                'name': 'None'
            }).get('name').title()
        } for x in self.geo_queryset]

        return results
コード例 #5
0
ファイル: search.py プロジェクト: mtomic/usaspending-api
    def state_results(self, filter_args, lookup_fields, loc_lookup):
        # Adding additional state filters if specified
        if self.geo_layer_filters:
            self.queryset = self.queryset.filter(
                **{'{}__{}'.format(loc_lookup, 'in'): self.geo_layer_filters})
        else:
            # Adding null filter for state for specific partial index
            # when not using geocode_filter
            filter_args['{}__isnull'.format(loc_lookup)] = False

        self.geo_queryset = self.queryset.filter(**filter_args) \
            .values(*lookup_fields)
        filter_types = self.filters[
            'award_type_codes'] if 'award_type_codes' in self.filters else award_type_mapping
        self.geo_queryset = sum_transaction_amount(self.geo_queryset,
                                                   filter_types=filter_types)

        # State names are inconsistent in database (upper, lower, null)
        # Used lookup instead to be consistent
        results = [{
            'shape_code':
            x[loc_lookup],
            'aggregated_amount':
            x['transaction_amount'],
            'display_name':
            code_to_state.get(x[loc_lookup], {
                'name': 'None'
            }).get('name').title()
        } for x in self.geo_queryset]

        return results
コード例 #6
0
    def state_results(self, filter_args, lookup_fields, loc_lookup):
        # Adding additional state filters if specified
        if self.geo_layer_filters:
            self.queryset = self.queryset.filter(**{'{}__{}'.format(loc_lookup, 'in'): self.geo_layer_filters})
        else:
            # Adding null filter for state for specific partial index
            # when not using geocode_filter
            filter_args['{}__isnull'.format(loc_lookup)] = False

        self.geo_queryset = self.queryset.filter(**filter_args).values(*lookup_fields)

        if self.subawards:
            self.geo_queryset = self.geo_queryset.annotate(transaction_amount=Sum('amount'))
        else:
            self.geo_queryset = self.geo_queryset \
                .annotate(transaction_amount=Sum('generated_pragmatic_obligation')) \
                .values('transaction_amount', *lookup_fields)
        # State names are inconsistent in database (upper, lower, null)
        # Used lookup instead to be consistent
        results = [{
            'shape_code': x[loc_lookup],
            'aggregated_amount': x['transaction_amount'],
            'display_name': code_to_state.get(x[loc_lookup], {'name': 'None'}).get('name').title()
        } for x in self.geo_queryset]

        return results
コード例 #7
0
    def state_results(self, filter_args, lookup_fields, loc_lookup):
        # Adding additional state filters if specified
        if self.geo_layer_filters:
            self.queryset = self.queryset.filter(**{"{}__{}".format(loc_lookup, "in"): self.geo_layer_filters})
        else:
            # Adding null filter for state for specific partial index
            # when not using geocode_filter
            filter_args["{}__isnull".format(loc_lookup)] = False

        self.geo_queryset = self.queryset.filter(**filter_args).values(*lookup_fields)

        if self.subawards:
            self.geo_queryset = self.geo_queryset.annotate(transaction_amount=Sum("amount"))
        else:
            self.geo_queryset = self.geo_queryset.annotate(
                transaction_amount=Sum("generated_pragmatic_obligation")
            ).values("transaction_amount", *lookup_fields)
        # State names are inconsistent in database (upper, lower, null)
        # Used lookup instead to be consistent
        results = [
            {
                "shape_code": x[loc_lookup],
                "aggregated_amount": x["transaction_amount"],
                "display_name": code_to_state.get(x[loc_lookup], {"name": "None"}).get("name").title(),
            }
            for x in self.geo_queryset
        ]

        return results
コード例 #8
0
    def county_results(self, state_lookup: str, county_name: str, geo_queryset: QuerySet) -> List[dict]:
        # Returns county results formatted for map
        state_pop_rows = PopCounty.objects.exclude(county_number="000").values()
        populations = {f"{row['state_code']}{row['county_number']}": row["latest_population"] for row in state_pop_rows}

        results = []
        for x in geo_queryset:
            shape_code = code_to_state.get(x[state_lookup])["fips"] + pad_codes(
                self.geo_layer.value, x["code_as_float"]
            )
            per_capita = None
            population = populations.get(shape_code)
            if population:
                per_capita = (Decimal(x["transaction_amount"]) / Decimal(population)).quantize(Decimal(".01"))

            results.append(
                {
                    "shape_code": shape_code,
                    "aggregated_amount": x["transaction_amount"],
                    "display_name": x[county_name].title() if x[county_name] is not None else x[county_name],
                    "population": population,
                    "per_capita": per_capita,
                }
            )

        return results
コード例 #9
0
ファイル: search.py プロジェクト: kristen178/usaspending-api
    def district_results(self, state_lookup):
        # Returns congressional district results formatted for map
        results = [{
            'shape_code': code_to_state.get(x[state_lookup])['fips'] + pad_codes(self.geo_layer, x['code_as_float']),
            'aggregated_amount': x['transaction_amount'],
            'display_name': x[state_lookup] + '-' + pad_codes(self.geo_layer, x['code_as_float'])
        } for x in self.geo_queryset]

        return results
コード例 #10
0
    def district_results(self, state_lookup):
        # Returns congressional district results formatted for map
        results = [{
            'shape_code': code_to_state.get(x[state_lookup])['fips'] + pad_codes(self.geo_layer, x['code_as_float']),
            'aggregated_amount': x['transaction_amount'],
            'display_name': x[state_lookup] + '-' + pad_codes(self.geo_layer, x['code_as_float'])
        } for x in self.geo_queryset]

        return results
コード例 #11
0
    def county_results(self, state_lookup, county_name):
        # Returns county results formatted for map
        results = [{
            'shape_code': code_to_state.get(x[state_lookup])['fips'] + pad_codes(self.geo_layer, x['code_as_float']),
            'aggregated_amount': x['transaction_amount'],
            'display_name': x[county_name].title() if x[county_name] is not None else x[county_name]
        } for x in self.geo_queryset]

        return results
コード例 #12
0
ファイル: search.py プロジェクト: kristen178/usaspending-api
    def county_results(self, state_lookup, county_name):
        # Returns county results formatted for map
        results = [{
            'shape_code': code_to_state.get(x[state_lookup])['fips'] + pad_codes(self.geo_layer, x['code_as_float']),
            'aggregated_amount': x['transaction_amount'],
            'display_name': x[county_name].title() if x[county_name] is not None else x[county_name]
        } for x in self.geo_queryset]

        return results
コード例 #13
0
    def fill_missing_state_data(self):
        """Fills in blank US state names or codes from its counterpart"""

        if self.state_code and self.state_name:
            return
        if self.country_name == 'UNITED STATES':
            if (not self.state_code):
                self.state_code = state_to_code.get(self.state_name)
            elif (not self.state_name):
                self.state_name = code_to_state.get(self.state_code)
コード例 #14
0
    def district_results(self, state_lookup):
        # Returns congressional district results formatted for map
        results = [
            {
                "shape_code": code_to_state.get(x[state_lookup])["fips"]
                + pad_codes(self.geo_layer, x["code_as_float"]),
                "aggregated_amount": x["transaction_amount"],
                "display_name": x[state_lookup] + "-" + pad_codes(self.geo_layer, x["code_as_float"]),
            }
            for x in self.geo_queryset
        ]

        return results
コード例 #15
0
    def county_results(self, state_lookup, county_name):
        # Returns county results formatted for map
        results = [
            {
                "shape_code": code_to_state.get(x[state_lookup])["fips"]
                + pad_codes(self.geo_layer, x["code_as_float"]),
                "aggregated_amount": x["transaction_amount"],
                "display_name": x[county_name].title() if x[county_name] is not None else x[county_name],
            }
            for x in self.geo_queryset
        ]

        return results
コード例 #16
0
    def fill_missing_state_data(self):
        """Fills in blank US state names or codes from its counterpart"""

        if self.state_code and self.state_name:
            return
        if self.country_name == "UNITED STATES":
            if not self.state_code:
                self.state_code = state_to_code.get(self.state_name)
            elif not self.state_name:
                state_obj = code_to_state.get(self.state_code)

                if state_obj:
                    self.state_name = state_obj["name"]
コード例 #17
0
    def fill_missing_state_data(self):
        """Fills in blank US state names or codes from its counterpart"""

        if self.state_code and self.state_name:
            return
        if self.country_name == 'UNITED STATES':
            if not self.state_code:
                self.state_code = state_to_code.get(self.state_name)
            elif not self.state_name:
                state_obj = code_to_state.get(self.state_code)

                if state_obj:
                    self.state_name = state_obj['name']
コード例 #18
0
def legal_entity_state_name(broker_input):
    if broker_input["legal_entity_state_descrip"]:
        return capitalize_if_string(broker_input["legal_entity_state_descrip"])
    elif broker_input["legal_entity_state_code"]:
        return code_to_state.get(broker_input["legal_entity_state_code"], {}).get("name")
    return None
コード例 #19
0
def place_of_performance_state_description(broker_input):
    if broker_input["place_of_perfor_state_desc"]:
        return capitalize_if_string(broker_input["place_of_perfor_state_desc"])
    elif broker_input["place_of_performance_state"]:
        return code_to_state.get(broker_input["place_of_performance_state"], {}).get("name")
    return None