mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 05:11:09 -05:00
initial geodjango implementation
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
from django.contrib.gis.db import models as gis_models
|
||||
from django.db import models
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.validators import MinValueValidator, MaxValueValidator
|
||||
from simple_history.models import HistoricalRecords
|
||||
from django.contrib.gis.geos import Point
|
||||
|
||||
class Location(models.Model):
|
||||
"""
|
||||
@@ -27,7 +29,7 @@ class Location(models.Model):
|
||||
MinValueValidator(-90),
|
||||
MaxValueValidator(90)
|
||||
],
|
||||
help_text="Latitude coordinate",
|
||||
help_text="Latitude coordinate (legacy field)",
|
||||
null=True,
|
||||
blank=True
|
||||
)
|
||||
@@ -38,12 +40,20 @@ class Location(models.Model):
|
||||
MinValueValidator(-180),
|
||||
MaxValueValidator(180)
|
||||
],
|
||||
help_text="Longitude coordinate",
|
||||
help_text="Longitude coordinate (legacy field)",
|
||||
null=True,
|
||||
blank=True
|
||||
)
|
||||
|
||||
# Address components - all made nullable
|
||||
# GeoDjango point field
|
||||
point = gis_models.PointField(
|
||||
srid=4326, # WGS84 coordinate system
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="Geographic coordinates as a Point"
|
||||
)
|
||||
|
||||
# Address components
|
||||
street_address = models.CharField(max_length=255, blank=True, null=True)
|
||||
city = models.CharField(max_length=100, blank=True, null=True)
|
||||
state = models.CharField(max_length=100, blank=True, null=True, help_text="State/Region/Province")
|
||||
@@ -58,7 +68,6 @@ class Location(models.Model):
|
||||
class Meta:
|
||||
indexes = [
|
||||
models.Index(fields=['content_type', 'object_id']),
|
||||
models.Index(fields=['latitude', 'longitude']),
|
||||
models.Index(fields=['city']),
|
||||
models.Index(fields=['country']),
|
||||
]
|
||||
@@ -73,6 +82,15 @@ class Location(models.Model):
|
||||
location_str = ", ".join(location_parts) if location_parts else "Unknown location"
|
||||
return f"{self.name} ({location_str})"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
# Sync point field with lat/lon fields for backward compatibility
|
||||
if self.latitude is not None and self.longitude is not None and not self.point:
|
||||
self.point = Point(float(self.longitude), float(self.latitude))
|
||||
elif self.point and (self.latitude is None or self.longitude is None):
|
||||
self.longitude = self.point.x
|
||||
self.latitude = self.point.y
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def get_formatted_address(self):
|
||||
"""Returns a formatted address string"""
|
||||
components = []
|
||||
@@ -91,6 +109,29 @@ class Location(models.Model):
|
||||
@property
|
||||
def coordinates(self):
|
||||
"""Returns coordinates as a tuple"""
|
||||
if self.latitude is not None and self.longitude is not None:
|
||||
if self.point:
|
||||
return (self.point.y, self.point.x) # Returns (latitude, longitude)
|
||||
elif self.latitude is not None and self.longitude is not None:
|
||||
return (float(self.latitude), float(self.longitude))
|
||||
return None
|
||||
|
||||
def distance_to(self, other_location):
|
||||
"""
|
||||
Calculate the distance to another location in meters.
|
||||
Returns None if either location is missing coordinates.
|
||||
"""
|
||||
if not self.point or not other_location.point:
|
||||
return None
|
||||
return self.point.distance(other_location.point) * 100000 # Convert to meters
|
||||
|
||||
def nearby_locations(self, distance_km=10):
|
||||
"""
|
||||
Find locations within specified distance in kilometers.
|
||||
Returns a queryset of nearby Location objects.
|
||||
"""
|
||||
if not self.point:
|
||||
return Location.objects.none()
|
||||
|
||||
return Location.objects.filter(
|
||||
point__distance_lte=(self.point, distance_km * 1000) # Convert km to meters
|
||||
).exclude(pk=self.pk)
|
||||
|
||||
Reference in New Issue
Block a user