mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 02:11:08 -05:00
- Add complete backend/ directory with full Django application - Add frontend/ directory with Vite + TypeScript setup ready for Next.js - Add comprehensive shared/ directory with: - Complete documentation and memory-bank archives - Media files and avatars (letters, park/ride images) - Deployment scripts and automation tools - Shared types and utilities - Add architecture/ directory with migration guides - Configure pnpm workspace for monorepo development - Update .gitignore to exclude .django_tailwind_cli/ build artifacts - Preserve all historical documentation in shared/docs/memory-bank/ - Set up proper structure for full-stack development with shared resources
176 lines
6.4 KiB
Python
176 lines
6.4 KiB
Python
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 django.contrib.gis.geos import Point
|
|
import pghistory
|
|
from apps.core.history import TrackedModel
|
|
|
|
|
|
@pghistory.track()
|
|
class Location(TrackedModel):
|
|
"""
|
|
A generic location model that can be associated with any model
|
|
using GenericForeignKey. Stores detailed location information
|
|
including coordinates and address components.
|
|
"""
|
|
|
|
# Generic relation fields
|
|
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
|
|
object_id = models.PositiveIntegerField()
|
|
content_object = GenericForeignKey("content_type", "object_id")
|
|
|
|
# Location name and type
|
|
name = models.CharField(
|
|
max_length=255,
|
|
help_text="Name of the location (e.g. business name, landmark)",
|
|
)
|
|
location_type = models.CharField(
|
|
max_length=50,
|
|
help_text="Type of location (e.g. business, landmark, address)",
|
|
)
|
|
|
|
# Geographic coordinates
|
|
latitude = models.DecimalField(
|
|
max_digits=9,
|
|
decimal_places=6,
|
|
validators=[MinValueValidator(-90), MaxValueValidator(90)],
|
|
help_text="Latitude coordinate (legacy field)",
|
|
null=True,
|
|
blank=True,
|
|
)
|
|
longitude = models.DecimalField(
|
|
max_digits=9,
|
|
decimal_places=6,
|
|
validators=[MinValueValidator(-180), MaxValueValidator(180)],
|
|
help_text="Longitude coordinate (legacy field)",
|
|
null=True,
|
|
blank=True,
|
|
)
|
|
|
|
# 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",
|
|
)
|
|
country = models.CharField(max_length=100, blank=True, null=True)
|
|
postal_code = models.CharField(max_length=20, blank=True, null=True)
|
|
|
|
# Metadata
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
class Meta:
|
|
indexes = [
|
|
models.Index(fields=["content_type", "object_id"]),
|
|
models.Index(fields=["city"]),
|
|
models.Index(fields=["country"]),
|
|
]
|
|
ordering = ["name"]
|
|
constraints = [
|
|
# Business rule: Latitude must be within valid range (-90 to 90)
|
|
models.CheckConstraint(
|
|
name="location_latitude_range",
|
|
check=models.Q(latitude__isnull=True)
|
|
| (models.Q(latitude__gte=-90) & models.Q(latitude__lte=90)),
|
|
violation_error_message="Latitude must be between -90 and 90 degrees",
|
|
),
|
|
# Business rule: Longitude must be within valid range (-180 to 180)
|
|
models.CheckConstraint(
|
|
name="location_longitude_range",
|
|
check=models.Q(longitude__isnull=True)
|
|
| (models.Q(longitude__gte=-180) & models.Q(longitude__lte=180)),
|
|
violation_error_message="Longitude must be between -180 and 180 degrees",
|
|
),
|
|
# Business rule: If coordinates are provided, both lat and lng must
|
|
# be present
|
|
models.CheckConstraint(
|
|
name="location_coordinates_complete",
|
|
check=models.Q(latitude__isnull=True, longitude__isnull=True)
|
|
| models.Q(latitude__isnull=False, longitude__isnull=False),
|
|
violation_error_message="Both latitude and longitude must be provided together",
|
|
),
|
|
]
|
|
|
|
def __str__(self):
|
|
location_parts = []
|
|
if self.city:
|
|
location_parts.append(self.city)
|
|
if self.country:
|
|
location_parts.append(self.country)
|
|
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 = []
|
|
if self.street_address:
|
|
components.append(self.street_address)
|
|
if self.city:
|
|
components.append(self.city)
|
|
if self.state:
|
|
components.append(self.state)
|
|
if self.postal_code:
|
|
components.append(self.postal_code)
|
|
if self.country:
|
|
components.append(self.country)
|
|
return ", ".join(components) if components else ""
|
|
|
|
@property
|
|
def coordinates(self):
|
|
"""Returns coordinates as a tuple"""
|
|
if self.point:
|
|
# Returns (latitude, longitude)
|
|
return (self.point.y, self.point.x)
|
|
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)
|