Skip to content

Commit

Permalink
Fix Realtime API Query Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
lucernae committed Aug 8, 2018
1 parent 7b8ff0a commit 8ba2a63
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 8 deletions.
125 changes: 125 additions & 0 deletions django_project/realtime/tests/test_api_earthquake.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def setUp(self):
)
earthquake = Earthquake.objects.get(shake_id='20150619200628')
earthquake.save()
self.earthquake = earthquake
report_pdf = earthquake.shake_id + '-id.pdf'
report_png = earthquake.shake_id + '-id.png'
report_thumb = earthquake.shake_id + '-thumb-id.png'
Expand Down Expand Up @@ -191,6 +192,130 @@ def test_earthquake_list(self):
self.assertEqual(actual_results[0]['shake_id'],
expected_shake_id)

# Test basic filters
def _check_response(test_instance, query, result_count):
"""Shortcut to validate query."""
r = test_instance.client.get(
reverse('realtime:earthquake_list'),
data=query)

test_instance.assertEqual(r.status_code, status.HTTP_200_OK)
s = PageNumberPaginationSerializer(
serializer_class=EarthquakeSerializer, data=r.data)
test_instance.assertEqual(s.count, result_count)

# Depth filters
depth_query = {
'min_depth': 9
}
_check_response(self, depth_query, 1)

depth_query = {
'min_depth': 11
}
_check_response(self, depth_query, 0)

depth_query = {
'max_depth': 9
}
_check_response(self, depth_query, 0)

depth_query = {
'max_depth': 11
}
_check_response(self, depth_query, 1)

depth_query = {
'min_depth': 9,
'max_depth': 11
}
_check_response(self, depth_query, 1)

depth_query = {
'min_depth': 11,
'max_depth': 9
}
_check_response(self, depth_query, 0)

# Magnitude filters
magnitude_query = {
'min_magnitude': 4
}
_check_response(self, magnitude_query, 1)

magnitude_query = {
'min_magnitude': 5
}
_check_response(self, magnitude_query, 0)

magnitude_query = {
'max_magnitude': 5
}
_check_response(self, magnitude_query, 1)

magnitude_query = {
'max_magnitude': 4
}
_check_response(self, magnitude_query, 0)

magnitude_query = {
'min_magnitude': 4,
'max_magnitude': 5
}
_check_response(self, magnitude_query, 1)

magnitude_query = {
'min_magnitude': 5,
'max_magnitude': 4
}
_check_response(self, magnitude_query, 0)

# Time based filters
time_query = {
'min_time': '2015-06-19'
}
_check_response(self, time_query, 1)

time_query = {
'max_time': '2015-06-19'
}
_check_response(self, time_query, 0)

# manipulate time so we can filter by since_ query
previous_time = self.earthquake.time
days = 30
hours = days * 24
today = pytz.utc.fromutc(datetime.datetime.utcnow())
new_time = today - datetime.timedelta(days=30)

# temporarily change time value, so we can be sure we are filtering
# by this value
self.earthquake.time = new_time
self.earthquake.save()

time_query = {
'since_last_days': days + 1
}
_check_response(self, time_query, 1)

time_query = {
'since_last_days': days - 1
}
_check_response(self, time_query, 0)

time_query = {
'since_last_hours': hours + 1
}
_check_response(self, time_query, 1)

time_query = {
'since_last_hours': hours - 1
}
_check_response(self, time_query, 0)

self.earthquake.time = previous_time
self.earthquake.save()

def test_earthquake_detail(self):
kwargs = {
'shake_id': '20150619200628',
Expand Down
23 changes: 15 additions & 8 deletions django_project/realtime/views/earthquake.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ class EarthquakeList(mixins.ListModelMixin, mixins.CreateModelMixin,

def get(self, request, shake_id=None, source_type=None, *args, **kwargs):
try:
queryset = Earthquake.objects.all()
queryset = self.get_queryset()
if shake_id or source_type:
if shake_id:
queryset = queryset.filter(shake_id=shake_id)
if source_type:
queryset = queryset.filter(source_type=source_type)
queryset = self.filter_queryset(queryset)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
Expand Down Expand Up @@ -263,14 +264,16 @@ class EarthquakeReportList(mixins.ListModelMixin,
def get(self, request, shake_id=None, *args, **kwargs):
try:
if shake_id:
instances = EarthquakeReport.objects.filter(
queryset = self.get_queryset()
queryset = queryset.filter(
earthquake__shake_id=shake_id)
page = self.paginate_queryset(instances)
queryset = self.filter_queryset(queryset)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)

serializer = self.get_serializer(instances, many=True)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
else:
return self.list(request, *args, **kwargs)
Expand Down Expand Up @@ -334,11 +337,13 @@ def get(self, request, shake_id=None, source_type=None, language=None,
*args, **kwargs):
try:
if shake_id and source_type and language:
instance = EarthquakeReport.objects.get(
queryset = self.get_queryset()
queryset = queryset.get(
earthquake__shake_id=shake_id,
earthquake__source_type=source_type,
language=language)
serializer = self.get_serializer(instance)
queryset = self.filter_queryset(queryset)
serializer = self.get_serializer(queryset)
return Response(serializer.data)
elif shake_id:
return self.list(request, *args, **kwargs)
Expand All @@ -349,11 +354,13 @@ def get(self, request, shake_id=None, source_type=None, language=None,
# But in case it is happening, returned the last object, but still
# log the error to sentry
LOGGER.warning(e.message)
instance = EarthquakeReport.objects.filter(
queryset = self.get_queryset()
queryset = queryset.filter(
earthquake__shake_id=shake_id,
earthquake__source_type=source_type,
language=language).last()
serializer = self.get_serializer(instance)
queryset = self.filter_queryset(queryset)
serializer = self.get_serializer(queryset)
return Response(serializer.data)

def put(self, request, shake_id=None, source_type=None, language=None):
Expand Down

0 comments on commit 8ba2a63

Please sign in to comment.