From b611118e1a32a2b5e16939063767ebd7285f1331 Mon Sep 17 00:00:00 2001 From: husanjon Date: Fri, 17 Apr 2026 14:05:30 +0500 Subject: [PATCH] face id tog'rilandi --- .../commands/sync_firebase_categories.py | 56 +++++++++++++++++-- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/core/apps/vendors/management/commands/sync_firebase_categories.py b/core/apps/vendors/management/commands/sync_firebase_categories.py index bd97530..fa81818 100644 --- a/core/apps/vendors/management/commands/sync_firebase_categories.py +++ b/core/apps/vendors/management/commands/sync_firebase_categories.py @@ -1,5 +1,5 @@ """ -Firebase sections + vendor_categories → Django DB sync. +Firebase sections + vendor_categories + vendors → Django DB sync. Ishlatish: python manage.py sync_firebase_categories @@ -10,13 +10,12 @@ Docker: """ import os -from datetime import datetime, timezone import firebase_admin from firebase_admin import credentials, firestore 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( os.path.dirname(os.path.abspath(__file__)), @@ -52,7 +51,7 @@ class Command(BaseCommand): return # ── 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()) s_created = s_updated = s_skipped = 0 @@ -88,7 +87,7 @@ class Command(BaseCommand): ) # ── 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()) 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(" python manage.py sync_firebase_products --update")