feat: Implement avatar upload system with Cloudflare integration

- Added migration to transition avatar data from CloudflareImageField to ForeignKey structure in UserProfile.
- Fixed UserProfileEvent avatar field to align with new avatar structure.
- Created serializers for social authentication, including connected and available providers.
- Developed request logging middleware for comprehensive request/response logging.
- Updated moderation and parks migrations to remove outdated triggers and adjust foreign key relationships.
- Enhanced rides migrations to ensure proper handling of image uploads and triggers.
- Introduced a test script for the 3-step avatar upload process, ensuring functionality with Cloudflare.
- Documented the fix for avatar upload issues, detailing root cause, implementation, and verification steps.
- Implemented automatic deletion of Cloudflare images upon avatar, park, and ride photo changes or removals.
This commit is contained in:
pacnpal
2025-08-30 21:20:25 -04:00
parent fb6726f89a
commit 9bed782784
75 changed files with 4571 additions and 1962 deletions

View File

@@ -362,8 +362,10 @@ class RideModelPhoto(TrackedModel):
ride_model = models.ForeignKey(
RideModel, on_delete=models.CASCADE, related_name="photos"
)
image = models.ImageField(
upload_to="ride_models/photos/", help_text="Photo of the ride model"
image = models.ForeignKey(
'django_cloudflareimages_toolkit.CloudflareImage',
on_delete=models.CASCADE,
help_text="Photo of the ride model stored on Cloudflare Images"
)
caption = models.CharField(max_length=500, blank=True)
alt_text = models.CharField(max_length=255, blank=True)
@@ -624,9 +626,30 @@ class Ride(TrackedModel):
return f"{self.name} at {self.park.name}"
def save(self, *args, **kwargs) -> None:
# Handle slug generation and conflicts
if not self.slug:
self.slug = slugify(self.name)
# Check for slug conflicts when park changes or slug is new
original_ride = None
if self.pk:
try:
original_ride = Ride.objects.get(pk=self.pk)
except Ride.DoesNotExist:
pass
# If park changed or this is a new ride, ensure slug uniqueness within the park
park_changed = original_ride and original_ride.park_id != self.park_id
if not self.pk or park_changed:
self._ensure_unique_slug_in_park()
# Handle park area validation when park changes
if park_changed and self.park_area:
# Check if park_area belongs to the new park
if self.park_area.park_id != self.park_id:
# Clear park_area if it doesn't belong to the new park
self.park_area = None
# Generate frontend URLs
if self.park:
frontend_domain = getattr(
@@ -637,6 +660,73 @@ class Ride(TrackedModel):
super().save(*args, **kwargs)
def _ensure_unique_slug_in_park(self) -> None:
"""Ensure the ride's slug is unique within its park."""
base_slug = slugify(self.name)
self.slug = base_slug
counter = 1
while (
Ride.objects.filter(park=self.park, slug=self.slug)
.exclude(pk=self.pk)
.exists()
):
self.slug = f"{base_slug}-{counter}"
counter += 1
def move_to_park(self, new_park, clear_park_area=True):
"""
Move this ride to a different park with proper handling of related data.
Args:
new_park: The new Park instance to move the ride to
clear_park_area: Whether to clear park_area (default True, since areas are park-specific)
Returns:
dict: Summary of changes made
"""
from django.apps import apps
old_park = self.park
old_url = self.url
old_park_area = self.park_area
# Update park
self.park = new_park
# Handle park area
if clear_park_area:
self.park_area = None
# Save will handle slug conflicts and URL updates
self.save()
# Return summary of changes
changes = {
'old_park': {
'id': old_park.id,
'name': old_park.name,
'slug': old_park.slug
},
'new_park': {
'id': new_park.id,
'name': new_park.name,
'slug': new_park.slug
},
'url_changed': old_url != self.url,
'old_url': old_url,
'new_url': self.url,
'park_area_cleared': clear_park_area and old_park_area is not None,
'old_park_area': {
'id': old_park_area.id,
'name': old_park_area.name
} if old_park_area else None,
'slug_changed': self.slug != slugify(self.name),
'final_slug': self.slug
}
return changes
@pghistory.track()
class RollerCoasterStats(models.Model):