diff --git a/myproject/products/templates/products/productkit_create.html b/myproject/products/templates/products/productkit_create.html index c66c1ad..ba3f4ad 100644 --- a/myproject/products/templates/products/productkit_create.html +++ b/myproject/products/templates/products/productkit_create.html @@ -1373,12 +1373,38 @@ reader.readAsDataURL(file); }); } else { - photoPreviewContainer.style.display = 'none'; + photoPreviewContainer.style.display = 'none'; // Only hide if no source photos too (will check later) photoPreview.innerHTML = ''; + + // Re-render source photos if they exist and we just cleared new files + if (document.querySelectorAll('.source-photo-item').length > 0) { + photoPreviewContainer.style.display = 'block'; + } } }); } + // Render source photos if present + {% if source_photos %} + photoPreviewContainer.style.display = 'block'; + {% for photo in source_photos %} + (function () { + const col = document.createElement('div'); + col.className = 'col-4 col-md-3 col-lg-2 source-photo-item'; + col.innerHTML = ` +
+ Source Photo + + +
+ `; + photoPreview.appendChild(col); + })(); + {% endfor %} + {% endif %} + window.removePhoto = function (index) { selectedFiles.splice(index, 1); const dataTransfer = new DataTransfer(); diff --git a/myproject/products/views/productkit_views.py b/myproject/products/views/productkit_views.py index 4dfcbf6..6a5acfa 100644 --- a/myproject/products/views/productkit_views.py +++ b/myproject/products/views/productkit_views.py @@ -12,6 +12,7 @@ from user_roles.mixins import ManagerOwnerRequiredMixin from ..models import ProductKit, ProductCategory, ProductTag, ProductKitPhoto, Product, ProductVariantGroup, BouquetName, ProductSalesUnit from ..forms import ProductKitForm, KitItemFormSetCreate, KitItemFormSetUpdate from .utils import handle_photos +import os class ProductKitListView(LoginRequiredMixin, ManagerOwnerRequiredMixin, ListView): @@ -296,6 +297,17 @@ class ProductKitCreateView(LoginRequiredMixin, ManagerOwnerRequiredMixin, Create context['selected_products'] = selected_products context['selected_variants'] = selected_variants context['selected_sales_units'] = selected_sales_units + + # Pass source photos if copying + if copy_id: + try: + source_kit = ProductKit.objects.prefetch_related('photos').get(pk=copy_id) + photos = source_kit.photos.all().order_by('order') + print(f"DEBUG: Found {photos.count()} source photos for kit {copy_id}") + context['source_photos'] = photos + except ProductKit.DoesNotExist: + print(f"DEBUG: Source kit {copy_id} not found") + pass # Количество названий букетов в базе context['bouquet_names_count'] = BouquetName.objects.count() @@ -335,6 +347,48 @@ class ProductKitCreateView(LoginRequiredMixin, ManagerOwnerRequiredMixin, Create # Обработка фотографий handle_photos(self.request, self.object, ProductKitPhoto, 'kit') + # Handle copied photos + copied_photo_ids = self.request.POST.getlist('copied_photos') + print(f"DEBUG: copied_photo_ids in POST: {copied_photo_ids}") + + if copied_photo_ids: + from django.core.files.base import ContentFile + original_photos = ProductKitPhoto.objects.filter(id__in=copied_photo_ids) + print(f"DEBUG: Found {original_photos.count()} original photos to copy") + + # Get max order from existing photos (uploaded via handle_photos) + from django.db.models import Max + max_order = self.object.photos.aggregate(Max('order'))['order__max'] + next_order = 0 if max_order is None else max_order + 1 + print(f"DEBUG: Starting order for copies: {next_order}") + + for photo in original_photos: + try: + # Open the original image file + if photo.image: + print(f"DEBUG: Processing photo {photo.id}: {photo.image.name}") + with photo.image.open('rb') as f: + image_content = f.read() + + # Create a new ContentFile + new_image_name = f"copy_{self.object.id}_{os.path.basename(photo.image.name)}" + print(f"DEBUG: New image name: {new_image_name}") + + # Create new photo instance + new_photo = ProductKitPhoto(kit=self.object, order=next_order) + # Save the image file (this also saves the model instance) + new_photo.image.save(new_image_name, ContentFile(image_content)) + print(f"DEBUG: Successfully saved copy for photo {photo.id}") + + next_order += 1 + else: + print(f"DEBUG: Photo {photo.id} has no image file") + except Exception as e: + print(f"Error copying photo {photo.id}: {e}") + import traceback + traceback.print_exc() + continue + messages.success( self.request, f'Комплект "{self.object.name}" успешно создан!'