diff --git a/resources/scripts/migrate_firestore.py b/resources/scripts/migrate_firestore.py index 0586440..d283995 100644 --- a/resources/scripts/migrate_firestore.py +++ b/resources/scripts/migrate_firestore.py @@ -36,8 +36,14 @@ def download_image(url): def migrate_sections(): print("--- Sections migratsiyasi boshlandi ---") + existing_ids = set(SectionModel.objects.exclude(firestore_id__isnull=True).values_list('firestore_id', flat=True)) + docs = db.collection('sections').stream() for doc in docs: + if doc.id in existing_ids: + # print(f"Section {doc.id} allaqachon mavjud, o'tkazib yuborildi.") + continue + data = doc.to_dict() section, created = SectionModel.objects.get_or_create( firestore_id=doc.id, @@ -53,12 +59,17 @@ def migrate_sections(): img_file = download_image(section.image_url) if img_file: section.image.save(f"section_{doc.id}.png", img_file, save=True) - print(f"Section {section.name} {'yaratildi' if created else 'mavjud'}") + print(f"Section {section.name} yaratildi") def migrate_categories(): print("\n--- Categories migratsiyasi boshlandi ---") + existing_ids = set(CategoryModel.objects.exclude(firestore_id__isnull=True).values_list('firestore_id', flat=True)) + docs = db.collection('vendor_categories').stream() for doc in docs: + if doc.id in existing_ids: + continue + data = doc.to_dict() section_id = data.get('section_id') section = SectionModel.objects.filter(firestore_id=section_id).first() @@ -78,12 +89,17 @@ def migrate_categories(): img_file = download_image(category.photo_url) if img_file: category.photo.save(f"cat_{doc.id}.png", img_file, save=True) - print(f"Category {category.title} {'yaratildi' if created else 'mavjud'}") + print(f"Category {category.title} yaratildi") def migrate_vendors(): print("\n--- Vendors migratsiyasi boshlandi ---") + existing_ids = set(VendorModel.objects.exclude(firestore_id__isnull=True).values_list('firestore_id', flat=True)) + docs = db.collection('vendors').stream() for doc in docs: + if doc.id in existing_ids: + continue + data = doc.to_dict() section_id = data.get('section_id') section = SectionModel.objects.filter(firestore_id=section_id).first() @@ -107,13 +123,15 @@ def migrate_vendors(): img_file = download_image(vendor.photo_url) if img_file: vendor.photo.save(f"vendor_{doc.id}.png", img_file, save=True) - print(f"Vendor {vendor.title} {'yaratildi' if created else 'mavjud'}") + print(f"Vendor {vendor.title} yaratildi") def migrate_products(): print("\n--- Products migratsiyasi boshlandi ---") + existing_ids = set(VendorproductModel.objects.exclude(firestore_id__isnull=True).values_list('firestore_id', flat=True)) batch_size = 100 last_doc = None + new_count = 0 while True: query = db.collection('vendor_products').limit(batch_size) @@ -125,19 +143,22 @@ def migrate_products(): break for doc in docs: + if doc.id in existing_ids: + continue + data = doc.to_dict() vendor_id = data.get('vendorID') category_id = data.get('categoryID') section_id = data.get('section_id') - vendor = VendorModel.objects.filter(firestore_id=vendor_id).first() + vendor_obj = VendorModel.objects.filter(firestore_id=vendor_id).first() category = CategoryModel.objects.filter(firestore_id=category_id).first() section = SectionModel.objects.filter(firestore_id=section_id).first() product, created = VendorproductModel.objects.get_or_create( firestore_id=doc.id, defaults={ - 'vendor': vendor, + 'vendor': vendor_id, # 'vendor' field is CharField in model 'category': category, 'section': section, 'name': data.get('name', 'Nomsiz Product'), @@ -149,15 +170,17 @@ def migrate_products(): 'photos_json': data.get('photos', []), } ) - if created and product.photos_json: - main_img_url = product.photos_json[0] - img_file = download_image(main_img_url) - if img_file: - product.image.save(f"prod_{doc.id}.png", img_file, save=True) - print(f"Product {product.name} {'yaratildi' if created else 'mavjud'}") + if created: + new_count += 1 + if product.photos_json: + main_img_url = product.photos_json[0] + img_file = download_image(main_img_url) + if img_file: + product.image.save(f"prod_{doc.id}.png", img_file, save=True) + print(f"Product {product.name} yaratildi") last_doc = docs[-1] - print(f"--- {batch_size} ta mahsulot qayta ishlandi ---") + print(f"--- {batch_size} ta mahsulot tekshirildi (Hozirgacha yangi: {new_count}) ---") if __name__ == "__main__": migrate_sections()