From de8f12d74faef5fa3f9e38d1340948cab2d06ea3 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Wed, 1 Jul 2020 13:33:31 +0200 Subject: Manually generate IDs --- web-server/opendc/models/model.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'web-server/opendc/models/model.py') diff --git a/web-server/opendc/models/model.py b/web-server/opendc/models/model.py index 1935638f..f42134bf 100644 --- a/web-server/opendc/models/model.py +++ b/web-server/opendc/models/model.py @@ -1,3 +1,5 @@ +from uuid import uuid4 + from opendc.util.database import DB from opendc.util.exceptions import ClientError from opendc.util.rest import Response @@ -23,7 +25,7 @@ class Model: def get_id(self): """Returns the ID of the enclosed object.""" - return self.obj['_id'] + return str(self.obj['_id']) def check_exists(self): """Raises an error if the enclosed object does not exist.""" @@ -35,12 +37,13 @@ class Model: self.obj[key] = value def insert(self): - """Inserts the enclosed object and updates the internal reference to the newly inserted object.""" - self.obj = DB.insert(self.obj, self.collection_name) + """Inserts the enclosed object and generates a UUID for it.""" + self.obj['_id'] = str(uuid4()) + DB.insert(self.obj, self.collection_name) def update(self): """Updates the enclosed object and updates the internal reference to the newly inserted object.""" - self.obj = DB.update(self.get_id(), self.obj, self.collection_name) + DB.update(self.get_id(), self.obj, self.collection_name) def delete(self): """Deletes the enclosed object in the database.""" -- cgit v1.2.3