From 9f87ab4bbab048b527585929135cab80fafd9ef9 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Tue, 30 Jun 2020 13:28:18 +0200 Subject: Address a number of pylint issues --- web-server/opendc/models/model.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 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 b2fd1844..2b8eb4dc 100644 --- a/web-server/opendc/models/model.py +++ b/web-server/opendc/models/model.py @@ -4,31 +4,40 @@ from opendc.util.rest import Response class Model: + """Base class for all models.""" + collection_name = '' @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 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): + """Updates the enclosed object and updates the internal reference to the newly inserted object.""" self.obj = DB.update(self.obj['_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.obj['_id']}, self.collection_name) -- cgit v1.2.3 From a27598ee4755423ebd2f0ad8c505644d644cf2c8 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Tue, 30 Jun 2020 14:07:51 +0200 Subject: Make accessing the ID easer --- web-server/opendc/models/model.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 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 2b8eb4dc..1935638f 100644 --- a/web-server/opendc/models/model.py +++ b/web-server/opendc/models/model.py @@ -21,6 +21,10 @@ class Model: 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: @@ -36,8 +40,8 @@ class Model: def update(self): """Updates the enclosed object and updates the internal reference to the newly inserted object.""" - self.obj = DB.update(self.obj['_id'], self.obj, self.collection_name) + self.obj = DB.update(self.get_id(), self.obj, self.collection_name) def delete(self): """Deletes the enclosed object in the database.""" - DB.delete_one({'_id': self.obj['_id']}, self.collection_name) + DB.delete_one({'_id': self.get_id()}, self.collection_name) -- cgit v1.2.3