Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add issuedate range and warngeom spatial filters to weather warnings api #539

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion firecares/firestation/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from .forms import StaffingForm
from .models import FireStation, Staffing, FireDepartment, ParcelDepartmentHazardLevel, EffectiveFireFightingForceLevel
from ..utils.tastypie_geodjango import allow_geodjango_filters
from firecares.weather.models import DepartmentWarnings
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from django.core.serializers.json import DjangoJSONEncoder
Expand Down Expand Up @@ -392,13 +393,23 @@ class Meta:
update_permission_code='change_firedepartment',
create_permission_code='change_firedepartment',
delete_permission_code='change_firedepartment')
filtering = {'department': ('exact',), 'state': ('exact',), 'id': ('exact',)}
filtering = {
'department': ('exact',),
'state': ('exact',),
'id': ('exact',),
'issuedate': ('exact', 'range'),
'warngeom': ALL,
}
list_allowed_methods = ['get', 'post']
detail_allowed_methods = ['get']
cache = SimpleCache(timeout=120)
serializer = PrettyJSONSerializer()
always_return_data = True

def build_filters(self, filters=None, **kwargs):
allow_geodjango_filters(self._meta)
return super(WeatherWarningResource, self).build_filters(filters)

def get_object_list(self, request):
return super(WeatherWarningResource, self).get_object_list(request).filter(expiredate__gte=timezone.now())

Expand Down
39 changes: 39 additions & 0 deletions firecares/utils/tastypie_geodjango.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

def allow_geodjango_filters(model_resource_meta):
# HACK: Tastypie has a bug where it doesn't recognize geodjango filters. The one exception is the "contains"
# filter - but this is just a coincidental overlap with the standard string "contains" filter. So we have to
# manually whitelist supported geodjango query terms.
model_resource_meta.queryset.query.query_terms.update({
'bbcontains',
'bboverlaps',
'contained',
'contains',
'contains_properly',
'coveredby',
'covers',
'crosses',
'disjoint',
'equals',
'intersects',
'isvalid',
'overlaps'
'relate',
'touches',
'within',
'left',
'right',
'overlaps_left',
'overlaps_right',
'overlaps_above',
'overlaps_below',
'strictly_above',
'strictly_below',

# Distance lookups are currently unsupported.
# https://django-tastypie.readthedocs.io/en/v0.12.2/geodjango.html
'distance_gt',
'distance_gte',
'distance_lt',
'distance_lte',
'dwithin',
})