storage yaratildi
This commit is contained in:
167
resources/scripts/migrate_firestore.py
Normal file
167
resources/scripts/migrate_firestore.py
Normal file
@@ -0,0 +1,167 @@
|
||||
import os
|
||||
import sys
|
||||
import django
|
||||
import requests
|
||||
from io import BytesIO
|
||||
from django.core.files.base import ContentFile
|
||||
import firebase_admin
|
||||
from firebase_admin import credentials, firestore
|
||||
|
||||
# 1. Django muhitini yuklash
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
|
||||
django.setup()
|
||||
|
||||
from core.apps.vendors.models import SectionModel, CategoryModel, VendorModel, VendorproductModel
|
||||
|
||||
# 2. Firebase ulanish
|
||||
cert_path = "fondexuzb-firebase-adminsdk-fbsvc-7b0e2d6200.json"
|
||||
if not firebase_admin._apps:
|
||||
cred = credentials.Certificate(cert_path)
|
||||
firebase_admin.initialize_app(cred)
|
||||
|
||||
db = firestore.client()
|
||||
|
||||
def download_image(url):
|
||||
"""Rasmni URL orqali yuklab olish"""
|
||||
if not url or not url.startswith('http'):
|
||||
return None
|
||||
try:
|
||||
response = requests.get(url, timeout=10)
|
||||
if response.status_code == 200:
|
||||
return ContentFile(response.content)
|
||||
except Exception as e:
|
||||
print(f"Rasm yuklashda xato: {url} -> {e}")
|
||||
return None
|
||||
|
||||
def migrate_sections():
|
||||
print("--- Sections migratsiyasi boshlandi ---")
|
||||
docs = db.collection('sections').stream()
|
||||
for doc in docs:
|
||||
data = doc.to_dict()
|
||||
section, created = SectionModel.objects.get_or_create(
|
||||
firestore_id=doc.id,
|
||||
defaults={
|
||||
'name': data.get('name', 'Nomsiz Section'),
|
||||
'is_active': data.get('isActive', True),
|
||||
'image_url': data.get('sectionImage', ''),
|
||||
'color': data.get('color', ''),
|
||||
'service_type': data.get('serviceType', ''),
|
||||
}
|
||||
)
|
||||
if created and section.image_url:
|
||||
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'}")
|
||||
|
||||
def migrate_categories():
|
||||
print("\n--- Categories migratsiyasi boshlandi ---")
|
||||
docs = db.collection('vendor_categories').stream()
|
||||
for doc in docs:
|
||||
data = doc.to_dict()
|
||||
section_id = data.get('section_id')
|
||||
section = SectionModel.objects.filter(firestore_id=section_id).first()
|
||||
|
||||
category, created = CategoryModel.objects.get_or_create(
|
||||
firestore_id=doc.id,
|
||||
defaults={
|
||||
'section': section,
|
||||
'title': data.get('title', 'Nomsiz Kategoriya'),
|
||||
'description': data.get('description', ''),
|
||||
'photo_url': data.get('photo', ''),
|
||||
'is_publish': data.get('publish', True),
|
||||
'order': data.get('order', 0),
|
||||
}
|
||||
)
|
||||
if created and category.photo_url:
|
||||
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'}")
|
||||
|
||||
def migrate_vendors():
|
||||
print("\n--- Vendors migratsiyasi boshlandi ---")
|
||||
docs = db.collection('vendors').stream()
|
||||
for doc in docs:
|
||||
data = doc.to_dict()
|
||||
section_id = data.get('section_id')
|
||||
section = SectionModel.objects.filter(firestore_id=section_id).first()
|
||||
|
||||
vendor, created = VendorModel.objects.get_or_create(
|
||||
firestore_id=doc.id,
|
||||
defaults={
|
||||
'section': section,
|
||||
'title': data.get('title', 'Nomsiz Vendor'),
|
||||
'description': data.get('description', ''),
|
||||
'phone': data.get('phonenumber', ''),
|
||||
'location': {
|
||||
'lat': data.get('latitude', 0),
|
||||
'lng': data.get('longitude', 0),
|
||||
},
|
||||
'photo_url': data.get('photo', ''),
|
||||
'is_active': data.get('enabled_listing_can_stay', True),
|
||||
}
|
||||
)
|
||||
if created and vendor.photo_url:
|
||||
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'}")
|
||||
|
||||
def migrate_products():
|
||||
print("\n--- Products migratsiyasi boshlandi ---")
|
||||
|
||||
batch_size = 100
|
||||
last_doc = None
|
||||
|
||||
while True:
|
||||
query = db.collection('vendor_products').limit(batch_size)
|
||||
if last_doc:
|
||||
query = query.start_after(last_doc)
|
||||
|
||||
docs = list(query.stream())
|
||||
if not docs:
|
||||
break
|
||||
|
||||
for doc in docs:
|
||||
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()
|
||||
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,
|
||||
'category': category,
|
||||
'section': section,
|
||||
'name': data.get('name', 'Nomsiz Product'),
|
||||
'description': data.get('description', ''),
|
||||
'price': float(data.get('price', 0)),
|
||||
'discount_price': float(data.get('disPrice', 0)),
|
||||
'quantity': data.get('quantity', -1),
|
||||
'is_publish': data.get('publish', True),
|
||||
'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'}")
|
||||
|
||||
last_doc = docs[-1]
|
||||
print(f"--- {batch_size} ta mahsulot qayta ishlandi ---")
|
||||
|
||||
if __name__ == "__main__":
|
||||
migrate_sections()
|
||||
migrate_categories()
|
||||
migrate_vendors()
|
||||
migrate_products()
|
||||
print("\n✅ Migratsiya muvaffaqiyatli yakunlandi!")
|
||||
Reference in New Issue
Block a user