From 6a29cba09ffd17e3ce12a73eef5a7286cd303ce0 Mon Sep 17 00:00:00 2001 From: johnpccd Date: Sat, 24 May 2025 06:27:04 +0200 Subject: [PATCH] fix db --- src/db/models/team.py | 2 +- src/db/providers/firestore_provider.py | 56 ++++++++++++++++++++------ 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/db/models/team.py b/src/db/models/team.py index 63281e8..67d148b 100644 --- a/src/db/models/team.py +++ b/src/db/models/team.py @@ -10,7 +10,7 @@ class PyObjectId(ObjectId): yield cls.validate @classmethod - def validate(cls, v): + def validate(cls, v, self_instance=None): if not ObjectId.is_valid(v): raise ValueError('Invalid ObjectId') return ObjectId(v) diff --git a/src/db/providers/firestore_provider.py b/src/db/providers/firestore_provider.py index 03b81fc..f4387ae 100644 --- a/src/db/providers/firestore_provider.py +++ b/src/db/providers/firestore_provider.py @@ -29,13 +29,16 @@ class FirestoreProvider: """Connect to Firestore""" try: if settings.GCS_CREDENTIALS_FILE and os.path.exists(settings.GCS_CREDENTIALS_FILE): - self.client = firestore.Client.from_service_account_json(settings.GCS_CREDENTIALS_FILE) + self.client = firestore.Client.from_service_account_json( + settings.GCS_CREDENTIALS_FILE, + database='imagedb' # Use the specific database ID + ) else: - # Use application default credentials - self.client = firestore.Client() + # Use application default credentials with specific database + self.client = firestore.Client(database='imagedb') self._db = self.client - logger.info("Connected to Firestore") + logger.info("Connected to Firestore database: imagedb") return True except Exception as e: logger.error(f"Failed to connect to Firestore: {e}") @@ -71,23 +74,28 @@ class FirestoreProvider: collection = self.get_collection(collection_name) # Handle ObjectId conversion for Firestore + processed_data = {} for key, value in data.items(): - if hasattr(value, '__str__') and key != 'id': - data[key] = str(value) + if value is None: + processed_data[key] = None + elif hasattr(value, '__str__') and key != 'id': + processed_data[key] = str(value) + else: + processed_data[key] = value # Handle special case for document ID doc_id = None - if "_id" in data: - doc_id = str(data["_id"]) - del data["_id"] + if "_id" in processed_data: + doc_id = str(processed_data["_id"]) + del processed_data["_id"] # Add document to Firestore if doc_id: doc_ref = collection.document(doc_id) - doc_ref.set(data) + doc_ref.set(processed_data) return doc_id else: - doc_ref = collection.add(data) + doc_ref = collection.add(processed_data) return doc_ref[1].id except Exception as e: logger.error(f"Error adding document to {collection_name}: {e}") @@ -109,6 +117,24 @@ class FirestoreProvider: doc = doc_ref.get() if doc.exists: data = doc.to_dict() + # Properly handle None values and complex types + for key, value in data.items(): + if value == 'None' or value == 'null': + data[key] = None + # Handle lists stored as strings + elif isinstance(value, str) and value.startswith('[') and value.endswith(']'): + try: + import ast + data[key] = ast.literal_eval(value) + except (SyntaxError, ValueError): + pass # Keep as string if conversion fails + # Handle dictionaries stored as strings + elif isinstance(value, str) and value.startswith('{') and value.endswith('}'): + try: + import ast + data[key] = ast.literal_eval(value) + except (SyntaxError, ValueError): + pass # Keep as string if conversion fails data["_id"] = doc_id return data return None @@ -154,9 +180,13 @@ class FirestoreProvider: # Process data for Firestore processed_data = {} for key, value in data.items(): - if key != "_id" and hasattr(value, '__str__'): + if key == "_id": + continue + elif value is None: + processed_data[key] = None + elif hasattr(value, '__str__'): processed_data[key] = str(value) - elif key != "_id": + else: processed_data[key] = value doc_ref = self.get_collection(collection_name).document(doc_id)