mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-21 09:11:08 -05:00
Refactor test utilities and enhance ASGI settings
- Cleaned up and standardized assertions in ApiTestMixin for API response validation. - Updated ASGI settings to use os.environ for setting the DJANGO_SETTINGS_MODULE. - Removed unused imports and improved formatting in settings.py. - Refactored URL patterns in urls.py for better readability and organization. - Enhanced view functions in views.py for consistency and clarity. - Added .flake8 configuration for linting and style enforcement. - Introduced type stubs for django-environ to improve type checking with Pylance.
This commit is contained in:
@@ -1,58 +1,77 @@
|
||||
import os
|
||||
from django.core.management.base import BaseCommand
|
||||
from media.models import Photo
|
||||
from django.conf import settings
|
||||
from django.db import transaction
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Fix photo paths in database to match actual file locations'
|
||||
help = "Fix photo paths in database to match actual file locations"
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
self.stdout.write('Fixing photo paths in database...')
|
||||
|
||||
self.stdout.write("Fixing photo paths in database...")
|
||||
|
||||
# Get all photos
|
||||
photos = Photo.objects.all()
|
||||
|
||||
|
||||
for photo in 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/'):
|
||||
current_name = current_name[6:] # Remove 'media/' prefix
|
||||
|
||||
parts = current_name.split('/')
|
||||
|
||||
if current_name.startswith("media/"):
|
||||
# Remove 'media/' prefix
|
||||
current_name = current_name[6:]
|
||||
|
||||
parts = current_name.split("/")
|
||||
|
||||
if len(parts) >= 2:
|
||||
content_type = parts[0] # 'park' or 'ride'
|
||||
identifier = parts[1] # e.g., 'alton-towers'
|
||||
|
||||
identifier = parts[1] # e.g., 'alton-towers'
|
||||
|
||||
# Look for files in the media directory
|
||||
media_dir = os.path.join('media', content_type, identifier)
|
||||
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('.') and # Skip hidden files
|
||||
not f.startswith('tmp') and # Skip temp files
|
||||
os.path.isfile(os.path.join(media_dir, f))]
|
||||
|
||||
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)):
|
||||
# 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 photo {photo.id} to {file_path}')
|
||||
self.stdout.write(
|
||||
f"Updated path for photo {
|
||||
photo.id} to {file_path}"
|
||||
)
|
||||
else:
|
||||
self.stdout.write(f'File not found for photo {photo.id}: {file_path}')
|
||||
self.stdout.write(
|
||||
f"File not found for photo {
|
||||
photo.id}: {file_path}"
|
||||
)
|
||||
else:
|
||||
self.stdout.write(f'No files found in directory for photo {photo.id}: {media_dir}')
|
||||
self.stdout.write(
|
||||
f"No files found in directory for photo {
|
||||
photo.id}: {media_dir}"
|
||||
)
|
||||
else:
|
||||
self.stdout.write(f'Directory not found for photo {photo.id}: {media_dir}')
|
||||
|
||||
self.stdout.write(
|
||||
f"Directory not found for 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 photo {photo.id}: {str(e)}")
|
||||
continue
|
||||
|
||||
self.stdout.write('Finished fixing photo paths')
|
||||
|
||||
self.stdout.write("Finished fixing photo paths")
|
||||
|
||||
Reference in New Issue
Block a user