mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 14:11:09 -05:00
Refactor code structure and remove redundant changes
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import requests
|
||||
from django.core.management.base import BaseCommand
|
||||
from apps.media.models import Photo
|
||||
from apps.parks.models import Park
|
||||
from apps.rides.models import Ride
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from apps.parks.models import Park, ParkPhoto
|
||||
from apps.rides.models import Ride, RidePhoto
|
||||
import json
|
||||
from django.core.files.base import ContentFile
|
||||
|
||||
@@ -18,9 +16,6 @@ class Command(BaseCommand):
|
||||
with open("parks/management/commands/seed_data.json", "r") as f:
|
||||
seed_data = json.load(f)
|
||||
|
||||
park_content_type = ContentType.objects.get_for_model(Park)
|
||||
ride_content_type = ContentType.objects.get_for_model(Ride)
|
||||
|
||||
# Process parks and their photos
|
||||
for park_data in seed_data["parks"]:
|
||||
try:
|
||||
@@ -34,15 +29,11 @@ class Command(BaseCommand):
|
||||
response = requests.get(photo_url, timeout=60)
|
||||
if response.status_code == 200:
|
||||
# Delete any existing photos for this park
|
||||
Photo.objects.filter(
|
||||
content_type=park_content_type,
|
||||
object_id=park.id,
|
||||
).delete()
|
||||
ParkPhoto.objects.filter(park=park).delete()
|
||||
|
||||
# Create new photo record
|
||||
photo = Photo(
|
||||
content_type=park_content_type,
|
||||
object_id=park.id,
|
||||
photo = ParkPhoto(
|
||||
park=park,
|
||||
is_primary=idx == 1,
|
||||
)
|
||||
|
||||
@@ -87,15 +78,11 @@ class Command(BaseCommand):
|
||||
response = requests.get(photo_url, timeout=60)
|
||||
if response.status_code == 200:
|
||||
# Delete any existing photos for this ride
|
||||
Photo.objects.filter(
|
||||
content_type=ride_content_type,
|
||||
object_id=ride.id,
|
||||
).delete()
|
||||
RidePhoto.objects.filter(ride=ride).delete()
|
||||
|
||||
# Create new photo record
|
||||
photo = Photo(
|
||||
content_type=ride_content_type,
|
||||
object_id=ride.id,
|
||||
photo = RidePhoto(
|
||||
ride=ride,
|
||||
is_primary=idx == 1,
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
from django.core.management.base import BaseCommand
|
||||
from apps.media.models import Photo
|
||||
from apps.parks.models import ParkPhoto
|
||||
from apps.rides.models import RidePhoto
|
||||
from django.db import transaction
|
||||
|
||||
|
||||
@@ -11,9 +12,11 @@ class Command(BaseCommand):
|
||||
self.stdout.write("Fixing photo paths in database...")
|
||||
|
||||
# Get all photos
|
||||
photos = Photo.objects.all()
|
||||
park_photos = ParkPhoto.objects.all()
|
||||
ride_photos = RidePhoto.objects.all()
|
||||
|
||||
for photo in photos:
|
||||
# Process park photos
|
||||
for photo in park_photos:
|
||||
try:
|
||||
with transaction.atomic():
|
||||
# Get current file path
|
||||
@@ -27,8 +30,8 @@ class Command(BaseCommand):
|
||||
parts = current_name.split("/")
|
||||
|
||||
if len(parts) >= 2:
|
||||
content_type = parts[0] # 'park' or 'ride'
|
||||
identifier = parts[1] # e.g., 'alton-towers'
|
||||
content_type = "park"
|
||||
identifier = photo.park.slug
|
||||
|
||||
# Look for files in the media directory
|
||||
media_dir = os.path.join("media", content_type, identifier)
|
||||
@@ -51,27 +54,89 @@ class Command(BaseCommand):
|
||||
photo.image.name = file_path
|
||||
photo.save()
|
||||
self.stdout.write(
|
||||
f"Updated path for photo {
|
||||
f"Updated path for park photo {
|
||||
photo.id} to {file_path}"
|
||||
)
|
||||
else:
|
||||
self.stdout.write(
|
||||
f"File not found for photo {
|
||||
f"File not found for park photo {
|
||||
photo.id}: {file_path}"
|
||||
)
|
||||
else:
|
||||
self.stdout.write(
|
||||
f"No files found in directory for photo {
|
||||
f"No files found in directory for park photo {
|
||||
photo.id}: {media_dir}"
|
||||
)
|
||||
else:
|
||||
self.stdout.write(
|
||||
f"Directory not found for photo {
|
||||
f"Directory not found for park photo {
|
||||
photo.id}: {media_dir}"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
self.stdout.write(f"Error updating photo {photo.id}: {str(e)}")
|
||||
self.stdout.write(f"Error updating park photo {photo.id}: {str(e)}")
|
||||
continue
|
||||
|
||||
# Process ride photos
|
||||
for photo in ride_photos:
|
||||
try:
|
||||
with transaction.atomic():
|
||||
# Get current file path
|
||||
current_name = photo.image.name
|
||||
|
||||
# Remove any 'media/' prefix if it exists
|
||||
if current_name.startswith("media/"):
|
||||
# Remove 'media/' prefix
|
||||
current_name = current_name[6:]
|
||||
|
||||
parts = current_name.split("/")
|
||||
|
||||
if len(parts) >= 2:
|
||||
content_type = "ride"
|
||||
identifier = photo.ride.slug
|
||||
|
||||
# Look for files in the media directory
|
||||
media_dir = os.path.join("media", content_type, identifier)
|
||||
if os.path.exists(media_dir):
|
||||
files = [
|
||||
f
|
||||
for f in os.listdir(media_dir)
|
||||
if not f.startswith(".") # Skip hidden files
|
||||
and not f.startswith("tmp") # Skip temp files
|
||||
and os.path.isfile(os.path.join(media_dir, f))
|
||||
]
|
||||
|
||||
if files:
|
||||
# Get the first file and update the database
|
||||
# record
|
||||
file_path = os.path.join(
|
||||
content_type, identifier, files[0]
|
||||
)
|
||||
if os.path.exists(os.path.join("media", file_path)):
|
||||
photo.image.name = file_path
|
||||
photo.save()
|
||||
self.stdout.write(
|
||||
f"Updated path for ride photo {
|
||||
photo.id} to {file_path}"
|
||||
)
|
||||
else:
|
||||
self.stdout.write(
|
||||
f"File not found for ride photo {
|
||||
photo.id}: {file_path}"
|
||||
)
|
||||
else:
|
||||
self.stdout.write(
|
||||
f"No files found in directory for ride photo {
|
||||
photo.id}: {media_dir}"
|
||||
)
|
||||
else:
|
||||
self.stdout.write(
|
||||
f"Directory not found for ride photo {
|
||||
photo.id}: {media_dir}"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
self.stdout.write(f"Error updating ride photo {photo.id}: {str(e)}")
|
||||
continue
|
||||
|
||||
self.stdout.write("Finished fixing photo paths")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
from django.core.management.base import BaseCommand
|
||||
from apps.media.models import Photo
|
||||
from apps.parks.models import ParkPhoto
|
||||
from apps.rides.models import RidePhoto
|
||||
from django.conf import settings
|
||||
import shutil
|
||||
|
||||
@@ -12,12 +13,93 @@ class Command(BaseCommand):
|
||||
self.stdout.write("Moving photo files to normalized locations...")
|
||||
|
||||
# Get all photos
|
||||
photos = Photo.objects.all()
|
||||
park_photos = ParkPhoto.objects.all()
|
||||
ride_photos = RidePhoto.objects.all()
|
||||
|
||||
# Track processed files to clean up later
|
||||
processed_files = set()
|
||||
|
||||
for photo in photos:
|
||||
# Process park photos
|
||||
for photo in park_photos:
|
||||
try:
|
||||
# Get current file path
|
||||
current_name = photo.image.name
|
||||
current_path = os.path.join(settings.MEDIA_ROOT, current_name)
|
||||
|
||||
# Try to find the actual file
|
||||
if not os.path.exists(current_path):
|
||||
# Check if file exists in the old location structure
|
||||
parts = current_name.split("/")
|
||||
if len(parts) >= 2:
|
||||
content_type = "park"
|
||||
identifier = photo.park.slug
|
||||
|
||||
# Look for any files in that directory
|
||||
old_dir = os.path.join(
|
||||
settings.MEDIA_ROOT, content_type, identifier
|
||||
)
|
||||
if os.path.exists(old_dir):
|
||||
files = [
|
||||
f
|
||||
for f in os.listdir(old_dir)
|
||||
if not f.startswith(".") # Skip hidden files
|
||||
and not f.startswith("tmp") # Skip temp files
|
||||
and os.path.isfile(os.path.join(old_dir, f))
|
||||
]
|
||||
if files:
|
||||
current_path = os.path.join(old_dir, files[0])
|
||||
|
||||
# Skip if file still not found
|
||||
if not os.path.exists(current_path):
|
||||
self.stdout.write(f"Skipping {current_name} - file not found")
|
||||
continue
|
||||
|
||||
# Get content type and object
|
||||
content_type_model = "park"
|
||||
obj = photo.park
|
||||
identifier = getattr(obj, "slug", obj.id)
|
||||
|
||||
# Get photo number
|
||||
photo_number = ParkPhoto.objects.filter(
|
||||
park=photo.park,
|
||||
created_at__lte=photo.created_at,
|
||||
).count()
|
||||
|
||||
# Create new filename
|
||||
_, ext = os.path.splitext(current_path)
|
||||
if not ext:
|
||||
ext = ".jpg"
|
||||
ext = ext.lower()
|
||||
new_filename = f"{identifier}_{photo_number}{ext}"
|
||||
|
||||
# Create new path
|
||||
new_relative_path = f"{content_type_model}/{identifier}/{new_filename}"
|
||||
new_full_path = os.path.join(settings.MEDIA_ROOT, new_relative_path)
|
||||
|
||||
# Create directory if it doesn't exist
|
||||
os.makedirs(os.path.dirname(new_full_path), exist_ok=True)
|
||||
|
||||
# Move the file
|
||||
if current_path != new_full_path:
|
||||
shutil.copy2(
|
||||
current_path, new_full_path
|
||||
) # Use copy2 to preserve metadata
|
||||
processed_files.add(current_path)
|
||||
else:
|
||||
processed_files.add(current_path)
|
||||
|
||||
# Update database
|
||||
photo.image.name = new_relative_path
|
||||
photo.save()
|
||||
|
||||
self.stdout.write(f"Moved {current_name} to {new_relative_path}")
|
||||
|
||||
except Exception as e:
|
||||
self.stdout.write(f"Error moving park photo {photo.id}: {str(e)}")
|
||||
continue
|
||||
|
||||
# Process ride photos
|
||||
for photo in ride_photos:
|
||||
try:
|
||||
# Get current file path
|
||||
current_name = photo.image.name
|
||||
@@ -52,14 +134,13 @@ class Command(BaseCommand):
|
||||
continue
|
||||
|
||||
# Get content type and object
|
||||
content_type_model = photo.content_type.model
|
||||
obj = photo.content_object
|
||||
content_type_model = "ride"
|
||||
obj = photo.ride
|
||||
identifier = getattr(obj, "slug", obj.id)
|
||||
|
||||
# Get photo number
|
||||
photo_number = Photo.objects.filter(
|
||||
content_type=photo.content_type,
|
||||
object_id=photo.object_id,
|
||||
photo_number = RidePhoto.objects.filter(
|
||||
ride=photo.ride,
|
||||
created_at__lte=photo.created_at,
|
||||
).count()
|
||||
|
||||
@@ -93,7 +174,7 @@ class Command(BaseCommand):
|
||||
self.stdout.write(f"Moved {current_name} to {new_relative_path}")
|
||||
|
||||
except Exception as e:
|
||||
self.stdout.write(f"Error moving photo {photo.id}: {str(e)}")
|
||||
self.stdout.write(f"Error moving ride photo {photo.id}: {str(e)}")
|
||||
continue
|
||||
|
||||
# Clean up old files
|
||||
|
||||
Reference in New Issue
Block a user