def county_district_queryset(self, kwargs, fields_list, loc_lookup, state_lookup, scope_field_name): # Filtering queryset to specific county/districts if requested # Since geo_layer_filters comes as concat of state fips and county/district codes # need to split for the geocode_filter if self.geo_layer_filters: geo_layers_list = [ {"state": fips_to_code.get(x[:2]), self.geo_layer: x[2:], "country": "USA"} for x in self.geo_layer_filters ] self.queryset = self.queryset.filter(geocode_filter_locations(scope_field_name, geo_layers_list)) else: # Adding null, USA, not number filters for specific partial index when not using geocode_filter kwargs["{}__{}".format(loc_lookup, "isnull")] = False kwargs["{}__{}".format(state_lookup, "isnull")] = False kwargs["{}_country_code".format(scope_field_name)] = "USA" kwargs["{}__{}".format(loc_lookup, "iregex")] = r"^[0-9]*(\.\d+)?$" # Turn county/district codes into float since inconsistent in database # Codes in location table ex: '01', '1', '1.0' # Cast will group codes as a float and will combine inconsistent codes self.geo_queryset = ( self.queryset.filter(**kwargs).values(*fields_list).annotate(code_as_float=Cast(loc_lookup, FloatField())) ) 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", "code_as_float", *fields_list) return self.geo_queryset
def county_district_queryset(self, kwargs, fields_list, loc_lookup, state_lookup, scope_field_name): # Filtering queryset to specific county/districts if requested # Since geo_layer_filters comes as concat of state fips and county/district codes # need to split for the geocode_filter if self.geo_layer_filters: geo_layers_list = [ {'state': fips_to_code.get(x[:2]), self.geo_layer: x[2:], 'country': 'USA'} for x in self.geo_layer_filters ] self.queryset = self.queryset.filter(geocode_filter_locations(scope_field_name, geo_layers_list, True)) else: # Adding null, USA, not number filters for specific partial index when not using geocode_filter kwargs['{}__{}'.format(loc_lookup, 'isnull')] = False kwargs['{}__{}'.format(state_lookup, 'isnull')] = False kwargs['{}_country_code'.format(scope_field_name)] = 'USA' kwargs['{}__{}'.format(loc_lookup, 'iregex')] = r'^[0-9]*(\.\d+)?$' # Turn county/district codes into float since inconsistent in database # Codes in location table ex: '01', '1', '1.0' # Cast will group codes as a float and will combine inconsistent codes self.geo_queryset = self.queryset.filter(**kwargs) \ .values(*fields_list) \ .annotate(code_as_float=Cast(loc_lookup, FloatField())) 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', 'code_as_float', *fields_list) return self.geo_queryset
def county_district_queryset(self, kwargs, fields_list, loc_lookup, state_lookup, scope_field_name): # Filtering queryset to specific county/districts if requested # Since geo_layer_filters comes as concat of state fips and county/district codes # need to split for the geocode_filter if self.geo_layer_filters: self.queryset &= geocode_filter_locations(scope_field_name, [ {'state': fips_to_code.get(x[:2]), self.geo_layer: x[2:], 'country': 'USA'} for x in self.geo_layer_filters ], self.model_name, not self.subawards) else: # Adding null, USA, not number filters for specific partial index when not using geocode_filter kwargs['{}__{}'.format(loc_lookup, 'isnull')] = False kwargs['{}__{}'.format(state_lookup, 'isnull')] = False kwargs['{}_{}country_code'.format(scope_field_name, '_location_' if self.subawards else '')] = 'USA' kwargs['{}__{}'.format(loc_lookup, 'iregex')] = r'^[0-9]*(\.\d+)?$' # Turn county/district codes into float since inconsistent in database # Codes in location table ex: '01', '1', '1.0' # Cast will group codes as a float and will combine inconsistent codes self.geo_queryset = self.queryset.filter(**kwargs) \ .values(*fields_list) \ .annotate(code_as_float=Cast(loc_lookup, FloatField())) 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) if not self.subawards \ else self.geo_queryset.annotate(transaction_amount=Sum('amount')) return self.geo_queryset