diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-10-26 23:09:37 +0100 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-10-26 23:22:57 +0100 |
| commit | eb723af5ce2787dd864c97d44767cbf9bc73076b (patch) | |
| tree | c23add298fa668b9d1b7b979088755ecb12bce10 /api/opendc/models | |
| parent | 81003bf38eb5384f6b7f485b5186e1df4e7430b4 (diff) | |
Do not hardcode test ids inside tests
Diffstat (limited to 'api/opendc/models')
| -rw-r--r-- | api/opendc/models/model.py | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/api/opendc/models/model.py b/api/opendc/models/model.py index 32159ca3..f9dfc9ad 100644 --- a/api/opendc/models/model.py +++ b/api/opendc/models/model.py @@ -1,4 +1,4 @@ -from uuid import uuid4 +from bson.objectid import ObjectId from opendc.util.database import DB from opendc.util.exceptions import ClientError @@ -13,6 +13,11 @@ class Model: @classmethod def from_id(cls, _id): """Fetches the document with given ID from the collection.""" + if isinstance(_id, str) and len(_id) == 24: + _id = ObjectId(_id) + elif not isinstance(_id, ObjectId): + return cls(None) + return cls(DB.fetch_one({'_id': _id}, cls.collection_name)) @classmethod @@ -42,7 +47,7 @@ class Model: def insert(self): """Inserts the enclosed object and generates a UUID for it.""" - self.obj['_id'] = str(uuid4()) + self.obj['_id'] = ObjectId() DB.insert(self.obj, self.collection_name) def update(self): |
