diff --git a/vrobbler/apps/locations/models.py b/vrobbler/apps/locations/models.py index 1aa5e15..6d08c27 100644 --- a/vrobbler/apps/locations/models.py +++ b/vrobbler/apps/locations/models.py @@ -91,31 +91,21 @@ class GeoLocation(ScrobblableMixin): abs(Decimal(old_lat_lon[1]) - Decimal(self.lon)), ) - def has_moved_for_user(self, user_id: int) -> bool: + def has_moved(self, previous_location: "GeoLocation") -> bool: has_moved = False - user = User.objects.filter(id=user_id).first() - if not user: - return False - last_point = ( - user.scrobble_set.filter(media_type="GeoLocation") - .order_by("-timestamp") - .first() - ).media_obj - - if not last_point: - return True - - loc_diff = self.loc_diff((last_point.lat, last_point.lon)) + loc_diff = self.loc_diff( + (previous_location.lat, previous_location.lon) + ) if loc_diff[0] > GEOLOC_PROXIMITY or loc_diff[1] > GEOLOC_PROXIMITY: has_moved = True logger.debug( f"[locations] checked whether location has moved against proximity setting", extra={ - "location": self, + "location_id": self.id, "loc_diff": loc_diff, "has_moved": has_moved, - "point": last_point, + "previous_location_id": previous_location.id, "geoloc_proximity": GEOLOC_PROXIMITY, }, ) diff --git a/vrobbler/apps/locations/tests/test_models.py b/vrobbler/apps/locations/tests/test_models.py index a89f63e..044e3fb 100644 --- a/vrobbler/apps/locations/tests/test_models.py +++ b/vrobbler/apps/locations/tests/test_models.py @@ -72,17 +72,10 @@ def test_has_moved(caplog): lon = -69.234 loc = GeoLocation.objects.create(lat=lat, lon=lon, altitude=60) - past1 = GeoLocation.objects.get_or_create( - lat=lat + 0.000, lon=lon - 0.000, altitude=60 + past = GeoLocation.objects.get_or_create( + lat=lat + 0.0009, lon=lon - 0.002, altitude=60 )[0] - past2 = GeoLocation.objects.get_or_create( - lat=lat + 0.002, lon=lon - 0.002, altitude=60 - )[0] - past3 = GeoLocation.objects.get_or_create( - lat=lat + 0.0001, lon=lon - 0.000, altitude=60 - )[0] - last_three = [past1, past2, past3] - assert loc.has_moved(last_three) + assert loc.has_moved(past) @pytest.mark.django_db @@ -91,17 +84,7 @@ def test_has_not_moved(): lon = -69.234 loc = GeoLocation.objects.create(lat=lat, lon=lon, altitude=60) - past1 = GeoLocation.objects.get_or_create( - lat=lat + 0.00001, lon=lon - 0.0000, altitude=60 + past = GeoLocation.objects.get_or_create( + lat=lat + 0.00009, lon=lon - 0.00009, altitude=60 )[0] - past2 = GeoLocation.objects.get_or_create( - lat=lat + 0.000, lon=lon - 0.000, altitude=60 - )[0] - past3 = GeoLocation.objects.get_or_create( - lat=lat + 0.0000, lon=lon - 0.00001, altitude=60 - )[0] - past4 = GeoLocation.objects.get_or_create( - lat=lat + 0.005, lon=lon - 0.0003, altitude=60 - )[0] - last_four = [past1, past2, past3, past4] - assert not loc.has_moved(last_four) + assert not loc.has_moved(past) diff --git a/vrobbler/apps/scrobbles/models.py b/vrobbler/apps/scrobbles/models.py index cffbbdd..42396b4 100644 --- a/vrobbler/apps/scrobbles/models.py +++ b/vrobbler/apps/scrobbles/models.py @@ -932,7 +932,7 @@ class Scrobble(TimeStampedModel): ) return scrobble - has_moved = location.has_moved_for_user(scrobble.user.id) + has_moved = location.has_moved(scrobble.media_obj) logger.info( f"[scrobbling] checking - has last location has moved?", extra={