فهرست منبع

Add truncated lat lon to model

Colin Powell 1 سال پیش
والد
کامیت
7a6bd169bf

+ 27 - 0
vrobbler/apps/locations/migrations/0005_alter_rawgeolocation_options_and_more.py

@@ -0,0 +1,27 @@
+# Generated by Django 4.1.7 on 2023-11-24 18:17
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("locations", "0004_alter_rawgeolocation_options"),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name="rawgeolocation",
+            options={"get_latest_by": "modified"},
+        ),
+        migrations.AddField(
+            model_name="geolocation",
+            name="truncated_lat",
+            field=models.FloatField(blank=True, null=True),
+        ),
+        migrations.AddField(
+            model_name="geolocation",
+            name="truncated_lon",
+            field=models.FloatField(blank=True, null=True),
+        ),
+    ]

+ 12 - 10
vrobbler/apps/locations/models.py

@@ -20,11 +20,19 @@ class GeoLocation(ScrobblableMixin):
     uuid = models.UUIDField(default=uuid4, editable=False, **BNULL)
     lat = models.FloatField()
     lon = models.FloatField()
+    truncated_lat = models.FloatField(**BNULL)
+    truncated_lon = models.FloatField(**BNULL)
     altitude = models.FloatField(**BNULL)
 
     class Meta:
         unique_together = [["lat", "lon", "altitude"]]
 
+    def save(self, *args, **kwargs):
+        if not self.truncated_lat or not self.truncated_lon:
+            self.truncated_lat = float(str(self.lat)[:-3])
+            self.truncated_lon = float(str(self.lon)[:-3])
+        super(GeoLocation, self).save(*args, **kwargs)
+
     def __str__(self):
         if self.title:
             return self.title
@@ -36,14 +44,6 @@ class GeoLocation(ScrobblableMixin):
             "locations:geo_location_detail", kwargs={"slug": self.uuid}
         )
 
-    @property
-    def truncated_lat(self):
-        return float(str(self.lat)[:-3])
-
-    @property
-    def truncated_lan(self):
-        return float(str(self.lon)[:-3])
-
     @classmethod
     def find_or_create(cls, data_dict: Dict) -> "GeoLocation":
         """Given a data dict from GPSLogger, does the heavy lifting of looking up
@@ -60,8 +60,8 @@ class GeoLocation(ScrobblableMixin):
         data_dict["altitude"] = float(data_dict.get("alt", ""))
 
         location = cls.objects.filter(
-            lat=data_dict.get("lat"),
-            lon=data_dict.get("lon"),
+            truncated_lat=float(str(data_dict.get("lat", ""))[:-3]),
+            truncated_lon=float(str(data_dict.get("lon", ""))[:-3]),
             altitude=data_dict.get("alt"),
         ).first()
 
@@ -69,6 +69,8 @@ class GeoLocation(ScrobblableMixin):
             location = cls.objects.create(
                 lat=data_dict.get("lat"),
                 lon=data_dict.get("lon"),
+                truncated_lat=float(str(data_dict.get("lat", ""))[:-3]),
+                truncated_lon=float(str(data_dict.get("lon", ""))[:-3]),
                 altitude=data_dict.get("alt"),
             )
         return location