summaryrefslogtreecommitdiff
path: root/opendc/models
diff options
context:
space:
mode:
authorGeorgios Andreadis <info@gandreadis.com>2020-06-26 12:27:51 +0200
committerGeorgios Andreadis <info@gandreadis.com>2020-06-26 12:27:51 +0200
commit19bede4fc7f7320bb4eb16c3fe1a211b19ab4714 (patch)
tree6a53629feb7bd8c7a9380d204287f3429ed2f378 /opendc/models
parent92b94b59ad80329a2c99471edbf5bbdc9af1e525 (diff)
Revamp error responses everywhere
Diffstat (limited to 'opendc/models')
-rw-r--r--opendc/models/model.py7
-rw-r--r--opendc/models/simulation.py7
-rw-r--r--opendc/models/topology.py7
-rw-r--r--opendc/models/user.py18
4 files changed, 14 insertions, 25 deletions
diff --git a/opendc/models/model.py b/opendc/models/model.py
index d0bf34ee..2505ae61 100644
--- a/opendc/models/model.py
+++ b/opendc/models/model.py
@@ -1,4 +1,5 @@
from opendc.util.database import DB
+from opendc.util.exceptions import ClientError
from opendc.util.rest import Response
@@ -12,11 +13,9 @@ class Model:
def __init__(self, obj):
self.obj = obj
- def validate(self, request_google_id=None):
+ def check_exists(self):
if self.obj is None:
- return Response(404, 'Not found.')
-
- return None
+ raise ClientError(Response(404, 'Not found.'))
def set_property(self, key, value):
self.obj[key] = value
diff --git a/opendc/models/simulation.py b/opendc/models/simulation.py
index f58581cf..5cd3d49e 100644
--- a/opendc/models/simulation.py
+++ b/opendc/models/simulation.py
@@ -1,16 +1,15 @@
from opendc.models.model import Model
from opendc.models.user import User
+from opendc.util.exceptions import ClientError
from opendc.util.rest import Response
class Simulation(Model):
collection_name = 'simulations'
- def validate_user_access(self, google_id, edit_access):
+ def check_user_access(self, google_id, edit_access):
user = User.from_google_id(google_id)
authorizations = list(
filter(lambda x: str(x['simulationId']) == str(self.obj['_id']), user.obj['authorizations']))
if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'):
- return Response(403, "Forbidden from retrieving simulation.")
-
- return None
+ raise ClientError(Response(403, "Forbidden from retrieving simulation."))
diff --git a/opendc/models/topology.py b/opendc/models/topology.py
index b01d5f41..6dde3e2a 100644
--- a/opendc/models/topology.py
+++ b/opendc/models/topology.py
@@ -1,16 +1,15 @@
from opendc.models.model import Model
from opendc.models.user import User
+from opendc.util.exceptions import ClientError
from opendc.util.rest import Response
class Topology(Model):
collection_name = 'topologies'
- def validate_user_access(self, google_id, edit_access):
+ def check_user_access(self, google_id, edit_access):
user = User.from_google_id(google_id)
authorizations = list(
filter(lambda x: str(x['topologyId']) == str(self.obj['_id']), user.obj['authorizations']))
if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'):
- return Response(403, "Forbidden from retrieving topology.")
-
- return None \ No newline at end of file
+ raise ClientError(Response(403, "Forbidden from retrieving topology."))
diff --git a/opendc/models/user.py b/opendc/models/user.py
index ea8b1f3f..cd314457 100644
--- a/opendc/models/user.py
+++ b/opendc/models/user.py
@@ -1,5 +1,6 @@
from opendc.models.model import Model
from opendc.util.database import DB
+from opendc.util.exceptions import ClientError
from opendc.util.rest import Response
@@ -14,21 +15,12 @@ class User(Model):
def from_google_id(cls, google_id):
return User(DB.fetch_one({'googleId': google_id}, User.collection_name))
- def validate(self, request_google_id=None):
- super_validation = super().validate(request_google_id)
-
- if super_validation is not None:
- return super_validation
-
+ def check_correct_user(self, request_google_id):
if request_google_id is not None and self.obj['googleId'] != request_google_id:
- return Response(403, f'Forbidden from editing user with ID {self.obj["_id"]}.')
+ raise ClientError(Response(403, f'Forbidden from editing user with ID {self.obj["_id"]}.'))
- return None
-
- def validate_insertion(self):
+ def check_already_exists(self):
existing_user = DB.fetch_one({'googleId': self.obj['googleId']}, self.collection_name)
if existing_user is not None:
- return Response(409, f'User already exists.')
-
- return None
+ raise ClientError(Response(409, 'User already exists.'))