Refactor comments app to use mixins for comment functionality; update admin interfaces and add historical model fixes

This commit is contained in:
pacnpal
2025-02-08 16:33:55 -05:00
parent f000c492e8
commit 181f49a0f2
21 changed files with 548 additions and 280 deletions

43
location/mixins.py Normal file
View File

@@ -0,0 +1,43 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models import QuerySet
from typing import Optional, Tuple
class LocationMixin:
"""Mixin for models that can have location data attached."""
def get_location(self) -> Optional['Location']:
"""Get location for this instance."""
from location.models import Location
ct = ContentType.objects.get_for_model(self.__class__)
return Location.objects.filter(content_type=ct, object_id=self.pk).first()
def set_location(self, address: str, latitude: float, longitude: float) -> 'Location':
"""Set or update location for this instance."""
from location.models import Location
ct = ContentType.objects.get_for_model(self.__class__)
location, created = Location.objects.update_or_create(
content_type=ct,
object_id=self.pk,
defaults={
'address': address,
'latitude': latitude,
'longitude': longitude
}
)
return location
@property
def coordinates(self) -> Optional[Tuple[float, float]]:
"""Get coordinates (latitude, longitude) if available."""
location = self.get_location()
if location:
return location.latitude, location.longitude
return None
@property
def formatted_location(self) -> str:
"""Get formatted address string if available."""
location = self.get_location()
if location:
return location.get_formatted_address()
return ""