migrate firbase yangilandi
This commit is contained in:
@@ -36,8 +36,14 @@ def download_image(url):
|
|||||||
|
|
||||||
def migrate_sections():
|
def migrate_sections():
|
||||||
print("--- Sections migratsiyasi boshlandi ---")
|
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()
|
docs = db.collection('sections').stream()
|
||||||
for doc in docs:
|
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()
|
data = doc.to_dict()
|
||||||
section, created = SectionModel.objects.get_or_create(
|
section, created = SectionModel.objects.get_or_create(
|
||||||
firestore_id=doc.id,
|
firestore_id=doc.id,
|
||||||
@@ -53,12 +59,17 @@ def migrate_sections():
|
|||||||
img_file = download_image(section.image_url)
|
img_file = download_image(section.image_url)
|
||||||
if img_file:
|
if img_file:
|
||||||
section.image.save(f"section_{doc.id}.png", img_file, save=True)
|
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():
|
def migrate_categories():
|
||||||
print("\n--- Categories migratsiyasi boshlandi ---")
|
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()
|
docs = db.collection('vendor_categories').stream()
|
||||||
for doc in docs:
|
for doc in docs:
|
||||||
|
if doc.id in existing_ids:
|
||||||
|
continue
|
||||||
|
|
||||||
data = doc.to_dict()
|
data = doc.to_dict()
|
||||||
section_id = data.get('section_id')
|
section_id = data.get('section_id')
|
||||||
section = SectionModel.objects.filter(firestore_id=section_id).first()
|
section = SectionModel.objects.filter(firestore_id=section_id).first()
|
||||||
@@ -78,12 +89,17 @@ def migrate_categories():
|
|||||||
img_file = download_image(category.photo_url)
|
img_file = download_image(category.photo_url)
|
||||||
if img_file:
|
if img_file:
|
||||||
category.photo.save(f"cat_{doc.id}.png", img_file, save=True)
|
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():
|
def migrate_vendors():
|
||||||
print("\n--- Vendors migratsiyasi boshlandi ---")
|
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()
|
docs = db.collection('vendors').stream()
|
||||||
for doc in docs:
|
for doc in docs:
|
||||||
|
if doc.id in existing_ids:
|
||||||
|
continue
|
||||||
|
|
||||||
data = doc.to_dict()
|
data = doc.to_dict()
|
||||||
section_id = data.get('section_id')
|
section_id = data.get('section_id')
|
||||||
section = SectionModel.objects.filter(firestore_id=section_id).first()
|
section = SectionModel.objects.filter(firestore_id=section_id).first()
|
||||||
@@ -107,13 +123,15 @@ def migrate_vendors():
|
|||||||
img_file = download_image(vendor.photo_url)
|
img_file = download_image(vendor.photo_url)
|
||||||
if img_file:
|
if img_file:
|
||||||
vendor.photo.save(f"vendor_{doc.id}.png", img_file, save=True)
|
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():
|
def migrate_products():
|
||||||
print("\n--- Products migratsiyasi boshlandi ---")
|
print("\n--- Products migratsiyasi boshlandi ---")
|
||||||
|
existing_ids = set(VendorproductModel.objects.exclude(firestore_id__isnull=True).values_list('firestore_id', flat=True))
|
||||||
|
|
||||||
batch_size = 100
|
batch_size = 100
|
||||||
last_doc = None
|
last_doc = None
|
||||||
|
new_count = 0
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
query = db.collection('vendor_products').limit(batch_size)
|
query = db.collection('vendor_products').limit(batch_size)
|
||||||
@@ -125,19 +143,22 @@ def migrate_products():
|
|||||||
break
|
break
|
||||||
|
|
||||||
for doc in docs:
|
for doc in docs:
|
||||||
|
if doc.id in existing_ids:
|
||||||
|
continue
|
||||||
|
|
||||||
data = doc.to_dict()
|
data = doc.to_dict()
|
||||||
vendor_id = data.get('vendorID')
|
vendor_id = data.get('vendorID')
|
||||||
category_id = data.get('categoryID')
|
category_id = data.get('categoryID')
|
||||||
section_id = data.get('section_id')
|
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()
|
category = CategoryModel.objects.filter(firestore_id=category_id).first()
|
||||||
section = SectionModel.objects.filter(firestore_id=section_id).first()
|
section = SectionModel.objects.filter(firestore_id=section_id).first()
|
||||||
|
|
||||||
product, created = VendorproductModel.objects.get_or_create(
|
product, created = VendorproductModel.objects.get_or_create(
|
||||||
firestore_id=doc.id,
|
firestore_id=doc.id,
|
||||||
defaults={
|
defaults={
|
||||||
'vendor': vendor,
|
'vendor': vendor_id, # 'vendor' field is CharField in model
|
||||||
'category': category,
|
'category': category,
|
||||||
'section': section,
|
'section': section,
|
||||||
'name': data.get('name', 'Nomsiz Product'),
|
'name': data.get('name', 'Nomsiz Product'),
|
||||||
@@ -149,15 +170,17 @@ def migrate_products():
|
|||||||
'photos_json': data.get('photos', []),
|
'photos_json': data.get('photos', []),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
if created and product.photos_json:
|
if created:
|
||||||
main_img_url = product.photos_json[0]
|
new_count += 1
|
||||||
img_file = download_image(main_img_url)
|
if product.photos_json:
|
||||||
if img_file:
|
main_img_url = product.photos_json[0]
|
||||||
product.image.save(f"prod_{doc.id}.png", img_file, save=True)
|
img_file = download_image(main_img_url)
|
||||||
print(f"Product {product.name} {'yaratildi' if created else 'mavjud'}")
|
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]
|
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__":
|
if __name__ == "__main__":
|
||||||
migrate_sections()
|
migrate_sections()
|
||||||
|
|||||||
Reference in New Issue
Block a user