fixed a bunch of things, hopefully didn't break things

This commit is contained in:
pacnpal
2024-11-05 21:51:02 +00:00
parent 2e8a725933
commit eb5d2acab5
30 changed files with 944 additions and 569 deletions

View File

@@ -4,6 +4,7 @@ from django.utils.text import slugify
from django.contrib.contenttypes.fields import GenericRelation
from django.core.exceptions import ValidationError
from decimal import Decimal, ROUND_DOWN, InvalidOperation
from typing import Tuple, Optional, Any
from simple_history.models import HistoricalRecords
from companies.models import Company
@@ -13,6 +14,7 @@ from location.models import Location
class Park(HistoricalModel):
id: int # Type hint for Django's automatic id field
STATUS_CHOICES = [
("OPERATING", "Operating"),
("CLOSED_TEMP", "Temporarily Closed"),
@@ -57,54 +59,56 @@ class Park(HistoricalModel):
# Metadata
created_at = models.DateTimeField(auto_now_add=True, null=True)
updated_at = models.DateTimeField(auto_now=True)
history = HistoricalRecords()
class Meta:
ordering = ["name"]
def __str__(self):
def __str__(self) -> str:
return self.name
def save(self, *args, **kwargs):
def save(self, *args: Any, **kwargs: Any) -> None:
if not self.slug:
self.slug = slugify(self.name)
super().save(*args, **kwargs)
def get_absolute_url(self):
def get_absolute_url(self) -> str:
return reverse("parks:park_detail", kwargs={"slug": self.slug})
@property
def formatted_location(self):
def formatted_location(self) -> str:
if self.location.exists():
location = self.location.first()
return location.get_formatted_address()
if location:
return location.get_formatted_address()
return ""
@property
def coordinates(self):
def coordinates(self) -> Optional[Tuple[float, float]]:
"""Returns coordinates as a tuple (latitude, longitude)"""
if self.location.exists():
location = self.location.first()
return location.coordinates
if location:
return location.coordinates
return None
@classmethod
def get_by_slug(cls, slug):
def get_by_slug(cls, slug: str) -> Tuple['Park', bool]:
"""Get park by current or historical slug"""
try:
return cls.objects.get(slug=slug), False
except cls.DoesNotExist:
# Check historical slugs
history = cls.history.filter(slug=slug).order_by("-history_date").first()
history = cls.history.filter(slug=slug).order_by("-history_date").first() # type: ignore[attr-defined]
if history:
try:
return cls.objects.get(id=history.id), True
except cls.DoesNotExist:
pass
raise cls.DoesNotExist()
return cls.objects.get(pk=history.instance.pk), True
except cls.DoesNotExist as e:
raise cls.DoesNotExist("No park found with this slug") from e
raise cls.DoesNotExist("No park found with this slug")
class ParkArea(HistoricalModel):
id: int # Type hint for Django's automatic id field
park = models.ForeignKey(Park, on_delete=models.CASCADE, related_name="areas")
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255)
@@ -115,37 +119,36 @@ class ParkArea(HistoricalModel):
# Metadata
created_at = models.DateTimeField(auto_now_add=True, null=True)
updated_at = models.DateTimeField(auto_now=True)
history = HistoricalRecords()
class Meta:
ordering = ["name"]
unique_together = ["park", "slug"]
def __str__(self):
def __str__(self) -> str:
return f"{self.name} at {self.park.name}"
def save(self, *args, **kwargs):
def save(self, *args: Any, **kwargs: Any) -> None:
if not self.slug:
self.slug = slugify(self.name)
super().save(*args, **kwargs)
def get_absolute_url(self):
def get_absolute_url(self) -> str:
return reverse(
"parks:area_detail",
kwargs={"park_slug": self.park.slug, "area_slug": self.slug},
)
@classmethod
def get_by_slug(cls, slug):
def get_by_slug(cls, slug: str) -> Tuple['ParkArea', bool]:
"""Get area by current or historical slug"""
try:
return cls.objects.get(slug=slug), False
except cls.DoesNotExist:
# Check historical slugs
history = cls.history.filter(slug=slug).order_by("-history_date").first()
history = cls.history.filter(slug=slug).order_by("-history_date").first() # type: ignore[attr-defined]
if history:
try:
return cls.objects.get(id=history.id), True
except cls.DoesNotExist:
pass
raise cls.DoesNotExist()
return cls.objects.get(pk=history.instance.pk), True
except cls.DoesNotExist as e:
raise cls.DoesNotExist("No park area found with this slug") from e
raise cls.DoesNotExist("No park area found with this slug")