mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-21 13:31:09 -05:00
fixed a bunch of things, hopefully didn't break things
This commit is contained in:
Binary file not shown.
@@ -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")
|
||||
|
||||
122
parks/tests.py
122
parks/tests.py
@@ -4,15 +4,32 @@ from django.contrib.auth import get_user_model
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.contrib.gis.geos import Point
|
||||
from django.http import HttpResponse
|
||||
from typing import cast, Optional, Tuple
|
||||
from .models import Park, ParkArea
|
||||
from companies.models import Company
|
||||
from location.models import Location
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
def create_test_location(park: Park) -> Location:
|
||||
"""Helper function to create a test location"""
|
||||
return Location.objects.create(
|
||||
content_type=ContentType.objects.get_for_model(Park),
|
||||
object_id=park.id,
|
||||
name='Test Park Location',
|
||||
location_type='park',
|
||||
street_address='123 Test St',
|
||||
city='Test City',
|
||||
state='TS',
|
||||
country='Test Country',
|
||||
postal_code='12345',
|
||||
point=Point(-118.2437, 34.0522)
|
||||
)
|
||||
|
||||
class ParkModelTests(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
def setUpTestData(cls) -> None:
|
||||
# Create test user
|
||||
cls.user = User.objects.create_user(
|
||||
username='testuser',
|
||||
@@ -35,20 +52,9 @@ class ParkModelTests(TestCase):
|
||||
)
|
||||
|
||||
# Create test location
|
||||
cls.location = Location.objects.create(
|
||||
content_type=ContentType.objects.get_for_model(Park),
|
||||
object_id=cls.park.id,
|
||||
name='Test Park Location',
|
||||
location_type='park',
|
||||
street_address='123 Test St',
|
||||
city='Test City',
|
||||
state='TS',
|
||||
country='Test Country',
|
||||
postal_code='12345',
|
||||
point=Point(-118.2437, 34.0522) # Los Angeles coordinates
|
||||
)
|
||||
cls.location = create_test_location(cls.park)
|
||||
|
||||
def test_park_creation(self):
|
||||
def test_park_creation(self) -> None:
|
||||
"""Test park instance creation and field values"""
|
||||
self.assertEqual(self.park.name, 'Test Park')
|
||||
self.assertEqual(self.park.owner, self.company)
|
||||
@@ -56,34 +62,35 @@ class ParkModelTests(TestCase):
|
||||
self.assertEqual(self.park.website, 'http://testpark.com')
|
||||
self.assertTrue(self.park.slug)
|
||||
|
||||
def test_park_str_representation(self):
|
||||
def test_park_str_representation(self) -> None:
|
||||
"""Test string representation of park"""
|
||||
self.assertEqual(str(self.park), 'Test Park')
|
||||
|
||||
def test_park_location(self):
|
||||
def test_park_location(self) -> None:
|
||||
"""Test park location relationship"""
|
||||
self.assertTrue(self.park.location.exists())
|
||||
location = self.park.location.first()
|
||||
self.assertEqual(location.street_address, '123 Test St')
|
||||
self.assertEqual(location.city, 'Test City')
|
||||
self.assertEqual(location.state, 'TS')
|
||||
self.assertEqual(location.country, 'Test Country')
|
||||
self.assertEqual(location.postal_code, '12345')
|
||||
if location := self.park.location.first():
|
||||
self.assertEqual(location.street_address, '123 Test St')
|
||||
self.assertEqual(location.city, 'Test City')
|
||||
self.assertEqual(location.state, 'TS')
|
||||
self.assertEqual(location.country, 'Test Country')
|
||||
self.assertEqual(location.postal_code, '12345')
|
||||
|
||||
def test_park_coordinates(self):
|
||||
def test_park_coordinates(self) -> None:
|
||||
"""Test park coordinates property"""
|
||||
coords = self.park.coordinates
|
||||
self.assertIsNotNone(coords)
|
||||
self.assertAlmostEqual(coords[0], 34.0522, places=4) # latitude
|
||||
self.assertAlmostEqual(coords[1], -118.2437, places=4) # longitude
|
||||
if coords:
|
||||
self.assertAlmostEqual(coords[0], 34.0522, places=4) # latitude
|
||||
self.assertAlmostEqual(coords[1], -118.2437, places=4) # longitude
|
||||
|
||||
def test_park_formatted_location(self):
|
||||
def test_park_formatted_location(self) -> None:
|
||||
"""Test park formatted_location property"""
|
||||
expected = '123 Test St, Test City, TS, 12345, Test Country'
|
||||
self.assertEqual(self.park.formatted_location, expected)
|
||||
|
||||
class ParkAreaTests(TestCase):
|
||||
def setUp(self):
|
||||
def setUp(self) -> None:
|
||||
# Create test company
|
||||
self.company = Company.objects.create(
|
||||
name='Test Company',
|
||||
@@ -98,18 +105,7 @@ class ParkAreaTests(TestCase):
|
||||
)
|
||||
|
||||
# Create test location
|
||||
self.location = Location.objects.create(
|
||||
content_type=ContentType.objects.get_for_model(Park),
|
||||
object_id=self.park.id,
|
||||
name='Test Park Location',
|
||||
location_type='park',
|
||||
street_address='123 Test St', # Added street_address
|
||||
city='Test City',
|
||||
state='TS',
|
||||
country='Test Country',
|
||||
postal_code='12345',
|
||||
point=Point(-118.2437, 34.0522)
|
||||
)
|
||||
self.location = create_test_location(self.park)
|
||||
|
||||
# Create test area
|
||||
self.area = ParkArea.objects.create(
|
||||
@@ -118,25 +114,25 @@ class ParkAreaTests(TestCase):
|
||||
description='Test Description'
|
||||
)
|
||||
|
||||
def test_area_creation(self):
|
||||
def test_area_creation(self) -> None:
|
||||
"""Test park area creation"""
|
||||
self.assertEqual(self.area.name, 'Test Area')
|
||||
self.assertEqual(self.area.park, self.park)
|
||||
self.assertTrue(self.area.slug)
|
||||
|
||||
def test_area_str_representation(self):
|
||||
def test_area_str_representation(self) -> None:
|
||||
"""Test string representation of park area"""
|
||||
expected = f'Test Area at {self.park.name}'
|
||||
self.assertEqual(str(self.area), expected)
|
||||
|
||||
def test_area_get_by_slug(self):
|
||||
def test_area_get_by_slug(self) -> None:
|
||||
"""Test get_by_slug class method"""
|
||||
area, is_historical = ParkArea.get_by_slug(self.area.slug)
|
||||
self.assertEqual(area, self.area)
|
||||
self.assertFalse(is_historical)
|
||||
|
||||
class ParkViewTests(TestCase):
|
||||
def setUp(self):
|
||||
def setUp(self) -> None:
|
||||
self.client = Client()
|
||||
self.user = User.objects.create_user(
|
||||
username='testuser',
|
||||
@@ -152,43 +148,35 @@ class ParkViewTests(TestCase):
|
||||
owner=self.company,
|
||||
status='OPERATING'
|
||||
)
|
||||
self.location = Location.objects.create(
|
||||
content_type=ContentType.objects.get_for_model(Park),
|
||||
object_id=self.park.id,
|
||||
name='Test Park Location',
|
||||
location_type='park',
|
||||
street_address='123 Test St', # Added street_address
|
||||
city='Test City',
|
||||
state='TS',
|
||||
country='Test Country',
|
||||
postal_code='12345',
|
||||
point=Point(-118.2437, 34.0522)
|
||||
)
|
||||
self.location = create_test_location(self.park)
|
||||
|
||||
def test_park_list_view(self):
|
||||
def test_park_list_view(self) -> None:
|
||||
"""Test park list view"""
|
||||
response = self.client.get(reverse('parks:park_list'))
|
||||
response = cast(HttpResponse, self.client.get(reverse('parks:park_list')))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, self.park.name)
|
||||
content = response.content.decode('utf-8')
|
||||
self.assertIn(self.park.name, content)
|
||||
|
||||
def test_park_detail_view(self):
|
||||
def test_park_detail_view(self) -> None:
|
||||
"""Test park detail view"""
|
||||
response = self.client.get(
|
||||
response = cast(HttpResponse, self.client.get(
|
||||
reverse('parks:park_detail', kwargs={'slug': self.park.slug})
|
||||
)
|
||||
))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, self.park.name)
|
||||
self.assertContains(response, '123 Test St')
|
||||
content = response.content.decode('utf-8')
|
||||
self.assertIn(self.park.name, content)
|
||||
self.assertIn('123 Test St', content)
|
||||
|
||||
def test_park_area_detail_view(self):
|
||||
def test_park_area_detail_view(self) -> None:
|
||||
"""Test park area detail view"""
|
||||
area = ParkArea.objects.create(
|
||||
park=self.park,
|
||||
name='Test Area'
|
||||
)
|
||||
response = self.client.get(
|
||||
response = cast(HttpResponse, self.client.get(
|
||||
reverse('parks:area_detail',
|
||||
kwargs={'park_slug': self.park.slug, 'area_slug': area.slug})
|
||||
)
|
||||
))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, area.name)
|
||||
content = response.content.decode('utf-8')
|
||||
self.assertIn(area.name, content)
|
||||
|
||||
Reference in New Issue
Block a user