face id tog'rilandi

This commit is contained in:
2026-04-17 14:05:30 +05:00
parent 427e8ad522
commit b611118e1a

View File

@@ -1,5 +1,5 @@
""" """
Firebase sections + vendor_categories → Django DB sync. Firebase sections + vendor_categories + vendors → Django DB sync.
Ishlatish: Ishlatish:
python manage.py sync_firebase_categories python manage.py sync_firebase_categories
@@ -10,13 +10,12 @@ Docker:
""" """
import os import os
from datetime import datetime, timezone
import firebase_admin import firebase_admin
from firebase_admin import credentials, firestore from firebase_admin import credentials, firestore
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from core.apps.vendors.models import SectionModel, CategoryModel from core.apps.vendors.models import SectionModel, CategoryModel, VendorModel
CERT_PATH = os.path.normpath(os.path.join( CERT_PATH = os.path.normpath(os.path.join(
os.path.dirname(os.path.abspath(__file__)), os.path.dirname(os.path.abspath(__file__)),
@@ -52,7 +51,7 @@ class Command(BaseCommand):
return return
# ── 1. SECTIONS ────────────────────────────────────────────────────── # ── 1. SECTIONS ──────────────────────────────────────────────────────
self.stdout.write("\n[1/2] Sections sinxronlanmoqda…") self.stdout.write("\n[1/3] Sections sinxronlanmoqda…")
section_docs = list(db.collection("sections").stream()) section_docs = list(db.collection("sections").stream())
s_created = s_updated = s_skipped = 0 s_created = s_updated = s_skipped = 0
@@ -88,7 +87,7 @@ class Command(BaseCommand):
) )
# ── 2. VENDOR_CATEGORIES ───────────────────────────────────────────── # ── 2. VENDOR_CATEGORIES ─────────────────────────────────────────────
self.stdout.write("\n[2/2] Kategoriyalar sinxronlanmoqda…") self.stdout.write("\n[2/3] Kategoriyalar sinxronlanmoqda…")
cat_docs = list(db.collection("vendor_categories").stream()) cat_docs = list(db.collection("vendor_categories").stream())
c_created = c_updated = c_skipped = 0 c_created = c_updated = c_skipped = 0
@@ -128,5 +127,52 @@ class Command(BaseCommand):
) )
) )
# ── 3. VENDORS ────────────────────────────────────────────────────────
self.stdout.write("\n[3/3] Vendorlar sinxronlanmoqda…")
vendor_docs = list(db.collection("vendors").stream())
v_created = v_updated = v_skipped = 0
for doc in vendor_docs:
data = doc.to_dict() or {}
fid = data.get("id") or doc.id
section = SectionModel.objects.filter(
firestore_id=data.get("section_id", "")
).first()
coords = data.get("coordinates") or data.get("g", {}).get("geopoint")
location = None
if coords and isinstance(coords, dict):
location = {"lat": coords.get("lat"), "lng": coords.get("lng")}
existing = VendorModel.objects.filter(firestore_id=fid).first()
fields = dict(
title=data.get("title") or "",
description=data.get("description") or "",
phone=data.get("phonenumber") or data.get("phone") or "",
photo_url=data.get("photo") or (data.get("photos") or [None])[0],
location=location,
is_active=bool(data.get("isActive", True)),
section=section,
)
if existing and not update:
v_skipped += 1
continue
if existing:
for k, v in fields.items():
setattr(existing, k, v)
existing.save()
v_updated += 1
else:
VendorModel.objects.create(firestore_id=fid, **fields)
v_created += 1
self.stdout.write(
self.style.SUCCESS(
f" Vendorlar → yaratildi: {v_created}, yangilandi: {v_updated}, o'tkazildi: {v_skipped}"
)
)
self.stdout.write(self.style.SUCCESS("\nTayyor! Endi mahsulotlarni yangilash uchun:")) self.stdout.write(self.style.SUCCESS("\nTayyor! Endi mahsulotlarni yangilash uchun:"))
self.stdout.write(" python manage.py sync_firebase_products --update") self.stdout.write(" python manage.py sync_firebase_products --update")