Add truncated lat lon to model
This commit is contained in:
@ -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),
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -20,11 +20,19 @@ class GeoLocation(ScrobblableMixin):
|
|||||||
uuid = models.UUIDField(default=uuid4, editable=False, **BNULL)
|
uuid = models.UUIDField(default=uuid4, editable=False, **BNULL)
|
||||||
lat = models.FloatField()
|
lat = models.FloatField()
|
||||||
lon = models.FloatField()
|
lon = models.FloatField()
|
||||||
|
truncated_lat = models.FloatField(**BNULL)
|
||||||
|
truncated_lon = models.FloatField(**BNULL)
|
||||||
altitude = models.FloatField(**BNULL)
|
altitude = models.FloatField(**BNULL)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
unique_together = [["lat", "lon", "altitude"]]
|
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):
|
def __str__(self):
|
||||||
if self.title:
|
if self.title:
|
||||||
return self.title
|
return self.title
|
||||||
@ -36,14 +44,6 @@ class GeoLocation(ScrobblableMixin):
|
|||||||
"locations:geo_location_detail", kwargs={"slug": self.uuid}
|
"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
|
@classmethod
|
||||||
def find_or_create(cls, data_dict: Dict) -> "GeoLocation":
|
def find_or_create(cls, data_dict: Dict) -> "GeoLocation":
|
||||||
"""Given a data dict from GPSLogger, does the heavy lifting of looking up
|
"""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", ""))
|
data_dict["altitude"] = float(data_dict.get("alt", ""))
|
||||||
|
|
||||||
location = cls.objects.filter(
|
location = cls.objects.filter(
|
||||||
lat=data_dict.get("lat"),
|
truncated_lat=float(str(data_dict.get("lat", ""))[:-3]),
|
||||||
lon=data_dict.get("lon"),
|
truncated_lon=float(str(data_dict.get("lon", ""))[:-3]),
|
||||||
altitude=data_dict.get("alt"),
|
altitude=data_dict.get("alt"),
|
||||||
).first()
|
).first()
|
||||||
|
|
||||||
@ -69,6 +69,8 @@ class GeoLocation(ScrobblableMixin):
|
|||||||
location = cls.objects.create(
|
location = cls.objects.create(
|
||||||
lat=data_dict.get("lat"),
|
lat=data_dict.get("lat"),
|
||||||
lon=data_dict.get("lon"),
|
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"),
|
altitude=data_dict.get("alt"),
|
||||||
)
|
)
|
||||||
return location
|
return location
|
||||||
|
|||||||
Reference in New Issue
Block a user