mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 14:51:08 -05:00
fix commas
This commit is contained in:
103
parks/models.py
103
parks/models.py
@@ -16,12 +16,14 @@ def normalize_coordinate(value, max_digits, decimal_places):
|
||||
try:
|
||||
if value is None:
|
||||
return None
|
||||
|
||||
|
||||
# Convert to Decimal for precise handling
|
||||
value = Decimal(str(value))
|
||||
# Round to specified decimal places
|
||||
value = Decimal(value.quantize(Decimal('0.' + '0' * decimal_places), rounding=ROUND_DOWN))
|
||||
|
||||
value = Decimal(
|
||||
value.quantize(Decimal("0." + "0" * decimal_places), rounding=ROUND_DOWN)
|
||||
)
|
||||
|
||||
return value
|
||||
except (TypeError, ValueError, InvalidOperation):
|
||||
return None
|
||||
@@ -34,10 +36,10 @@ def validate_coordinate_digits(value, max_digits, decimal_places):
|
||||
# Convert to Decimal for precise handling
|
||||
value = Decimal(str(value))
|
||||
# Round to exactly 6 decimal places
|
||||
value = value.quantize(Decimal('0.000001'), rounding=ROUND_DOWN)
|
||||
value = value.quantize(Decimal("0.000001"), rounding=ROUND_DOWN)
|
||||
return value
|
||||
except (InvalidOperation, TypeError):
|
||||
raise ValidationError('Invalid coordinate value.')
|
||||
raise ValidationError("Invalid coordinate value.")
|
||||
return value
|
||||
|
||||
|
||||
@@ -53,21 +55,19 @@ def validate_longitude_digits(value):
|
||||
|
||||
class Park(models.Model):
|
||||
STATUS_CHOICES = [
|
||||
('OPERATING', 'Operating'),
|
||||
('CLOSED_TEMP', 'Temporarily Closed'),
|
||||
('CLOSED_PERM', 'Permanently Closed'),
|
||||
('UNDER_CONSTRUCTION', 'Under Construction'),
|
||||
('DEMOLISHED', 'Demolished'),
|
||||
('RELOCATED', 'Relocated'),
|
||||
("OPERATING", "Operating"),
|
||||
("CLOSED_TEMP", "Temporarily Closed"),
|
||||
("CLOSED_PERM", "Permanently Closed"),
|
||||
("UNDER_CONSTRUCTION", "Under Construction"),
|
||||
("DEMOLISHED", "Demolished"),
|
||||
("RELOCATED", "Relocated"),
|
||||
]
|
||||
|
||||
name = models.CharField(max_length=255)
|
||||
slug = models.SlugField(max_length=255, unique=True)
|
||||
description = models.TextField(blank=True)
|
||||
status = models.CharField(
|
||||
max_length=20,
|
||||
choices=STATUS_CHOICES,
|
||||
default='OPERATING'
|
||||
max_length=20, choices=STATUS_CHOICES, default="OPERATING"
|
||||
)
|
||||
|
||||
# Location fields
|
||||
@@ -76,24 +76,24 @@ class Park(models.Model):
|
||||
decimal_places=6,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text='Latitude coordinate (-90 to 90)',
|
||||
help_text="Latitude coordinate (-90 to 90)",
|
||||
validators=[
|
||||
MinValueValidator(Decimal('-90')),
|
||||
MaxValueValidator(Decimal('90')),
|
||||
MinValueValidator(Decimal("-90")),
|
||||
MaxValueValidator(Decimal("90")),
|
||||
validate_latitude_digits,
|
||||
]
|
||||
],
|
||||
)
|
||||
longitude = models.DecimalField(
|
||||
max_digits=10,
|
||||
decimal_places=6,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text='Longitude coordinate (-180 to 180)',
|
||||
help_text="Longitude coordinate (-180 to 180)",
|
||||
validators=[
|
||||
MinValueValidator(Decimal('-180')),
|
||||
MaxValueValidator(Decimal('180')),
|
||||
MinValueValidator(Decimal("-180")),
|
||||
MaxValueValidator(Decimal("180")),
|
||||
validate_longitude_digits,
|
||||
]
|
||||
],
|
||||
)
|
||||
street_address = models.CharField(max_length=255, blank=True)
|
||||
city = models.CharField(max_length=255, blank=True)
|
||||
@@ -106,32 +106,22 @@ class Park(models.Model):
|
||||
closing_date = models.DateField(null=True, blank=True)
|
||||
operating_season = models.CharField(max_length=255, blank=True)
|
||||
size_acres = models.DecimalField(
|
||||
max_digits=10,
|
||||
decimal_places=2,
|
||||
null=True,
|
||||
blank=True
|
||||
max_digits=10, decimal_places=2, null=True, blank=True
|
||||
)
|
||||
website = models.URLField(blank=True)
|
||||
|
||||
# Statistics
|
||||
average_rating = models.DecimalField(
|
||||
max_digits=3,
|
||||
decimal_places=2,
|
||||
null=True,
|
||||
blank=True
|
||||
max_digits=3, decimal_places=2, null=True, blank=True
|
||||
)
|
||||
total_rides = models.IntegerField(null=True, blank=True)
|
||||
total_roller_coasters = models.IntegerField(null=True, blank=True)
|
||||
|
||||
# Relationships
|
||||
owner = models.ForeignKey(
|
||||
Company,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='parks'
|
||||
Company, on_delete=models.SET_NULL, null=True, blank=True, related_name="parks"
|
||||
)
|
||||
photos = GenericRelation(Photo, related_query_name='park')
|
||||
photos = GenericRelation(Photo, related_query_name="park")
|
||||
|
||||
# Metadata
|
||||
created_at = models.DateTimeField(auto_now_add=True, null=True)
|
||||
@@ -139,7 +129,7 @@ class Park(models.Model):
|
||||
history = HistoricalRecords()
|
||||
|
||||
class Meta:
|
||||
ordering = ['name']
|
||||
ordering = ["name"]
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
@@ -147,17 +137,28 @@ class Park(models.Model):
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.slug:
|
||||
self.slug = slugify(self.name)
|
||||
|
||||
|
||||
# Normalize coordinates before saving
|
||||
if self.latitude is not None:
|
||||
self.latitude = normalize_coordinate(self.latitude, 9, 6)
|
||||
if self.longitude is not None:
|
||||
self.longitude = normalize_coordinate(self.longitude, 10, 6)
|
||||
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse('parks:park_detail', kwargs={'slug': self.slug})
|
||||
return reverse("parks:park_detail", kwargs={"slug": self.slug})
|
||||
|
||||
@property
|
||||
def formatted_location(self):
|
||||
parts = []
|
||||
if self.city:
|
||||
parts.append(self.city)
|
||||
if self.state:
|
||||
parts.append(self.state)
|
||||
if self.country:
|
||||
parts.append(self.country)
|
||||
return ", ".join(parts)
|
||||
|
||||
@classmethod
|
||||
def get_by_slug(cls, slug):
|
||||
@@ -166,7 +167,7 @@ class Park(models.Model):
|
||||
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()
|
||||
if history:
|
||||
try:
|
||||
return cls.objects.get(id=history.id), True
|
||||
@@ -176,11 +177,7 @@ class Park(models.Model):
|
||||
|
||||
|
||||
class ParkArea(models.Model):
|
||||
park = models.ForeignKey(
|
||||
Park,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='areas'
|
||||
)
|
||||
park = models.ForeignKey(Park, on_delete=models.CASCADE, related_name="areas")
|
||||
name = models.CharField(max_length=255)
|
||||
slug = models.SlugField(max_length=255)
|
||||
description = models.TextField(blank=True)
|
||||
@@ -193,8 +190,8 @@ class ParkArea(models.Model):
|
||||
history = HistoricalRecords()
|
||||
|
||||
class Meta:
|
||||
ordering = ['name']
|
||||
unique_together = ['park', 'slug']
|
||||
ordering = ["name"]
|
||||
unique_together = ["park", "slug"]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.name} at {self.park.name}"
|
||||
@@ -205,10 +202,10 @@ class ParkArea(models.Model):
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse('parks:area_detail', kwargs={
|
||||
'park_slug': self.park.slug,
|
||||
'area_slug': self.slug
|
||||
})
|
||||
return reverse(
|
||||
"parks:area_detail",
|
||||
kwargs={"park_slug": self.park.slug, "area_slug": self.slug},
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_by_slug(cls, slug):
|
||||
@@ -217,7 +214,7 @@ class ParkArea(models.Model):
|
||||
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()
|
||||
if history:
|
||||
try:
|
||||
return cls.objects.get(id=history.id), True
|
||||
|
||||
Reference in New Issue
Block a user