diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-07-14 21:10:56 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-08-24 19:48:04 +0200 |
| commit | 02997b2522b9c66072b16f1425c02e81e0085e3c (patch) | |
| tree | eb8cb533e3ef37a11598e86736b063293f8b0e2b /web-server/opendc/models/model.py | |
| parent | 1a4776636bf6b585d4a19a6721d9d57b02c88ca4 (diff) | |
Rename web-server to API
This change renames the web-server component to API in order to be more
descriptive of its role. The OpenDC API bridges between the frontend on
one side and the database and simulator on the other side.
Diffstat (limited to 'web-server/opendc/models/model.py')
| -rw-r--r-- | web-server/opendc/models/model.py | 59 |
1 files changed, 0 insertions, 59 deletions
diff --git a/web-server/opendc/models/model.py b/web-server/opendc/models/model.py deleted file mode 100644 index bcb833ae..00000000 --- a/web-server/opendc/models/model.py +++ /dev/null @@ -1,59 +0,0 @@ -from uuid import uuid4 - -from opendc.util.database import DB -from opendc.util.exceptions import ClientError -from opendc.util.rest import Response - - -class Model: - """Base class for all models.""" - - collection_name = '<specified in subclasses>' - - @classmethod - def from_id(cls, _id): - """Fetches the document with given ID from the collection.""" - return cls(DB.fetch_one({'_id': _id}, cls.collection_name)) - - @classmethod - def get_all(cls): - """Fetches all documents from the collection.""" - return cls(DB.fetch_all({}, cls.collection_name)) - - def __init__(self, obj): - self.obj = obj - - def get_id(self): - """Returns the ID of the enclosed object.""" - return str(self.obj['_id']) - - def check_exists(self): - """Raises an error if the enclosed object does not exist.""" - if self.obj is None: - raise ClientError(Response(404, 'Not found.')) - - def set_property(self, key, value): - """Sets the given property on the enclosed object, with support for simple nested access.""" - if '.' in key: - keys = key.split('.') - self.obj[keys[0]][keys[1]] = value - else: - self.obj[key] = value - - def insert(self): - """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.""" - DB.update(self.get_id(), self.obj, self.collection_name) - - def delete(self): - """Deletes the enclosed object in the database, if it existed.""" - if self.obj is None: - return None - - old_object = self.obj.copy() - DB.delete_one({'_id': self.get_id()}, self.collection_name) - return old_object |
