From 3083c9ede5c81201131f6a13ed20c31fcb22d6ce Mon Sep 17 00:00:00 2001 From: ZorEl212 Date: Tue, 10 Sep 2024 20:21:42 -0400 Subject: [PATCH] Refactor DB `get()` method to return a Python object instead of a dictionary for easier handling. Update `check_server_details()` to align with this change Signed-off-by: ZorEl212 --- models/auth.py | 6 +++--- models/engine/mongodb.py | 22 ++++++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/models/auth.py b/models/auth.py index 3828f95..e2a35f0 100644 --- a/models/auth.py +++ b/models/auth.py @@ -76,7 +76,7 @@ def create_token(cls, data, sid): @classmethod def check_server_details(cls, user_id, serverId): server = storage.get('Server', serverId) - if (server is None or server.get('serverId') != serverId or - server.get('userId') != user_id): - return None + if (server is None or server.id != serverId or + server.userId != user_id): + return False return server.to_dict() diff --git a/models/engine/mongodb.py b/models/engine/mongodb.py index fdfd0ae..f44c32f 100644 --- a/models/engine/mongodb.py +++ b/models/engine/mongodb.py @@ -31,22 +31,32 @@ def all(self, cls=None): return docs def new(self, obj): - collection = self.database[obj.get_cls_name()] + collection = self.database[obj.cls_name()] collection.insert_one(obj.to_dict()) def delete(self, obj): - collection = self.database[obj.get_cls_name()] + collection = self.database[obj.cls_name()] collection.delete_one({'id': obj.id}) def get(self, cls, id): - collection = self.database[cls] + if isinstance(cls, str): + cls = next((c for c in self.classes if c.get_cls_name() == cls), None) + + if cls is None: + return None + + collection = self.database[cls.get_cls_name()] document = collection.find_one({'id': id}) + if document is not None: - document.pop('_id') - return document + document.pop('_id') + document.pop('cls_name') + return cls(**document) + + return None # Return None if document is not found def update(self, obj): - collection = self.database[obj.get_cls_name()] + collection = self.database[obj.cls_name()] collection.update_one({'id': obj.id}, {'$set': obj.to_dict()}) return obj