okay fine

This commit is contained in:
pacnpal
2024-11-03 17:47:26 +00:00
parent 01c6004a79
commit 27eb239e97
10020 changed files with 1935769 additions and 2364 deletions

96
location/models.py Normal file
View File

@@ -0,0 +1,96 @@
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
class Location(models.Model):
"""
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",
null=True,
blank=True
)
longitude = models.DecimalField(
max_digits=9,
decimal_places=6,
validators=[
MinValueValidator(-180),
MaxValueValidator(180)
],
help_text="Longitude coordinate",
null=True,
blank=True
)
# Address components - all made nullable
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)
history = HistoricalRecords()
class Meta:
indexes = [
models.Index(fields=['content_type', 'object_id']),
models.Index(fields=['latitude', 'longitude']),
models.Index(fields=['city']),
models.Index(fields=['country']),
]
ordering = ['name']
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 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.latitude is not None and self.longitude is not None:
return (float(self.latitude), float(self.longitude))
return None