summaryrefslogtreecommitdiff
path: root/api/opendc/models
diff options
context:
space:
mode:
Diffstat (limited to 'api/opendc/models')
-rw-r--r--api/opendc/models/__init__.py0
-rw-r--r--api/opendc/models/model.py64
-rw-r--r--api/opendc/models/portfolio.py24
-rw-r--r--api/opendc/models/prefab.py28
-rw-r--r--api/opendc/models/project.py31
-rw-r--r--api/opendc/models/scenario.py26
-rw-r--r--api/opendc/models/topology.py27
-rw-r--r--api/opendc/models/trace.py7
-rw-r--r--api/opendc/models/user.py36
9 files changed, 0 insertions, 243 deletions
diff --git a/api/opendc/models/__init__.py b/api/opendc/models/__init__.py
deleted file mode 100644
index e69de29b..00000000
--- a/api/opendc/models/__init__.py
+++ /dev/null
diff --git a/api/opendc/models/model.py b/api/opendc/models/model.py
deleted file mode 100644
index f9dfc9ad..00000000
--- a/api/opendc/models/model.py
+++ /dev/null
@@ -1,64 +0,0 @@
-from bson.objectid import ObjectId
-
-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."""
- 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
- 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 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'] = ObjectId()
- 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
diff --git a/api/opendc/models/portfolio.py b/api/opendc/models/portfolio.py
deleted file mode 100644
index 32961b63..00000000
--- a/api/opendc/models/portfolio.py
+++ /dev/null
@@ -1,24 +0,0 @@
-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 Portfolio(Model):
- """Model representing a Portfolio."""
-
- collection_name = 'portfolios'
-
- def check_user_access(self, google_id, edit_access):
- """Raises an error if the user with given [google_id] has insufficient access.
-
- Checks access on the parent project.
-
- :param google_id: The Google ID of the user.
- :param edit_access: True when edit access should be checked, otherwise view access.
- """
- user = User.from_google_id(google_id)
- authorizations = list(
- filter(lambda x: str(x['projectId']) == str(self.obj['projectId']), user.obj['authorizations']))
- if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'):
- raise ClientError(Response(403, 'Forbidden from retrieving/editing portfolio.'))
diff --git a/api/opendc/models/prefab.py b/api/opendc/models/prefab.py
deleted file mode 100644
index edf1d4c4..00000000
--- a/api/opendc/models/prefab.py
+++ /dev/null
@@ -1,28 +0,0 @@
-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 Prefab(Model):
- """Model representing a Project."""
-
- collection_name = 'prefabs'
-
- def check_user_access(self, google_id):
- """Raises an error if the user with given [google_id] has insufficient access to view this prefab.
-
- :param google_id: The Google ID of the user.
- """
- user = User.from_google_id(google_id)
-
- # TODO(Jacob) add special handling for OpenDC-provided prefabs
-
- #try:
-
- print(self.obj)
- if self.obj['authorId'] != user.get_id() and self.obj['visibility'] == "private":
- raise ClientError(Response(403, "Forbidden from retrieving prefab."))
- #except KeyError:
- # OpenDC-authored objects don't necessarily have an authorId
- # return
diff --git a/api/opendc/models/project.py b/api/opendc/models/project.py
deleted file mode 100644
index b57e9f77..00000000
--- a/api/opendc/models/project.py
+++ /dev/null
@@ -1,31 +0,0 @@
-from opendc.models.model import Model
-from opendc.models.user import User
-from opendc.util.database import DB
-from opendc.util.exceptions import ClientError
-from opendc.util.rest import Response
-
-
-class Project(Model):
- """Model representing a Project."""
-
- collection_name = 'projects'
-
- def check_user_access(self, google_id, edit_access):
- """Raises an error if the user with given [google_id] has insufficient access.
-
- :param google_id: The Google ID of the user.
- :param edit_access: True when edit access should be checked, otherwise view access.
- """
- user = User.from_google_id(google_id)
- authorizations = list(filter(lambda x: str(x['projectId']) == str(self.get_id()),
- user.obj['authorizations']))
- if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'):
- raise ClientError(Response(403, "Forbidden from retrieving project."))
-
- def get_all_authorizations(self):
- """Get all user IDs having access to this project."""
- return [
- str(user['_id']) for user in DB.fetch_all({'authorizations': {
- 'projectId': self.obj['_id']
- }}, User.collection_name)
- ]
diff --git a/api/opendc/models/scenario.py b/api/opendc/models/scenario.py
deleted file mode 100644
index 8d53e408..00000000
--- a/api/opendc/models/scenario.py
+++ /dev/null
@@ -1,26 +0,0 @@
-from opendc.models.model import Model
-from opendc.models.portfolio import Portfolio
-from opendc.models.user import User
-from opendc.util.exceptions import ClientError
-from opendc.util.rest import Response
-
-
-class Scenario(Model):
- """Model representing a Scenario."""
-
- collection_name = 'scenarios'
-
- def check_user_access(self, google_id, edit_access):
- """Raises an error if the user with given [google_id] has insufficient access.
-
- Checks access on the parent project.
-
- :param google_id: The Google ID of the user.
- :param edit_access: True when edit access should be checked, otherwise view access.
- """
- portfolio = Portfolio.from_id(self.obj['portfolioId'])
- user = User.from_google_id(google_id)
- authorizations = list(
- filter(lambda x: str(x['projectId']) == str(portfolio.obj['projectId']), user.obj['authorizations']))
- if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'):
- raise ClientError(Response(403, 'Forbidden from retrieving/editing scenario.'))
diff --git a/api/opendc/models/topology.py b/api/opendc/models/topology.py
deleted file mode 100644
index cb4c4bab..00000000
--- a/api/opendc/models/topology.py
+++ /dev/null
@@ -1,27 +0,0 @@
-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):
- """Model representing a Project."""
-
- collection_name = 'topologies'
-
- def check_user_access(self, google_id, edit_access):
- """Raises an error if the user with given [google_id] has insufficient access.
-
- Checks access on the parent project.
-
- :param google_id: The Google ID of the user.
- :param edit_access: True when edit access should be checked, otherwise view access.
- """
- user = User.from_google_id(google_id)
- if 'projectId' not in self.obj:
- raise ClientError(Response(400, 'Missing projectId in topology.'))
-
- authorizations = list(
- filter(lambda x: str(x['projectId']) == str(self.obj['projectId']), user.obj['authorizations']))
- if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'):
- raise ClientError(Response(403, 'Forbidden from retrieving topology.'))
diff --git a/api/opendc/models/trace.py b/api/opendc/models/trace.py
deleted file mode 100644
index 2f6e4926..00000000
--- a/api/opendc/models/trace.py
+++ /dev/null
@@ -1,7 +0,0 @@
-from opendc.models.model import Model
-
-
-class Trace(Model):
- """Model representing a Trace."""
-
- collection_name = 'traces'
diff --git a/api/opendc/models/user.py b/api/opendc/models/user.py
deleted file mode 100644
index 8e8ff945..00000000
--- a/api/opendc/models/user.py
+++ /dev/null
@@ -1,36 +0,0 @@
-from opendc.models.model import Model
-from opendc.util.database import DB
-from opendc.util.exceptions import ClientError
-from opendc.util.rest import Response
-
-
-class User(Model):
- """Model representing a User."""
-
- collection_name = 'users'
-
- @classmethod
- def from_email(cls, email):
- """Fetches the user with given email from the collection."""
- return User(DB.fetch_one({'email': email}, User.collection_name))
-
- @classmethod
- def from_google_id(cls, google_id):
- """Fetches the user with given Google ID from the collection."""
- return User(DB.fetch_one({'googleId': google_id}, User.collection_name))
-
- def check_correct_user(self, request_google_id):
- """Raises an error if a user tries to modify another user.
-
- :param request_google_id:
- """
- if request_google_id is not None and self.obj['googleId'] != request_google_id:
- raise ClientError(Response(403, f'Forbidden from editing user with ID {self.obj["_id"]}.'))
-
- def check_already_exists(self):
- """Checks if the user already exists in the database."""
-
- existing_user = DB.fetch_one({'googleId': self.obj['googleId']}, self.collection_name)
-
- if existing_user is not None:
- raise ClientError(Response(409, 'User already exists.'))