From 605435b9ea1e8759baa8b4971b79fae6cabf3bd7 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Sun, 18 Feb 2024 11:39:37 -0500 Subject: [PATCH] [locations] Make finding proximity locations easier --- vrobbler/apps/locations/models.py | 7 ++++--- vrobbler/apps/scrobbles/models.py | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/vrobbler/apps/locations/models.py b/vrobbler/apps/locations/models.py index 1ff7a2d..f1de065 100644 --- a/vrobbler/apps/locations/models.py +++ b/vrobbler/apps/locations/models.py @@ -107,18 +107,19 @@ class GeoLocation(ScrobblableMixin): return has_moved - def named_in_proximity(self): + def named_in_proximity(self, named=True) -> models.QuerySet: lat_min = Decimal(self.lat) - GEOLOC_PROXIMITY lat_max = Decimal(self.lat) + GEOLOC_PROXIMITY lon_min = Decimal(self.lon) - GEOLOC_PROXIMITY lon_max = Decimal(self.lon) + GEOLOC_PROXIMITY + is_title_null = not named return GeoLocation.objects.filter( - title__isnull=False, + title__isnull=is_title_null, lat__lte=lat_max, lat__gte=lat_min, lon__lte=lon_max, lon__gte=lon_min, - ).first() + ) class RawGeoLocation(TimeStampedModel): diff --git a/vrobbler/apps/scrobbles/models.py b/vrobbler/apps/scrobbles/models.py index e0070ce..dc06718 100644 --- a/vrobbler/apps/scrobbles/models.py +++ b/vrobbler/apps/scrobbles/models.py @@ -838,7 +838,8 @@ class Scrobble(TimeStampedModel): ) scrobble.stop(force_finish=True) - if existing_location := location.named_in_proximity(): + if existing_locations := location.named_in_proximity(): + existing_location = existing_locations.first() logger.info( f"[scrobbling] moved to {location.id} but named location {existing_location.id} found, using it instead" )