Add checks for missing user geoip coordinates and fall back to not sorting by distance

This commit is contained in:
Michael Hall 2018-06-13 13:01:48 -04:00
parent 3defa51ff0
commit 9ddc993cd5
3 changed files with 47 additions and 1 deletions

View file

@ -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:

View file

@ -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()

View file

@ -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