Add checks for missing user geoip coordinates and fall back to not sorting by distance
This commit is contained in:
parent
3defa51ff0
commit
9ddc993cd5
3 changed files with 47 additions and 1 deletions
|
@ -52,18 +52,24 @@ def distance(center1, center2):
|
||||||
return dkm
|
return dkm
|
||||||
|
|
||||||
def city_distance_from(ll, city):
|
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:
|
if city is not None and city.latitude is not None and city.longitude is not None:
|
||||||
return distance((ll), (city.latitude, city.longitude))
|
return distance((ll), (city.latitude, city.longitude))
|
||||||
else:
|
else:
|
||||||
return 99999
|
return 99999
|
||||||
|
|
||||||
def team_distance_from(ll, team):
|
def team_distance_from(ll, team):
|
||||||
|
if ll is None:
|
||||||
|
return 0
|
||||||
if team.city is not None:
|
if team.city is not None:
|
||||||
return city_distance_from(ll, team.city)
|
return city_distance_from(ll, team.city)
|
||||||
else:
|
else:
|
||||||
return 99999
|
return 99999
|
||||||
|
|
||||||
def event_distance_from(ll, event):
|
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:
|
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))
|
return distance((ll), (event.place.latitude, event.place.longitude))
|
||||||
if event.team is not None:
|
if event.team is not None:
|
||||||
|
@ -72,12 +78,16 @@ def event_distance_from(ll, event):
|
||||||
return 99999
|
return 99999
|
||||||
|
|
||||||
def searchable_distance_from(ll, searchable):
|
def searchable_distance_from(ll, searchable):
|
||||||
|
if ll is None:
|
||||||
|
return 0
|
||||||
if searchable.latitude is not None and searchable.longitude is not None:
|
if searchable.latitude is not None and searchable.longitude is not None:
|
||||||
return distance((ll), (float(searchable.latitude), float(searchable.longitude)))
|
return distance((ll), (float(searchable.latitude), float(searchable.longitude)))
|
||||||
else:
|
else:
|
||||||
return 99999
|
return 99999
|
||||||
|
|
||||||
def get_nearest_city(ll, max_distance=100):
|
def get_nearest_city(ll, max_distance=100):
|
||||||
|
if ll is None:
|
||||||
|
return None
|
||||||
city = None
|
city = None
|
||||||
city_distance = 1 #km
|
city_distance = 1 #km
|
||||||
while city is None and city_distance <= max_distance:
|
while city is None and city_distance <= max_distance:
|
||||||
|
|
|
@ -1,9 +1,23 @@
|
||||||
from django.test import TestCase, Client
|
from django.test import TestCase, Client
|
||||||
|
from django.shortcuts import resolve_url
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
from model_mommy import mommy
|
from model_mommy import mommy
|
||||||
|
import mock
|
||||||
|
import geocoder
|
||||||
|
import datetime
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
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.
|
# 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):
|
class EventDisplayTests(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -12,6 +26,26 @@ class EventDisplayTests(TestCase):
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
super().tearDown()
|
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):
|
def test_show_event(self):
|
||||||
event = mommy.make(Event)
|
event = mommy.make(Event)
|
||||||
event.save()
|
event.save()
|
||||||
|
|
|
@ -19,8 +19,10 @@ geocoder==1.36.0
|
||||||
icalendar==4.0.1
|
icalendar==4.0.1
|
||||||
idna==2.6
|
idna==2.6
|
||||||
Markdown==2.6.11
|
Markdown==2.6.11
|
||||||
|
mock==2.0.0
|
||||||
model-mommy==1.5.1
|
model-mommy==1.5.1
|
||||||
oauthlib==2.0.6
|
oauthlib==2.0.6
|
||||||
|
pbr==4.0.4
|
||||||
pilkit==2.0
|
pilkit==2.0
|
||||||
Pillow==5.0.0
|
Pillow==5.0.0
|
||||||
PyJWT==1.5.3
|
PyJWT==1.5.3
|
||||||
|
|
Loading…
Reference in a new issue