summaryrefslogtreecommitdiff
path: root/web-server/opendc/models/model.py
diff options
context:
space:
mode:
authorjc0b <j@jc0b.computer>2020-06-30 14:12:07 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2020-08-24 19:43:10 +0200
commit66b2d85385d05abb590535da60341876ecdbab71 (patch)
tree0656f64a4179d419adac86e488e21def7a7fa2b8 /web-server/opendc/models/model.py
parent88d8a9cbeae3466230db6bd13120bd4438abbc66 (diff)
parentc99ef7504a1374170f88b89faeb7e6dec6a55253 (diff)
Merge changes with upstream
Diffstat (limited to 'web-server/opendc/models/model.py')
-rw-r--r--web-server/opendc/models/model.py21
1 files changed, 17 insertions, 4 deletions
diff --git a/web-server/opendc/models/model.py b/web-server/opendc/models/model.py
index b2fd1844..1935638f 100644
--- a/web-server/opendc/models/model.py
+++ b/web-server/opendc/models/model.py
@@ -4,31 +4,44 @@ from opendc.util.rest import Response
class Model:
+ """Base class for all models."""
+
collection_name = '<specified in subclasses>'
@classmethod
def from_id(cls, _id):
- return cls(DB.fetch_one({'_id': _id}, Model.collection_name))
+ """Fetches the document with given ID from the collection."""
+ return cls(DB.fetch_one({'_id': _id}, cls.collection_name))
@classmethod
def get_all(cls):
- return cls(DB.fetch_all({}, Model.collection_name))
+ """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 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."""
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)
def update(self):
- self.obj = DB.update(self.obj['_id'], self.obj, self.collection_name)
+ """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)
def delete(self):
- self.obj = DB.delete_one({'_id': self.obj['_id']}, self.collection_name)
+ """Deletes the enclosed object in the database."""
+ DB.delete_one({'_id': self.get_id()}, self.collection_name)