feat: Implement comprehensive image storage and processing system

- Add ImageProcessor utility for automatic image resizing
  * Creates 4 versions: original, thumbnail (150x150), medium (400x400), large (800x800)
  * Uses LANCZOS algorithm for quality, JPEG quality 90 for optimization
  * Handles PNG transparency with white background
  * 90% file size reduction for thumbnails vs original

- Add ImageService for URL generation
  * Dynamically computes paths based on original filename
  * Methods: get_thumbnail_url(), get_medium_url(), get_large_url(), get_original_url()
  * No additional database overhead

- Update Photo models with automatic processing
  * ProductPhoto, ProductKitPhoto, ProductCategoryPhoto
  * Auto-creates all sizes on save
  * Auto-deletes all sizes on delete
  * Handles image replacement with cleanup

- Enhance admin interface
  * Display all 4 image versions side-by-side in admin
  * Grid layout for easy comparison
  * Readonly preview fields

- Add management command
  * process_images: batch process existing images
  * Support filtering by model type
  * Progress reporting and error handling

- Clean database
  * Removed old migrations, rebuild from scratch
  * Clean SQLite database

- Add comprehensive documentation
  * IMAGE_STORAGE_STRATEGY.md: full system architecture
  * QUICK_START_IMAGES.md: quick reference guide
  * IMAGE_SYSTEM_EXAMPLES.md: code examples for templates/views/API

Performance metrics:
  * Original: 6.1K
  * Medium: 2.9K (52% smaller)
  * Large: 5.6K (8% smaller)
  * Thumbnail: 438B (93% smaller)

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-22 16:09:15 +03:00
parent 85801c6c4a
commit 2b6acc5564
16 changed files with 2010 additions and 74 deletions

View File

@@ -0,0 +1,87 @@
"""
Management команда для обработки существующих изображений товаров, комплектов и категорий.
Создает все необходимые размеры (thumbnail, medium, large) для уже загруженных изображений.
Использование:
python manage.py process_images
python manage.py process_images --model ProductPhoto
python manage.py process_images --model ProductKitPhoto
python manage.py process_images --model ProductCategoryPhoto
"""
from django.core.management.base import BaseCommand
from products.models import ProductPhoto, ProductKitPhoto, ProductCategoryPhoto
from products.utils.image_processor import ImageProcessor
class Command(BaseCommand):
help = "Обработка существующих изображений и создание всех необходимых размеров"
def add_arguments(self, parser):
parser.add_argument(
'--model',
type=str,
default=None,
help='Какую модель обрабатывать (ProductPhoto, ProductKitPhoto, ProductCategoryPhoto)',
)
def handle(self, *args, **options):
model_name = options.get('model')
models_to_process = []
if not model_name:
# Обрабатываем все модели
models_to_process = [
('ProductPhoto', ProductPhoto, 'products'),
('ProductKitPhoto', ProductKitPhoto, 'kits'),
('ProductCategoryPhoto', ProductCategoryPhoto, 'categories'),
]
else:
# Обрабатываем конкретную модель
if model_name == 'ProductPhoto':
models_to_process = [('ProductPhoto', ProductPhoto, 'products')]
elif model_name == 'ProductKitPhoto':
models_to_process = [('ProductKitPhoto', ProductKitPhoto, 'kits')]
elif model_name == 'ProductCategoryPhoto':
models_to_process = [('ProductCategoryPhoto', ProductCategoryPhoto, 'categories')]
else:
self.stdout.write(
self.style.ERROR(f'Неизвестная модель: {model_name}')
)
return
total_processed = 0
total_errors = 0
for model_display_name, model_class, base_path in models_to_process:
self.stdout.write(f'\nОбработка {model_display_name}...')
self.stdout.write('-' * 50)
photos = model_class.objects.filter(image__isnull=False).exclude(image='')
if not photos.exists():
self.stdout.write(self.style.WARNING(f'Нет изображений для обработки в {model_display_name}'))
continue
count = photos.count()
self.stdout.write(f'Найдено изображений: {count}')
for i, photo in enumerate(photos, 1):
try:
# Сохраняем фото - это вызовет обработку в методе save()
photo.save()
total_processed += 1
self.stdout.write(
self.style.SUCCESS(f'✓ [{i}/{count}] {photo} - OK')
)
except Exception as e:
total_errors += 1
self.stdout.write(
self.style.ERROR(f'✗ [{i}/{count}] {photo} - ОШИБКА: {str(e)}')
)
self.stdout.write('\n' + '=' * 50)
self.stdout.write(self.style.SUCCESS(f'Обработано: {total_processed}'))
if total_errors:
self.stdout.write(self.style.ERROR(f'Ошибок: {total_errors}'))
self.stdout.write('=' * 50)