From 9ddc993cd52cf05f9e4999a7924fa61413da0360 Mon Sep 17 00:00:00 2001 From: Michael Hall Date: Wed, 13 Jun 2018 13:01:48 -0400 Subject: [PATCH] Add checks for missing user geoip coordinates and fall back to not sorting by distance --- events/location.py | 10 ++++++++++ get_together/tests/events.py | 36 +++++++++++++++++++++++++++++++++++- requirements.txt | 2 ++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/events/location.py b/events/location.py index ab84ea8..919694d 100644 --- a/events/location.py +++ b/events/location.py @@ -52,18 +52,24 @@ def distance(center1, center2): return dkm def city_distance_from(ll, city): + if ll is None: + return 0 if city is not None and city.latitude is not None and city.longitude is not None: return distance((ll), (city.latitude, city.longitude)) else: return 99999 def team_distance_from(ll, team): + if ll is None: + return 0 if team.city is not None: return city_distance_from(ll, team.city) else: return 99999 def event_distance_from(ll, event): + if ll is None: + return 0 if event.place is not None and event.place.latitude is not None and event.place.longitude is not None: return distance((ll), (event.place.latitude, event.place.longitude)) if event.team is not None: @@ -72,12 +78,16 @@ def event_distance_from(ll, event): return 99999 def searchable_distance_from(ll, searchable): + if ll is None: + return 0 if searchable.latitude is not None and searchable.longitude is not None: return distance((ll), (float(searchable.latitude), float(searchable.longitude))) else: return 99999 def get_nearest_city(ll, max_distance=100): + if ll is None: + return None city = None city_distance = 1 #km while city is None and city_distance <= max_distance: diff --git a/get_together/tests/events.py b/get_together/tests/events.py index 3a14913..89577ec 100644 --- a/get_together/tests/events.py +++ b/get_together/tests/events.py @@ -1,9 +1,23 @@ from django.test import TestCase, Client +from django.shortcuts import resolve_url +from django.utils import timezone + from model_mommy import mommy +import mock +import geocoder +import datetime from django.contrib.auth.models import User -from events.models import Event, Attendee, UserProfile +from events.models import Event, Place, Attendee, UserProfile # Create your tests here. + +def mock_get_geoip(latlng=(0.0, 0.0)): + def get_geoip(request): + g = geocoder.ip('8.8.8.8') + g.latlng = latlng + return g + return get_geoip + class EventDisplayTests(TestCase): def setUp(self): @@ -12,6 +26,26 @@ class EventDisplayTests(TestCase): def tearDown(self): super().tearDown() + @mock.patch("events.location.get_geoip", mock_get_geoip((0.0, 0.0))) + def test_events_list(self): + place = mommy.make(Place, name="Test Place", latitude=0.0, longitude=0.0) + event = mommy.make(Event, name="Test Event", place=place, start_time=timezone.now() + datetime.timedelta(days=1), end_time=timezone.now() + datetime.timedelta(days=2)) + event.save() + + c = Client() + response = c.get(resolve_url('all-events')) + assert(response.status_code == 200) + + @mock.patch("events.location.get_geoip", mock_get_geoip(None)) + def test_events_list_no_geoip(self): + place = mommy.make(Place, name="Test Place", latitude=0.0, longitude=0.0) + event = mommy.make(Event, name="Test Event", place=place, start_time=timezone.now() + datetime.timedelta(days=1), end_time=timezone.now() + datetime.timedelta(days=2)) + event.save() + + c = Client() + response = c.get(resolve_url('all-events')) + assert(response.status_code == 200) + def test_show_event(self): event = mommy.make(Event) event.save() diff --git a/requirements.txt b/requirements.txt index 57c35d7..9af1f18 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,8 +19,10 @@ geocoder==1.36.0 icalendar==4.0.1 idna==2.6 Markdown==2.6.11 +mock==2.0.0 model-mommy==1.5.1 oauthlib==2.0.6 +pbr==4.0.4 pilkit==2.0 Pillow==5.0.0 PyJWT==1.5.3